Trouble with basic switching

Hello! I am having some strange issues where I can change the reading of an arduino input.. simply by touching the wire.

is an example :stuck_out_tongue:

I am guessing that this is a simple error - or I need to use some pull up / pull down resistor or something.

Any ideas?

I am unable to have a simple on / off switch for a LED (based on arduino I/O without having the same strange behavior.

Not strange, just the result of a floating input not wired to anything. And yes if the input is wired to a pull-up or pull-down resistor then you shouldn't see any noise. If you are going to use the input to a switch that also connects to ground then read up on how to apply a "soft pull-up" via sending an output low to the input pin.

Lefty

Hmm ok - so what are the arduino specs for I/O...

Is something above 3.7V a logic HIGH? and below 0.6 a logic LOW?

I could not find that information anywhere..

I still don't see how touching a wire could possibly register as a HIGH and turn on an LED.. :frowning:

Because of the very high input impedenace of a digital input, it will couple to normal AC noise when you touch them. Depending on how you wire your switch, just adding a line of code to turn on the internal soft pull up or pull down feature, this problem will go away.
This is also seen on the analog input pins. I can drive the value to max 5vdc by touching an unterminated analog input pin.
Lefty

Oh wow - thanks for explaining. Are there any resources on this board or in the playground explaining the code to do the software pull up?

This seems like a simple fix - I am just still new to the arduino!

Thanks again for the help

See if this helps:

http://www.arduino.cc/playground/Learning/Pins

Lefty

I read that article which was really useful / helpful.
Will paste code / video (possibly) if this does not seem to fix my issues.

Seems like the built-in functionality of the arduino should be enough!

Also - if anyone knows.. what kind of current does the arduino accept for a power supply. I was looking to get a 9v power supply for the board... but can not find what current is ideal.

Thanks much!

That's like asking how big a faucet a bucket "supports." :slight_smile:

The wall-wart I have is rated for 9VDC at 650mA.

The current required is dependent on what you're actually doing with the device. If you drive everything you can, as fast as you can, up to the thermal and switching limit of the hardware, you might need more than 650mA. Unless you're trying to move motors or burn a hundred LEDs brightly, you won't even approach that range.

Ok, so here is my code:

int button = HIGH;     
int output = LOW;    

int inPin = 2;    
int outPin = 1;     

int check = 0;
void setup()
{
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
  //digitalWrite(inPin, HIGH);
}

void loop()
{
  button = digitalRead(inPin);

    if (button == HIGH)
    {
      digitalWrite(outPin, HIGH);
    }
    else
    {
      //output = LOW;
      //digitalWrite(outPin, output);
      digitalWrite(outPin, LOW);
    } 
}

I have the LED seemingly working... sometimes...
Please check the video here:

I have the switch as in input to pin 2
and an output as pin 1...

I feed the 5v from the arduino power in through the switch into pin 2
I also tried throwing a resistor in before the switch for fun, without any change in results.

Please help!

"I feed the 5v from the arduino power in through the switch into pin 2
I also tried throwing a resistor in before the switch for fun, without any change in results."

Try wiring a resistor from pin5 EDIT Pin2 to ground. Without a pull down resistor, just opening up the switch does not ensure a digital low will be seen by the input pin, rather the input pin will just 'float' and would most likely stay high. The resistor will force a low when the switch is opened, and the when the switch is closed the +5vdc will 'over power' the resistor and the input pin will see a high.

Lefty

Sorry if this is horrible stupid sounding - but pin5? I am not using pin5 in the design..
Should I adjust the code to accomidate a pin 5 and set it to low? Or are you talking about the ground pin on the arduino?

Thanks much lefty - you have been a great help!

Sorry, I meant pin 2 (I edited my original, should not allow spell check to be the only proof reader ). The same pin in which you are applying the switched +5vdc is the one that should also use a pull down resistor.

Lefty

Isn't there a way to do this with software?

I am having a hard time imagining that every user with an arduino has to use a resistor for every switch they use..!

Isn't digitalWrite(outPin, LOW); enough?

Thanks again in advance

The reality is that you always need a pullup or pulldown resistor to get a crisp HIGH or LOW input that won't float around at the phase of the moon. You should get used to this wiring pattern.

As it turns out, the ATmega package DOES have an internal resistor inside the package which you can request be put into use for this purpose. However, it's a pullup resistor, so you have to think of the button as "usually HIGH" and only when pushed, it goes LOW. Your software code has to be expecting this reversed logic.

To do that, you first set the pinMode() to make an INPUT, then you do a digitalWrite() to set the pin HIGH. This engages the internal pullup. There is no "internal pulldown." This is actually the third line in your setup() function above, that you have commented out.

"To do that, you first set the pinMode() to make an INPUT, then you do a digitalWrite() to set the pin HIGH. This engages the internal pullup. There is no "internal pulldown." This is actually the third line in your setup() function above, that you have commented out."

And in addition to reversing the logic in the software, the other end of the switch then has to wired to ground rather then +5vdc, pressing the switch will read as a low and releasing it will read as a high.

The ability of the AVR chip to have a programmable pull up does save the need to an external resistor but does require this logic inversion in the user's software routines.

And of course your not totally done unless you account for switch contact bounce. Real world mechanical switches rarely have a clean on/off phase, but rather a series of contact bounces when first being operated or released. This can cause problems with software routines that read the switch signal. Again this can be controlled with either external hardware bounce filtering or software debouncing.

The interface between software and real world hardware meet at the I/O pins and is never quite as simple as some might wish. :wink:

Lefty

Hehe yes - this makes a lot of sense.
I did a lab where we found some 'gross' old switch and looked at the bounce with an oscilloscope.
It was really exciting ;D

I am a junior computer engineering student - who has only used the altera UP2 board / Quartus II software. I think that is why I am so confused right now :-/

Everything I was doing was with the FPGA, and basic logic parts -- in software -> laying out wires / gates -- not even using any verilog or vhdl - although I just finished my VLSI (verilog/vhdl) course.

It is just horribly embarrassing that I am having this much trouble with the arduino.

I will experiment with everything you've said - and reverse the logic.
Thanks for the help :slight_smile: