Project 2: purpose of 10k resistor?

I don't understand the book explanation of this 10k resistor connecting the switch and ground.

This is my current code:

int switchState=0;
void setup(){
  pinMode(2,INPUT); //switch
  pinMode(3,OUTPUT); //green led
  pinMode(4,OUTPUT); //red led
  pinMode(5,OUTPUT); //red led
}
void loop(){
  switchState=digitalRead(2);
  //voltage on pin 2 =1
  //no voltage on pin 2 =0;
  
  if(switchState==0){
    //no voltage on pin 2
    digitalWrite(3,HIGH);
    digitalWrite(4,LOW);
    digitalWrite(5,LOW);
  }
  else{
    digitalWrite(3,LOW);
    digitalWrite(4,HIGH);
    digitalWrite(5,HIGH);
  }
}

With this 10k resistor attached, the device works as expected. Only green is lit when switch is not pressed, and only the two reds are lit when switch is pressed.
However, without the 10k resistor, all three LEDS are lit when the switch is not pressed. When the switch is pressed, the green stops being lit, but the two other red LEDS stay lit, though they become brighter.

So my question is... when the 10k is removed, why are the 3 LEDS lit when the switch is not pressed?

Without the 10K, the voltage on the pin can "float" (drift around, even change from your hand being nearby, acting like a little antenna) and the input can be unpredictable.
With the 10K, the input will always be known when the switch is open.

Can you explain this a bit more? Why does the voltage float, and how does adding the resistor result in fixed input?

My flawed understanding is:

Without the resistor, the circuit across the switch should be open when the switch isn't pressed, so no current should get to the Arduino pin (#2), so it should read as "LOW".

What am I missing here?

Thanks!

Day one with my kit, and it's great fun so far :slight_smile:

The '328P input only needs 1uA of current to change from high to low, or low to high.
1uA is next to nothing - a hand waving around can radiate enough energy for that 1uA.
The 10k resistor provides up to 500uA to overcome any radiated energy from the environment.

OK, so we need the resistor here to require substantial voltage before the 2 pin will register that the switch is closed.

Why didn't we need this for our earlier use of the push button switch? Is it because the amount that the voltage can float is too low to turn on the LED? So even when the button is not pressed, a tiny, variable voltage is still applied to the lights?

Thanks!

Earlier version?
All the Arduino's going back to at least the Duemilanove have this pullup.
It's recommended by Atmel application note AVR042.

Sorry, I meant in the first project in the book, where we use the push button without the 10k resistor to control a LED.

I've personally [edit:] Never used an external resistor with a push button/switch.
I use the internal pullup, and wire the switch to connect to Gnd when pressed.
Then I just look for a LOW to see if it's pressed. A simple matter to think of active as LOW vs HIGH:

void loop(){
if (digitalRead(pinX) == LOW){ // button pressed?
digitalWrite (ledPin, LOW); // assumes LED is wire with Anode to +5, pin pulls cathode low to turn it on (with series current limit resistor)
}
else {
digitalWrite (ledPin, HIGH); // drive cathode high to turn LED off
}

}

Or with LED wired the other way around, pinX to Anode, cathode to resistor to Gnd:

void loop(){
if (digitalRead(pinX) == LOW){ // button pressed?
digitalWrite (ledPin, HIGH); // drive anode HIGH to turn LED on}
else {
digitalWrite (ledPin, LOW); // drive anode LOW to turn LED off
}

}

Positive logic, negative logic, active high, active low, call it whatever you want, just realize it's a simple thing to write the code to act one way or the other.

So basically you are saying the resistor ensures that no current is flowing to the board by providing a way for any current to be used going to ground?

ths both incredibly!!! Just couldn't find out what was happening. Maybe time for a review of the starterkit...

plantarum:
Sorry, I meant in the first project in the book, where we use the push button without the 10k resistor to control a LED.

There is no Arduino pin involved in project 1, so there is no need to have a 10k resistance !

On top, without a 10k resistance in project 2, you would create a short circuit sending the 5V output directly on the 0V entry and BOOM !