Boris LeonovArthur BrownSam Kristoff
Published © MIT

Arduino Button Troubleshooting

Using an Arduino to toggle something like an LED light is simple in theory, but buttons don't always behave the way we think.

EasyShowcase (no instructions)1 hour579
Arduino Button Troubleshooting

Things used in this project

Hardware components

Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×1
OpenScope MZ
Digilent OpenScope MZ
×1
General Purpose Transistor NPN
General Purpose Transistor NPN
Any general-purpose NPN Transistor will work fine.
×1
5 mm LED: Red
5 mm LED: Red
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1
Resistor 100 ohm
Resistor 100 ohm
×1
Resistor 10k ohm
Resistor 10k ohm
×1

Software apps and online services

Digilent WaveForms Live
Arduino IDE
Arduino IDE

Story

Read more

Code

Button Toggle Debounced

Arduino
Arduino code to perform a toggle operation when it sees a button press, with the added feature of ignoring switching noise.
//set up I/O pins
//pin 2 will connect to the base of the NPN transistor and trigger the LED on/off
int LED = 2;

//pin 4 will be used to monitor the button
int SWTCH = 4;

//a boolean to keep track of the current state of the LED
bool LED_on;


void setup() {
  //set the respective pins as inputs and outputs
  pinMode(LED, OUTPUT);
  pinMode(SWTCH, INPUT);

  //initially turn on the LED
  digitalWrite(LED, HIGH);
  LED_on = true;
  
}

void loop() {
  //monitor the switch pin for a drop in voltage
  if(digitalRead(SWTCH) == 0){
    //wait for the signal to stabilize
    delay(1);

    //make sure the inital drop wasn't random noise
    if(digitalRead(SWTCH) == 0){
      
      //toggle the boolean...
      LED_on = !LED_on;

      //...which will toggle the LED
      if(LED_on){
        digitalWrite(LED, HIGH);
      }
      else{
        digitalWrite(LED, LOW);
      }

      //wait for the signal to go high again before doing anything else
      while(digitalRead(SWTCH) == 0){
      
      }

      //wait for the signal to stabilize
      delay(1);
    }
  }
}

Button Toggle

Arduino
Arduino code that was initially used to perform a toggle operation when there is a button press. The toggling is very unreliable because of signal noise. Used as an example of where the code started.
//set up I/O pins
//pin 2 will connect to the base of the NPN transistor and trigger the LED on/off
int LED = 2;

//pin 4 will be used to monitor the button
int SWTCH = 4;

//a boolean to keep track of the current state of the LED
bool LED_on;


void setup() {
  //set the respective pins as inputs and outputs
  pinMode(LED, OUTPUT);
  pinMode(SWTCH, INPUT);

  //initially turn on the LED
  digitalWrite(LED, HIGH);
  LED_on = true;
  
}

void loop() {
  //monitor the switch pin for a drop in voltage
  if(digitalRead(SWT) == 0){
    
    //toggle the boolean...
    LED_on = !LED_on;

    //...which will toggle the LED
    if(LED_on){
      digitalWrite(LED, HIGH);
    }
    else{
      digitalWrite(LED, LOW);
    }
}

Credits

Boris Leonov

Boris Leonov

10 projects • 15 followers
Electrical engineering student in the Seattle area. Electrical and mechanical DIY enthusiast. Fixer of things, large and small.
Arthur Brown

Arthur Brown

14 projects • 15 followers
Applications engineer and digital logic geek
Sam Kristoff

Sam Kristoff

35 projects • 30 followers
Director of Engineering at Digilent.

Comments

Add projectSign up / Login