Robojax

Why use Resistors with Push buttons and Switches with Arduino

دروس آردوینو به فارسی

Why should we use resistor with push button with Arduino?

This is Arduino Sketch for Tutorial video explaining why resistor is needed to be used with push button with Arduino.

We are using resistor with push button either from Arduino pin to the ground as shown in this picture.


Or using resistor from Arduino pin to VCC (5V) as shown in this picture.

Code example using resistor


 /*
 * This is Arduino Sketch for Tutorial video 
 * explaining why resistor is needed to be used with push button
 * with Arduino.
 * 
 * Written by Ahmad Shamshiri on July 17, 2018 at 23:11 in Ajax, Ontario, Canada
 * For Robojax.com
 * Watch instruciton video for this code: https://youtu.be/TTqoKcpIWkQ
 * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.
 */

void setup() {
  // Robojax.com push button tutorial
  pinMode(3,INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("Robojax Push button Tutorial");
  

}

void loop() {
  // Robojax.com push button tutorial
  int pushButton = digitalRead(3);
  if(pushButton == LOW)
  {
    Serial.println("Pin 3 LOW");
  }else{
    Serial.println("Pin 3 HIGH");
  }

delay(500);
}
   

Code example using INPUT_PULLUP without resistor


 /*
 * This is Arduino Sketch for Tutorial video 
 * explaining why resistor is needed to be used with push button
 * with Arduino to connect the pin to Ground (GND)
 * 
 * Written by Ahmad Shamshiri on July 18, 2018 at 17:36 in Ajax, Ontario, Canada
 * For Robojax.com
 * Watch instruciton video for this code: https://youtu.be/TTqoKcpIWkQ
 * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.
 */

void setup() {
  // Robojax.com push button tutorial
  pinMode(2,INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("Robojax Push button Tutorial");
  

}

void loop() {
  // Robojax.com push button tutorial
  int pushButton = digitalRead(2);
  if(pushButton == LOW)
  {
    Serial.println("Button is pushed.");
  }else{
    Serial.println("Button is NOT pushed.");    
  }

delay(500);
}