Hey everyone, just started using an arduino, this is my first question.
I'm using an Arduino Duemilanove,
Basically I want to use the analogRead on Pin 2 and the program will do something if (val == 1000 ), val is equal to the analogRead.
Everything works fine if i:
A) Connect Pin 2 to ground: Nothing Happens (good)
B) Connect Pin 2 to 3.3V: Nothing Happens (good)
C) Connect Pin 2 to 5V: The LED turns on (good)
D) if I just connect a wire to Pin 2, and the other end is connected to nothing, every once in a while it will turn on randomly. For example, if I touch it with my finger.
What is causing Case D, and how can I fix it?
Please help! Thanks so much!
Zachary
analogRead() only works on the Analog Pins. So first, make sure you are using the Analog Pins and not Digital Pin 2.
When you attach a wire to the pin and leave the pin floating in the air, what voltage do you think is present? Keep in mind that "0" is not a correct answer. A floating pin (in your case is extended by a wire) is just that, floating. You have no way of knowing what voltage will be present at the pin because it isn't tied in any direction.
Ok, so I can't keep it floating....
OH! I know, I'll attach the 3.3V pin to the Analog Input Pin 2 so it will never float, and then when I send the 5V signal to Pin 2 it will be at least 5 V which is what I want!
I just tried that and it works, yes!
Thanks so much!
Zachary
OH! I know, I'll attach the 3.3V pin to the Analog Input Pin 2 so it will never float, and then when I send the 5V signal to Pin 2 it will be at least 5 V which is what I want!
Uh yeah, that won't work. Do a search for "pull-up resistors."
@James C4S Maybe I didn't explain what I did but it's working now!
It's sending a 3.3V signal when the 5V signal isn't on, and a 5V signal when it's on. (Based on my Multimeter readings.) and the Arduino is working as it should (Case A-C, no D).
As long as I'm not doing something that could damage the Arduino, I'm ok!
I'm using an old switch I found in my college's lab.
When the button is not pressed, the input and output wires aren't connected to anything, which was giving me the "floating" trouble.
So what I did was plug the output wire of the switch to a line on my breadboard, then connected the Analog Input Pin 2 and the 3.3V to the same line on the breadboard.
So when the old switch is pressed, 5V gets sent to the line in the breadboard and goes to the input pin, when its not pressed just the 3.3 volts go.
So when the old switch is pressed, 5V gets sent to the line in the breadboard and goes to the input pin, when its not pressed just the 3.3 volts go.
Okay, I misunderstood what you originally said. I thought you were leaving the 3.3V source connected when applying the 5.0V source. (Which would cause problems.)
Don't do anything with the 3v3 line, disconnect it, remove it, you don't need it. You might damage your arduino if you continue.
When the button is not pressed, the input and output wires aren't connected to anything, which was giving me the "floating" trouble.
So connect the button from the input to ground and connect a 10K resistor from the input to +5. You can use an analogue or digital input with this configuration.
Connecting the 5V pin to the 3.3V pin might cause trouble. Since 5V is higher than 3.3V, depending on the circuitry, you might be causing the current to drain from the 5V through the 3.3V pin. If you connected a diode from the 3.3V pin to the switch, you'd probably be ok. The diode prevents the current from traveling the wrong way through the circuit. The pull up/down resistors mentioned previously would accomplish a similar objective.