RPi Tutorial EGHS:Switch Input

From eLinux.org
Revision as of 03:54, 13 March 2012 by Meltwater (talk | contribs) (PullUp/PullDown)
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.

Warnings

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!

Aims

Note:
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

Theory

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

PullUp/PullDown

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!






Example Pull-Up and Pull-Down Circuit


However, by simply using a resistor in-between the short-circuit can be avoided. The value of the resistor is determined by the leakage current of the GPIO pin (the current used by the circuit to read the pin) (although this is not specified for the RPi, it is expected to be in the range of 1uA) and the amount of voltage drop it creates as a result.

 If we set R1 to 10kohms, and max drop from Vcc is 10% (0.33V).
 Max leakage current = Vdrop/R1
                     = 0.33/10000
                     = 33uA

By picking a large resistor it ensures less current is drawn by the circuit, when the switch is active (this is why a large value of 10Kohms is usually used).

Current drawn = Vcc/R1
              = 3.3/10000
              = 0.33mA

The above circuit shows Switch 1 which when pressed will provide a LOW signal to the GPIO pin (otherwise the resistor pulls the circuit UP HIGH), and Switch 2 which when pressed will provide a HIGH signal to the GPIO pin (otherwise the resistor pulls the circuit DOWN LOW).


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


One additional thing to note is that the RPi GPIO has internal Pull-Up and Pull-Down resistors which can be enabled through software registers[3].

This means that you can avoid the extra circuit above and simply place a switching between Vcc or GND, but you have to set the Pull-Up/Pull-Down registers for it to function correctly. To keep the software simple and to avoid any damage, it is recommended you start with using external Pull-Ups and Pull-Downs until you are confident setting these registers correctly.

Output Protection

http://www.ladyada.net/learn/arduino/lesson5.html

Circuit

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

Circuit

Code:

RPi

References