Tuesday, August 28, 2012

Raspberry PI - Reading and Writing to GPIO Pins

Next step is to combine the GPIO Input and Output in to one program.

With the power to the rPI off, I used another jumper to tie header pin 18 to the input of a high impedance logic probe.

Then I powered up, logged in and nano's up the following program "switch_led.py"

import time
import RPi.GPIO as GPIO
# to use Raspberry Pi board pin numbers
GPIO.setup(18, GPIO.OUT)
GPIO.setup(11, GPIO.IN)
count = 1
while (count < 100):
        input_value = GPIO.input(11)
        GPIO.output(18, input_value)
        print input_value
print ("End")

Then I ran it with

     $ sudo python switch_led.py

Now when the switch is turned on  and off, the led goes on and off. 

Congratulations, you now have a $35 programmable light switch. But once you can get digital data in and out of a program , the sky is the limit!

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