Arduino Pro Mini vs. Duemilanove A. Input Question

Hi! I have a project that I ran on a an Arduino Deumilanove that I am now transfering over to an Arduino Pro Mini. As part of this project, I have a push button connected to ground and to an analogue input. On the Deumilanove, I set the pin to read mode and poll its voltage and when it reads 0 I see that the button has been pushed using some debounce code (the reads produce random noize when the button is not down). This seems to not be possible on the pro mini however because when I read values on the pin, it doesn't read just some noize value. It seems to read either 1023 or 0 depending on whether the yellow light is on. This is no good and disallows me to finish my project. Is there a work around? Why does this happen and is there a work around without adding extra pins? (I dont have any outputs or inputs left)

Are you using a pull-up or pull-down resistor with that switch?

It seems to read either 1023 or 0 depending on whether the yellow light is on.

What yellow light? On the switch?

I'd expect a properly wired switch on either the Duemilanove or the Mini to read 0 or 1023 depending on whether or not the switch is pressed, and on whether the resistor is wired as a pull-up or pull-down resistor.

For the Duemilanove, I just set the pin to input, but neither high or low. For the pro mini I set it to input and tried all combinations of setting high, low, or to nothing at all.

The yellow light I'm talking about it the one that goes on when one of the pins (13 I believe?) is high. I think there is some sort of cross noise that makes the input read high when the light is on or something.

According to the reference setting the pin to Input/HIGH should have used the pullup resistors.

You said the switch was connected to an analog pin. Analog pins don't have internal pull-up resistors and don't need to have the mode set (they are input only).

ASAIK analogue pins can operate as inputs or outputs right? I'm using the rest of them as outputs in my project. I tried not setting it as an input and setting it as an input and there was no change. The value just seems to oscillate with the light. Also, even when I had my button pressed down and the pin was grounded, it would still read the light value and not 0 :S

Are you using analogRead() to read the state of a switch? Why? See Footnote.

Why not just set the pin as an input (with pinMode INPUT) and then turn on the pullup resistor for that pin (with digitalWrite HIGH)? Then use digitalRead to get the switch state. (Actually the pin will already be in input mode when the chip is reset, but I like to put the pinMode instruction there just for my own amusement and edification.)

Analog pin A0 can be used with digitalRead or digitalWrite to digital pin D14. Analog pin A1 can be used with digitalRead or digitalWrite to digital pin D15. Etc.

Regards,

Dave

Footnote:

If you really want to use analogRead for getting a switch value (and the switch is connected to ground) you must have a pullup on the pin. Either connect an external pullup resistor (anything from 10K to 50K Ohms would be appropriate) or turn on the internal pullup for that pin, with pinMode INPUT and with digitalWrite HIGH.

Bottom line: Whether you use analogRead or digitalRead, if the input is not pulled up (internally or externally) the floating pin will be very susceptible to noise, and very well might follow some other signal on the board (such as the output to digital pin D13, which is connected to the LED.) Never leave input pins floating if you expect to read something meaningful.

Wow, thanks a ton! That worked!

I guess in my old project I forgot that you could digitally read from analogue pins so I used analogue read instead. But what still confuses me is why there is a difference between the analogueRead and digitalRead values, more specifically why digitalRead always reads 1 unless the button is down regardless while analogueRead oscillated depending on the button.

THANKS A TON!

As I mentioned:

You didn't have a pullup when you used analogRead. Note that you can do the "pinMode INPUT" followed by "digitalWrite HIGH" on D14 and then do analogRead on A0. The analog input values then should be either something pretty close to zero or something pretty close to 1023, depending on the switch state.

But we are beyond that now? Right? Using digitalRead.

It is nice to be able to come up with an explanation of what we see, in case we ever see it again. See Footnotes.

Regards,

Dave

Footnotes:
"Those who cannot remember the past are condemned to repeat it..."
---George Santayana

"And the study of history makes it possible to recognize the same
lousy, stinkin' mistakes when we make them over and over again..."
---davekw7x

"Those who do not study history are doomed
to repeat it...and the same goes for English
and algebra."
---Woody Boyd (on Cheers)