May 062017
 

Using a pushbutton that completes the circuit only while being pressed can turn an LED on while the button is being held down. But what if you wanted to press it once and have the LED light up and then press it again and turn off the LED? Well you can do this. It is done with an Arduino using State Change Detection (also known as edge detection). This tutorial will teach you how to do it and you will see the Arduino send a message to the Serial Monitor with that information and it will display the count of the state changes that turn on and off the LED. This can also be adapted to use with a relay to turn on and off an appliance such as a stereo amplifier, fan, or floor lamp. And with even further development  you can do this using an infrared remote control. So let’s learn more!

To make this project you will need:

  • An Arduino Uno, Nano, Pro-Mini etc.
  • LED
  • Momentary button or switch
  • 10k ohm resistor
  • Jumper wires
  • Breadboard

You Can Download The Arduino Sketch Here

And here is how you wire it up.

image developed using Fritzing. For more circuit examples, see the Fritzing project page @ http://fritzing.org/projects/

 

 

 

 

 

 

Connect three wires to the board. The first goes from one leg of the pushbutton through a pull-down resistor (here 10k ohm) to ground. The second goes from the corresponding leg of the pushbutton to the 5 volt supply. The third connects to a digital I/O pin (here pin 2) which reads the button’s state.

When the pushbutton is open (unpressed) there is no connection between the two legs of the pushbutton, so the pin is connected to ground (through the pull-down resistor) and we read a LOW. When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to voltage, so that we read a HIGH. (The pin is still connected to ground, but the resistor resists the flow of current, so the path of least resistance is to +5V.)

If you disconnect the digital I/O pin from everything, the LED may blink erratically. This is because the input is “floating” – that is, not connected to either voltage or ground. It will more or less randomly return either HIGH or LOW. That’s why you need a pull-down resistor in the circuit.

click the image to enlarge

 

 

 

 

 

 

 

 

 

/*
State change detection (edge detection)
Often, you don’t need to know the state of a digital input all the time,
but you just need to know when the input changes from one state to another.
For example, you want to know when a button goes from OFF to ON. This is called
state change detection, or edge detection.
This example shows how to detect when a button or button changes from off to on
and on to off.
The circuit:
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
* LED attached from pin 13 to ground (or use the built-in LED on
most Arduino boards)
created 27 Sep 2005
modified 30 Aug 2011
by Tom Igoe
The original sketch modified May 6, 2017 Volthuas Electronic Laboratory Austin, TX
http://volthauslab.com/led-on-and-off-one-button
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/ButtonStateChange
*/
// this constant won’t change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then when the button
// is pressed the LED will change from off to on:
buttonPushCounter++;
Serial.println(“on”);
Serial.print(“number of button pushes: “);
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then when the button
// is pressed the LED will change from on to off:
Serial.println(“off”);
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
// turns on or off the LED every time the button is pushed by
// checking the button push counter.
if (buttonPushCounter % 2 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}