Wall's Corners Wall's Corners Author
Title: Raspberry Pi GPIO Inputs in Python
Author: Wall's Corners
Rating 5 of 5 Des:
1 Overview I left off having hopefully shown just how easy it is to get turn on an LED on the Raspberry Pi. First up I’m going to show a ...

1 Overview

I left off having hopefully shown just how easy it is to get turn on an on the Pi. First up I’m going to show a quick Blinky example, for now I’m going to stick with just using in interactive mode form the terminal, but we may get into script files after I show the basic’s of inputs.

 

2 Blink on the Pi

Straight in with the the code then:

$ sudo python
>>> from time import sleep
>>> import RPi.GPIO as GPIO
>>> GPIO.setup(18, GPIO.OUT)
>>> while 1:
...     GPIO.output(18, False)
...     sleep(1)
...     GPIO.output(18, True)
...     sleep(1)
...

 

So a little more to that than before, notice the prompt changed after the

while 1:

to

...

After this we need to indent the code with a tab for it to be considered part of the loop. Hit enter twice after the

sleep(1)

and we now have a blinking red :)

Now to stop the loop and get back to the prompt use Ctrl-C and to get out of python and back to the bash prompt you can use Ctrl-D. Below you can see I just used Ctrl-C to get back to the python prompt

 

3 Inputs

Time to play with the push buttons, here the code again, if you are at the python prompt from above then just ignore the first two lines:

 $ sudo python
>>> import RPi.GPIO as GPIO
>>> GPIO.setup(11, GPIO.IN)
>>> GPIO.input(11)

 

Now we should get back a

False

as below

Try holding down the button and doing that last line again

>>> GPIO.input(11)

 

This time you should get back

True

Now lets see if we can’t combine inputs and outputs

 

4 Combining Inputs and Outputs

Ill give the full code example again:

$ sudo pyton
>>> import RPi.GPIO as GPIO
>>> GPIO.setup(18, GPIO.OUT)
>>> GPIO.setup(11, GPIO.IN)
>>> while 1:
...     if GPIO.input(11):
...         GPIO.output(18, False)
...     else:
...         GPIO.output(18, True)
...

 


Now have a simple push button turning on an LED

 

5 Something a little more complex

So it’s time to move onto something a little more complex and for this I’m going to start using a IDE called Geany, its already available on the Pi’s and has been used in some great youtube videos by Liam, check out his channel RaspberryPiTutorials

There are however a few limits to using geany, first we need to install xterm (at least on the debian image)

$ sudo apt-get install xterm

 

The second is a little more tricky, using RPi.GPIO needs root permission, hence we have been using sudo python up till now, luckily we can ask geany todo the same. Open geany form the menu, once it loads, we want a new file from the main.py template, try the little down arrow at the side of the ‘New’ button.
This should give us an untitled.py file pre filled with a load of text, we will go back to that in a bit. Next click on the ‘Build’ menu and then ‘Set Build Commands’. In the ‘Execute’ box near the bottom add the word “sudo” in front of the ‘python “%f” and hit ‘OK’

Now back to that file in the editor, lets delete everything but the first line #!/usr/bin/env python and type in some code, lets try out our button to led example from above:

Now hit the ‘Execute’ button (3 little gears) and a black window pops up in which the python script will run and we have our push button LED again. But I wired up 4 LED’s and two Buttons so lets get them all working together:

#!/usr/bin/env python
from time import sleep
# get the GPIO Library
import RPi.GPIO as GPIO
 
# setup some names references to the LED's and buttons
# red     = pin 18
# yellow  = pin 16
# green   = pin 15
# blue    = pin 13
 
# using an list to hold the led numbers
leds = [13, 15, 16, 18]
# the input buttons
up = 12
down = 11
 
# setup the pins as output and input as needed
# looping over the list 
for n in leds:
    GPIO.setup(n, GPIO.OUT)
 
GPIO.setup(up, GPIO.IN)
GPIO.setup(down, GPIO.IN)
# turn off all but the blue LED
GPIO.output(leds[3], True)
GPIO.output(leds[2], True)
GPIO.output(leds[1], True)
GPIO.output(leds[0], False)
 
#state trackers
level = 0
oldlevel = level
 
while 1:
    if GPIO.input(up):
        #up button pressed
        if level < 3:
            level += 1
    elif GPIO.input(down):
        #down button pressed
        if level > 0:
            level -= 1
    if oldlevel != level:
        # turn off last led and turn on next one
        GPIO.output(leds[oldlevel], True)
        GPIO.output(leds[level], False)
        # update state tracker
        oldlevel = level
        # sleep for a bit so button press is counted only once
        sleep(1)

 

Try it out, file is here, if all goes well the blue LED will light up and clicking the up button will change it to the green, and so on.

Another long post but should give an idea of how easy it can be to use the GPIO’s on the Raspberry Pi with a little Python.
Next I’ll be looking at either the little Charlieplex LED display I built on another slice or it will be about using the XRF and URF radio modules with pyserial. Here’s some pics 😉

 

Share This:

View more at: http://bit.ly/1XReQFr

About Author

Advertisement

Post a Comment

 
Top