I set my pushbutton switch up incorrectly - arduino

I am fiddling with an Arduino Uno and trying to get two switches to operate two servo motors. I am not able to do this right now, and I am not sure what my issue is. I know that my servos are wired correctly; however, I highly doubt that my switches are correctly wired.
Here is a diagram of my circuit and the code that I am using.
#include <Servo.h>
Servo servo_11;
Servo servo_10;
void setup()
{
servo_11.attach(11);
servo_10.attach(10);
pinMode(A3, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A2, INPUT);
}
void loop()
{
servo_11.write(0);
servo_10.write(0);
digitalWrite(A3, LOW);
digitalWrite(A2, LOW);
if (analogRead(A2) != LOW) {
servo_11.write(180);
}
delay(10);
}
Circuit image
If anyone could point out what's wrong, I would really appreciate it!!

You have floating inputs. When the button isn't pressed the pin isn't connected to anything. So the pin can read whatever noise it happens to pick up from the environment. You need pull-down or pull-up resistors. If you use pull-ups, then you can use the ones built into the chip.
You also seem to be confusing analogRead and digitalRead. It's digitalRead that gets HIGH and LOW. analogRead gets values from 0 to 1023 for 0 to 5V.

pull-down resistor circuit
try looking on this to understand the concept
it makes sure that when you dont press the pin will read ground and only when it presses the button you make voltage divider that ups the volage to a non-zero to give you HIGH

Related

Arduino LED not blinking

