Monday 19 March 2018

Pull up and pull down resistor

Pull-up and pull-down resistor

Arduino digital port  acts like an antenna. It can pick up electrical noise and fluctuate between logic HIGH and LOW. It is important that the digital ports are tied or fixed  to a particular logic value to avoid the system from behaving unpredictably.

There are two options: 
a. Pull the port up to 5V when the port is not used by using a pull-up resistor ( normally 4.7k or 10 k resistor) or

b. Pull the port down to 0V when the port is not used by using a pull-down resistor ( normally 4.7k or 10 k resistor).

Normally the pull-up or pull-down is used when the ports are connected to digital input such as pushbuttons or switches. Lets look at a pull-up resistor case. 
The pushbutton is connected to port 5 and LED is connected to port 2. When the pushbutton is not pressed, port 5 will be pulled up to 5V or logic 1. The action of pressing the button will cause the port to become low or 0V.

Pull-up resistor
Arduino code to turn on the LED when the button is pressed.

int led = 2;
int buttonPin =5;
int buttonState = 0;

void setup() {
// put your setup code here, to run once:
pinMode(led,OUTPUT);
pinMode(buttonPin,INPUT);
}

void loop() {
// put your main code here, to run repeatedly:
buttonState=digitalRead(buttonPin);

if(buttonState == LOW) // button pressed
  digitalWrite(led,HIGH);
else
  digitalWrite(led,LOW);
}


For pull-down resistor, the following modification is needed.


Pull-down resistor


Arduino code to turn on the LED when the button is pressed.

int led = 2;
int buttonPin =5;
int buttonState = 0;

void setup() {
  // put your setup code here, to run once:
pinMode(led,OUTPUT);
pinMode(buttonPin,INPUT);
}

void loop() {
 // put your main code here, to run repeatedly:
buttonState=digitalRead(buttonPin);

if( buttonState == HIGH) // button pressed
  digitalWrite(led,HIGH);
else
  digitalWrite(led,LOW);
}

Notice the difference in the condition when the button is pressed for both cases.



No comments:

Post a Comment