Introduction: Understanding the Pull-up/Pull-down Resistors With Arduino
With this little test I hope you'll understand why the pull-up (and pull-down) resistors are needed in digital circuits like in Arduino.
With a pull-up resistor and with the button unpressed you make a logic state ON and with the button pressed you make a logic OFF.
With a pull - down resistor and a pressed button you make an ON logic state and OFF logic state when its unpressed.
Make the above pull-up circuit and try the code. You'll see the LED flickering or less bright.
Pressing the button and you see now the LED turned normaly on (fully bright). Turning off the button and the LED its flickering again.
/*Pull-up resistor test*/
int buttonPin = 3;
int Led = 10;
void setup() {
pinMode(buttonPin,INPUT);
pinMode(Led,OUTPUT);
Serial.begin(9600);
}
void loop() {
int buttonState = digitalRead(buttonPin); //read the state of the button input
if (buttonState == LOW) { // if the button is pressed it is low state
digitalWrite(Led,HIGH); //see flickering led or less bright
} else {
digitalWrite(Led,LOW);
Serial.println(buttonState);
}
}
Step 1: With Out the Pull-up Resistor
So why was the LED flickering? Simply the logic static of the open switch is floating so it could be either a '0' or a '1". When the button is pressed this produces a clear logic state of LOW since its grounded.
Check in the Serial monitor to see this as well The will be a serial of unstable '0' and '1' caused by the floating open situation of the switch.
Step 2: With the Pull-up Resistor
To prevent the unknown state a pull-up resistor will ensure the state on the pin is low.
Add a resistor of 4.7k* (check in step 4 the calculation of the resistor) to the circuit, and try the below code
See the led working properly with the two states LOW and HIGH.
Check in the serial monitor, when you press the button you'll get a logic LOW
and without pressing a logic HIGH
/* with the pull-up resistor*/
int buttonPin = 3;
int Led = 10;
void setup() {
pinMode(buttonPin,INPUT);
pinMode(Led,OUTPUT);
Serial.begin(9600);
}
void loop() {
int buttonState = digitalRead(buttonPin); //read the state of the button input
if (buttonState == LOW) { //pressing the button will produce a LOW state 0V
digitalWrite(Led,HIGH); //the led with turn on
Serial.println(buttonState);
} else{
digitalWrite(Led,LOW); //the led with turn off
} Serial.println(buttonState); //check in the serial monitor
}
Step 3: A Schematic Explaining the LOW - HIGH State With and Without the Pull-up Resistor
On the left you see the button the moment its beeing pressed. This gives a digital signal of the LOW state.
On the right the voltage across the pull-up resistor with the button unpressed is 5Vdc providing the digital signal of the HIGH state.
Step 4: What Should Be the Value of the Resistor?
So lets assume you want to limit the current to 1mA.
Since Vcc = 5V, using Ohms law: R=U/I => R = 5000mV/1mA => R= 5000Ω = 5k
so a resistor of 4.7k will be fine
Mostly known to be used is a 10kΩ resistor, this will need only 0.5mΑ.
Step 5: Using the Pull - Down Resistor
/*simular and the pull - down resistor */
int button = 2;
int led = 10;
int buttonState = 0;
void setup() {
pinMode(led,OUTPUT);
pinMode(button,INPUT);
Serial.begin(9600);
}
void loop() {
buttonState = digitalRead(button);
if (buttonState == HIGH) {
digitalWrite(led, HIGH);
} else {
digitalWrite(led, LOW);
}
Serial.println(buttonState);
}
Step 6: Working With Out a Pull-up or Pull Down Resistor
Check for this in another post I made here
https://www.instructables.com/id/Working-Without-a-Pull-up-Pull-down-Resistor-With-/
11 Comments
6 years ago
Hi
I did this but the led doesn't flicker. I have connected it the way you show in the diagram. Any ideas?
Reply 6 years ago
Yes the LED is less bright. Pressing it gets a HIGH state. Try the second step with the Serial Monitor on you can see it
Reply 3 months ago
You are right, the code and the diagram do not say the same things, if you want to keep using the code, plug it into the digital 10 port
Reply 6 years ago
I meant to say, It stays on even without pressing the button.
Reply 1 year ago
I know you posted this 5 years ago, but I would like to try and figure out why it didn't work for you and you can tell me if I read the code correctly and if I figured out the problem.
At the top of the code it has
int led = 10;
There is nothing connected to pin 10 on the Arduino board. So instead it should read this instead
int led = 12;
Since the led is connected to pin 12 on the picture above.
Am I correct with this, or did I just totally not understand what the OP was trying to explain.?
2 years ago
This can be a very misleading tutorial, as one would expect you to talk about the integrated pull-up and pull-down resistors in the arduino. And how to use them with the code
Reply 9 months ago
You can not program an Arduino if you can not even think logically.
Internal pull up/down is referred to as "Internal pull up/down".
If he mentions pull up resistors then its not internal. duh. Thats logic
Reply 2 years ago
Yes you are probably right!
Reply 2 years ago
I didn't read the all of this article, but at least the part of illustration can mislead everyone into understanding.
In pull-up, pressing button mean add 0v to both resister and arduino.
In pull-down, pressing button mean add 5v to both resister and arduino.
Question 3 years ago
Where is PULLUP and PULLDOWN used in your code?
Answer 9 months ago
Its not in the code.
Internal pull up/down is referred to as "Internal pull up/down".
If he mentions pull up resistors then its not internal. duh