Creating an Arduino toggle switch can be done using a simple push
button. You don't need to buy an actual toggle switch (although it would
provide the same operation).
When you use a standard push button as an input to a microcontroller
pin you can read its state simply by using the digitalRead function.
From there you can take any action you want as a result of the key
press.
The difference between a toggle switch and a push button switch is
that the toggle switch can remain either high or low, whereas a push
button input is momentarily connected when pushed. A spring pushes
the button back when you release it.
So in effect, a physical toggle switch remembers its position (there's no spring pushing it back to an inactive state).
This key difference (memory) is what allows you to create a toggle
switch in software i.e. by adding memory to a push button switch, you can
create an Arduino toggle switch.
All you need is a variable associated with the push button input which you invert every time the button is pushed.
The following sketch uses digitalRead and toggles an LED output using
the method described above. So why have I said it might work? Try it
first and see if you see the problem.
When you push the button sometimes it will toggle and sometimes it won't.
If you don't see the problem then you got lucky - trust me; if you
don't correct it you'll see it later when you are not expecting it! More
than likely you'll have the LED go off and on randomly; sometimes it
will work correctly but at other times the LED will stay on or stay off!
#define BUTTON_PIN 2
#define LED_PIN LED_BUILTIN
void setup(void) {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN,OUTPUT);
}
void loop(void) {
static byte toggle_sw_memmory=0;
// Check for keypress
if ( !digitalRead(BUTTON_PIN) ) { // Pulled up so zero = hit.
toggle_sw_memmory = !toggle_sw_memmory;
if (toggle_sw_memmory)
digitalWrite(LED_PIN,HIGH);
else
digitalWrite(LED_PIN,LOW);
while(!digitalRead(BUTTON_PIN)); // wait for low
}
}
The problem is that push button switches have an internal spring that
does not settle immediately. So when you push it, it takes time to settle
down and bounces up and down, producing lots of on-off transitions (even if
you think you have pressed it with no bounce that you can feel!).
The solution to make the Arduino toggle switch work is...
...Debouncing.
There's are many ways to debounce a switch which are described in the easy switch debounce page.
The one chosen below is the simplest but uses the delay function so it
wastes processor time to perfborm the deounce. Better/different
solutions are here.
The result is that for every push button action the LED toggles as you would expect.
#define BUTTON_PIN 2
#define LED_PIN LED_BUILTIN
void setup(void) {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN,OUTPUT);
}
void loop(void) {
static byte toggle_sw_memmory=0;
// Check for keypress
if ( !digitalRead(BUTTON_PIN) ) { // Pulled up so zero = pushed.
delay(100);
if ( !digitalRead(BUTTON_PIN) ) { // if it is still pushed after a delay.
toggle_sw_memmory = !toggle_sw_memmory;
if (toggle_sw_memmory)
digitalWrite(LED_PIN,HIGH);
else
digitalWrite(LED_PIN,LOW);
}
while(!digitalRead(BUTTON_PIN)); // wait for low
}
}
Always use some form of button debouncing to ensure program reliability.
For an example of a really tricky button bounce problem (solved in the page) is the rotary encoder which you don't really think would have any bounce problem at all!
Comments
Have your say about what you just read! Leave me a comment in the box below.
Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.