Warning: call_user_func_array() expects parameter 1 to be a valid callback, no array or string given in /home3/validus/public_html/wp-includes/class-wp-hook.php on line 298

03 – How to debounce a switch with PICAXE?

What is switch bounce? When a mechanical switch is pressed the switch contacts makes contact but it does not stay on it, it oscillates for a couple times between on and off before it settles down in the on state. This happens in a couple of micro seconds but microprocessors is fast enough to register the multiple make and break pulses. This results in multiple presses being registered instead of just one. 

The process to stop switch bounce is known as debouncing the switch. How can one debounce a switch? There are a couple of ways to do it:

  • One can debounce the switch with hardware such as a flip-flop
  • Connecting a small capacitor over the switch contacts to dampen the oscillation
  • Within software by delaying the re-evaluation of the switch or by following a code path that is longer than the debounce time required for the switch.


The best option to debounce a switch in PICAXE is to use an interrupt on the switch input pin and put the delay code in the interrupt sub procedure. In this example the debouncing of the switch is done outside the main program in the interrupt sub procedure. In the main program the “pressed” variable acts as the switch and you can use it as a switch in your code without worrying about debouncing it in your main code. 

 The school experimenter board circuit diagram is used in this example.

You can download the program file, Debounce a switch in PICAXE by right clicking on it and selecting ‘save link as’ from the popup menu.