Arduino Toggle Switch

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.

Toggle and Push Button Difference

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).

Software Arduino Toggle switch

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.

A Program that Might Work!

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!

Sketch: No debounce

#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
   }

}

[File: toggle_switch_no_debounce.ino]

The Problem

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!).

Note: Some push buttons are better than others due to physical construction.
Push buttons come in different sizes and a small button will have different bounce characteristics to a larger one.

The solution

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 perform the debounce. Better/different solutions are here.

The result is that for every push button action the LED toggles as you would expect.

Warning: This code uses a simple debounce solution that wastes processor time.
Note: You can find other debounce methods here.

Sketch: With simple debounce

#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
   }

}

[File: toggle_switch_with_debounce.ino]

Conclusion

Always use some form of button debouncing to ensure program reliability.

Warning: Lots of examples on the web ignore button debouncing.
Lots of web examples do not even mention debouncing; this is because the specific button that they use to test the code worked at for them. When you try it it may or may not work for you.

TIP: Always use button debouncing then you won't have a problem later.

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!






New! Comments

Have your say about what you just read! Leave me a comment in the box below.



Privacy Policy | Contact | About Me

Site Map | Terms of Use