Saturday, March 21, 2009

Debouncing Switches

Well, now I'm properly under way with the project :-)

The first job was to get the input sorted. The plan is to use a bank of four push switches which will align with hot-key style control's on the display. The first job here of course is to connect the buttons to the Arduino and recognising in software when a button is pressed.

The circuit for this is really quite simple. Firstly, we take the power supply from the Arduino via the power connector (J1) simply using the +5V (pin3) and Ground (pin4). The display (CLCD420-B/J4) is connected via the I2C bus provided via the Arduino analogue input (J3); The SCL signal is provided by analogue pin 5 and SDA is analogue pin 4. Both SCL and SDA lines are connected to +5V via 4K7 ohm pullup resistors.

The switches (J5) share a common +5V supply, then each switch is fed to the Arduino Digital (J2) pins 9 through 12 and to ground via 220K Ohm resistors. When a switch is open, the resistor is effectively disconnected and the Arduino pin is held low. Whent the switch is closed, the resistor ensures that the input pin is held high, while also restricting the current flow.

So the software should also be quite simple... only there's a problem... when you press a push button, the electrical connection is not simply on/off, but rather toggles rapidly during the on/off transitions. To get round this we need to wait for the input to settle at a constant level. So rather than saying the button is pressed the instant the input goes high, we say the button is pressed once the input has remained constantly high for a given time. This process is called "Debouncing".

In the test program, I simply read the button presses and toggle a state value with each button press and output the state (on/off) to the display. The debounce time used is 50ms, that is, the button must be held for 50ms in order to count as a press and toggle the respective state. I created a class for handling the debounce function, so the code need only instatiate the class for each required button/input pin; the constructor handles the pin initialisation, so no other initialisation is required in the setup() function. Software then only needs to call the respective Read() function for each pin.


  1. ...nice work here, I like that. Also because I am working on the same but do not such a nice documentation as you do.
    You might be interested in the following:

    Wünsche Dir viel Erfolg,
    Grüsse aus Zürich, Hans.

  2. Why don't you use a deboucing circuit, it is quite simple and saves you some processing time.

  3. I did consider a debouncing circuit, but as the switches are not time-critical to the camera triggering (only used for control/setup), there is no issue with the processing time cost of debouncing.