RPi Tutorial EGHS:Switch Input

From eLinux.org
Revision as of 10:13, 13 March 2012 by Meltwater (Talk | contribs)

Jump to: navigation, search

Back to the Hub, or the Tutorials page.

GPIO Hardware & Software Tutorials:

This page is part of a work in progress.


While most of these circuits may interface directly to the RPi, the use of a buffered interface (such as the one supplied by the Gertboard) is recommended which will help protect against damage. Alternatively, experiment with one of the Alternative Test Platforms.

Extreme caution should be exercised when interfacing hardware at a low level, you may damage your RPi, your equipment and potentially yourself and others. Doing so is at your own risk!


Until RPi devices are available, I can not confirm this will work on a real RPi.
For now, I shall be using the TI LaunchPad (see  Alternative Test Platforms
for details) to test the hardware on (as it is cheap and the logic levels similar).

The Hardware


Work in progess...

The GPIO pin on the RPi when defined as an Input allows the state of the pin to be read as with HIGH or LOW. This allows us to use a simple switch to set the state of the pin and the software can read this and respond to the change (i.e. change the audio volume output or light an LED wired to another pin).

For additional theory see [1]

Logic Levels

For the GPIO pin to determine if an input level is HIGH or LOW it has to detect that the voltage on the pin is above or below a set level, you should aim to set the pin to 10% of the logic voltage (HIGH for the RPi this is 3.3V - so 2.97V to 3.3V, LOW is 0V to 0.33V).

If the pin voltage is somewhere between 2.97V and 0.33V then the system may not reliably determine if a HIGH or LOW value is set (particularly as you get closer to 1.65V (halfway)).

If you drive voltages over 3.3V or negative voltages then you will risk damaging the internal GPIO circuits and perhaps even killing the RPi (which is why using extra components to protect and buffer inputs is recommended).


When a GPIO pin is disconnected and set as an input, the voltage is considered floating since there is no defined voltage level being applied to the pin. Therefore a good input circuit needs to apply either a HIGH voltage or a LOW voltage.

No Pull-Up/Pull-Down

The top circuit with Switch 1 (Double-poll switch) is fine, however, when using other types of switches, such as Switch 2 (i.e. Single-poll switch) you will create a short-circuit when the switch is pressed!

However, by simply using a resistor in-between the short-circuit can be avoided.

This is covered in a lot more detail here (although note the logic level here is 5V!) [2].

Output Protection



The Software

While the RPi is not available, I can only confirm the TI LaunchPad code works for me.

TI LaunchPad

The specifics and background behind the code is detailed very well on the following sites:

MSP430 LaunchPad PushButton

MSP430 LaunchPad Interrupt vs Polling

Sample test code for switch input (tested on TI MSP430G2553 device).





  1. Introduction To Embedded Programming - GPIO Input
  2. http://www.ladyada.net/learn/arduino/lesson5.html Arduino inputs