Go Down

Topic: Project 2: purpose of 10k resistor? (Read 1 time) previous topic - next topic

yosimba2000

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

This is my current code:
Code: [Select]
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?

CrossRoads

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.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

plantarum

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  :)

CrossRoads

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.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

plantarum

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!

CrossRoads

Earlier version?
All the Arduino's going back to at least the Duemilanove have this pullup.
It's recommended by Atmel application note AVR042.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

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.

CrossRoads

#7
Sep 15, 2014, 03:41 pm Last Edit: Sep 16, 2014, 01:18 am by CrossRoads Reason: 1
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:
Code: [Select]

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:
Code: [Select]

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.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Go Up