Monday, August 27, 2012

Raspberry PI - Reading a GPIO Pin


The next thing that I wanted to do was READ a GPIO Pin.

This article assume that you have installed the software mentioned in
"Raspberry PI – Writing to GPIO Pins".

I connected the following circuit using jumper pins and a proto board (BE careful and do this with the power off, triple check before turning it on!!!!).



You cannot just hook the switch to a logic input, you have to actively pull it high and low (in most cases, won't get into that now). The 10K resistor acts as a "pullup" to pull pin 11 high when the switch is open, and draws only microamps of leakage current (it any).

When the switch is closed, pin 11 is pulled low to ground, drawing only about 0.3 milliamps from the rPI.
 
After checking the circuit, power up the rPI, log in and nano up the following program as "switch.py":

#!/usr/bin/python
#
import RPi.GPIO as GPIO
#
# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)
#
GPIO.setup(11, GPIO.IN)
#
count = 1
while (count < 100):
        input_value = GPIO.input(11)
        print input_value
print ("End")


Now run the program

     $ sudo python swith.py

You will see "True" scrolling to the screen when the switch is open, and "False" when it is closed.

By the way, the schematic was edited with a "free as in beer" schematic editor from www.expresspcb.com


Reference



Eric Pierce VA3EP - See the Disclaimer in the Introduction

© Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", 1952-2099. Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", with appropriate and specific direction to the original content.

No comments:

Post a Comment