I am a beginner to ARDUINO. My connections i guess is fine. But LED does not blink. Green and Orange light is blinking on ARDUINO. Anode to pin 13 with 320K resistor and Cathode to ground
Code is as follows
const int LED=13;
void setup()
{
// put your setup code here, to run once:
pinMode(LED,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(LED,HIGH);
delay(1000);
digitalWrite(LED,LOW);
delay(1000);
}
Looks like you plugged one of the legs in ground and one in Another slot.
Check your connections and if it doesn't work, try using another led, and switching the polarities.
The problem here in that you haven't connected anything to pin 13!
Also, a 320K ohm resistor is a little overkill. Usualy, a 220 ohm resistor is enough.
Also, the flat side of the led is the negative side. It is more reliable then if you are following the pins size which isn't always constant.
You need a resistor even if using pin13.
The led in the shield has it's own resistor but if you plug your own you need a resistor.
Take a look at this post:
https://electronics.stackexchange.com/questions/66992/pin-13-do-i-need-a-resistor
hello you can try removing the 320K resistor and replacing it with 220 ohms resistor or directly connec t LED to pin 13 because the onboard resistor is on

Pins that I used in LCD are not working as OUTPUT for LED

I am running out of pins. I used the pin that I use in LCD as output for a LED however the led is not turning off its always even my code is like this
void setup() {
lcd.begin(20, 4);
pinMode(7 , OUTPUT);
digitalWrite(7, low);
}
Plug off the LCD for a while to see whether your led blinking program is correct.
Anyway, you can't use the same pin for different functions, it's good chance that they will
disturb each other (say, the led will flash silly when you send something to the LCD),
kick out each other (as we see it in your case).

Arduino 16x2 LCD Stays Blank

I am using an Arduino Mega with a 16x2 LCD. When I start the Arduino the LCD flashes white and sometimes random lines will show up and gradually fade out. The backlight is on, however, so the LCD is not inverted. At first I thought the Arduino was not getting enough power because I am using a Raspberry Pi to program it, but plugging in a 6V battery pack did not change the result. I tried plugging in a different 16x2 LCD to check if the one I was using is broken, but again, the result was the same. I have triple-checked my connections, adjusted the potentiometer, and fiddled around with where the LCD was connected on the breadboard in case some of the pins were broken, but to no avail.
Does the LCD have a problem with Arduino Mega boards? Or am I just unfortunate enough to have two broken LCDs?
Code:
#include <LiquidCrystal.h>
#define led_pin 22
#define buzzer_pin 7
LiquidCrystal lcd(12,11,5,4,3,2);
void setup() {
pinMode(led_pin, OUTPUT);
pinMode(buzzer_pin, OUTPUT);
lcd.begin(16,2);
digitalWrite(led_pin, HIGH);
tone(buzzer_pin, 1000, 500);
delay(500);
digitalWrite(led_pin, LOW);
lcd.print("LCD Test");
}
void loop() {
}
EDIT
I moved the LCD and wiring back to the other side of the breadboard and it did not change the output. I'm starting the think that the LCDs are just simply broken because the LCDs will randomly flash and fade unevenly. I found that these LCDs can be easily fried by incorrectly adjusting the potentiometer.
Make sure that you solder headers to the lcd, then press the headers into your breadboard and connect to arduino with jumper wires. Just resting the LCD on the headers without solder or jabbing the lcd pin holes with jumper wires don't make reliable connections. You WILL see random stuff
Make sure you connect the LCD correctly. If you did, you wouldn't have got any problems. Post a picture.
Make sure the pins you used in the code match pins you connected to the LCD.
Best way to get arduino help is on arduino forum.
Specifically which LCD are you using?
Do you have datasheets for the exact LCD you are using so you can confirm you have the correct wiring and supply voltages?
If you still have no luck, you may want to try a LCD module that has a serial, i2c, or spi interface.
They are much easier to use than HD44780 parallel modules (im assuming this is what you have).
An example source of such modules is https://www.crystalfontz.com/c/character-lcd-displays/interface/24

Reading sensor input with Pic32 and MPIDE

So I currently have a pic32 arduino. I'm pretty new to this stuff, so any tips would be appreciated.
I have a sensor that has 3 pins, 5VDC, ground, and sensor output. I connected the sensor output and ground header to the two pin slots at PORT0.
For some reason, the program always reads that the sensor is HIGH, even if the sensor is not connected.
If I connect the output to a breadboard with an LED, I can see the LED toggle on and off.
Here is my code:
const int sensor = 0; //sensor port
int sensorState = LOW;
void setup(){
pinMode(ledPin, OUTPUT);
pinMode(piezo, OUTPUT);
pinMode(sensor, INPUT);
Serial.begin(9600);
}
void loop(){
sensorState = digitalRead(sensor);
if(sensorState == HIGH)
alarm();
digitalWrite(ledPin, sensorState);
Serial.println(sensorState);
}
You may have the internal pull-up resistor enabled, so when nothing is connected, it will read high.
Also, these two statements are contradictory:
For some reason, the program always reads that the sensor is HIGH, even if the sensor is not connected.
If I connect the output to a breadboard with an LED, I can see the LED toggle on and off.
So the program always reads high but the LED toggles on or off? Which one is it?
If you manually pull the pin to ground, does your program react the way it is supposed to? If it does, then you should take a look at your sensor circuit.
Your sensor circuit sounds weird - you say
I have a sensor that has 3 pins, 5VDC, ground, and sensor output. I connected the sensor output and ground header to the two pin slots at PORT0
So the sensor output and ground are connected to pin zero? 5v should go to 5v, ground should go to ground, the sensor output should go to pin zero.

Arduino ultrasonic initialisation issue

Hardware:
Arduino MEGA 2560
2 x MaxBotix MaxSonar-EZ0
Software (relating to Ultrasonics, by no means the entire program):
void setup() {
Serial.begin(9600);
//Ultrasonic Left
pinMode(26, OUTPUT);
pinMode(2, INPUT);
digitalWrite(26, LOW);
//Ultrasonic Right
pinMode(27, OUTPUT);
pinMode(3, INPUT);
digitalWrite(27, LOW);
}
void readSonar() {
digitalWrite(26, HIGH);
delayMicroseconds(25);
digitalWrite(26, LOW);
data[0] = pulseIn(2, HIGH);
digitalWrite(27, HIGH);
delayMicroseconds(25);
digitalWrite(27, LOW);
data[1] = pulseIn(3, HIGH);
return data;
}
Problem:
When the Arduino is first booted, the readings from the two Ultrasonic sensors are not being updated. They are reporting as non-zero values, typically in the range of 500 - 1500. They fluctuate a little (most likely due to noise in the power supply), but tend to stay around the value that they initialise to.
As per the data sheet for these sensors, there are no obstacles within 14 inches of the sensors during the initialisation stage.
By simply disconnecting and reconnecting the cable going to the sensors (from the back of the sensor, not directly to the Arduino inputs), I am able to receive accurate readings from the sensors immediately.
Has anyone had this problem before? My setup() function looks 'normal' from the examples that I have seen. In order to fix this problem, I have connected a switch for the active lines of both sensors. This way the Arduino can boot and then I can give the sensors power. This seems like a botched workaround to me, and I would like a hard-coded software solution, if anyone is able to provide one!
If you look at the characteristics of the sensor it looks clear that you need to start them with the Rx at 0. Look at this link. This is most probably the reason why you have to disconnect the sensor after you start the Arduino to get it working. You also need to make sure that you have pullup resistors connected to avoid unreliability of the readings.

Resources