Wednesday 6 January 2016

Raspberry Pi basics - Controlling I/O using Python and GPIO

The Raspberry PI has a number of input and output pins called GPIO. A few things you should be careful about is the GPIO pins are 3.3 volt not 5 volt. If you connect a 5 volt device then it is likely to damage the Raspberry PI. In this section I will show you some basic inputs and outputs.

Things that you will need to do this project
  • 1 Bread board
  • Jumper Wires for a bread board
  • 3 LED's
  • 3 Tac switches
  • 3 500Ω Resistors
  • 3 50KΩ Resistors
Connect up LED's and tac switches to the Raspberry PI as below. There are 500Ω resistors to reduce the voltage to the LED's and 50KΩ pull down resistors to stop false readings on the inputs when they are not being connected to +3.3 volts via the switches. Orange wires are 3.3 volts (not red as it's used for +5 volts), black are ground, yellow are outputs and purple are inputs.






















































Below is First we write Initial code to define connections parameters and modules to import. Import the GPIO function and time for delays used later. Set each pin for input or output mode. I have used variables for the pins, this is good practice because it makes it easy to change the pin numbers if required.


import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
LED1 = 14            #LED Output 1
LED2 = 15            #LED Output 2
LED3 = 18            #LED Output 3
SWI1 = 23            #TAC Switch Input 1
SWI2 = 24            #TAC Switch Input 2
SWI3 = 25            #TAC Switch Input 3
ON = True            #Use ON instead of True
OFF = False          #Use OFF instead of False

# Setup GPIO pins for input or output
GPIO.setup(LED1, GPIO.OUT)
GPIO.setup(LED2, GPIO.OUT)
GPIO.setup(LED3, GPIO.OUT)
GPIO.setup(SWI1, GPIO.IN)
GPIO.setup(SWI2, GPIO.IN)
GPIO.setup(SWI3, GPIO.IN)

Add the below code to the end what happens?
#Set LED1 ON
GPIO.output(LED1, ON)
GPIO.cleanup()   #Tidy up GPIO

To run the program you will need to add sudo in the command line as GPIO commands needs super user access. Example: sudo python myfolder/gpioprog1.py
If you run the code above it seem that nothing happens all LED's stay off? t
hat is because the program runs so fast you can't see LED 1 go on. The GPIO.cleanup() line will reset all pins and should be in the exit code of any program using GPIO functions. 
Try adding a delay as below.

#Set LED1 ON
GPIO.output(LED1, ON)
time.sleep(5)    #Wait 5 seconds
GPIO.cleanup()   #Tidy up GPIO

Add the lines above the clean up line. You can also directly address the GPIO pin if you want like the below code but its better to use variables.

#Set LED2 ON another way
GPIO.output(15, True)
time.sleep(5)    #Wait 5 seconds

To use inputs we need a loop to continually read the input pins.
Add the below code before the clean up line. This program will loop until switch 1 is pressed.

#Set variable for exiting the loop
Exit = False
while (Exit == False):
#Detect if Switch 1 is turned pressed
if GPIO.input(SWI1):
print "Pin 1 has been pushed"
Exit = True # Exit loop

Now you have the basics to control inputs and outputs. Here is a program that does a bit more just for fun.

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
LED1 = 14            #LED Output 1
LED2 = 15            #LED Output 2
LED3 = 18            #LED Output 3
SWI1 = 23            #TAC Switch Input 1
SWI2 = 24            #TAC Switch Input 2
SWI3 = 25            #TAC Switch Input 3
ON = True            #Use ON instead of True
OFF = False          #Use OFF instead of False

# Setup GPIO pins for input or outputGPIO.setup(LED1, GPIO.OUT)
GPIO.setup(LED2, GPIO.OUT)
GPIO.setup(LED3, GPIO.OUT)
GPIO.setup(SWI1, GPIO.IN)
GPIO.setup(SWI2, GPIO.IN)
GPIO.setup(SWI3, GPIO.IN)

#Set variable for exiting the loop
Exit = False
OUT1 = OFF
OUT2 = OFF
OUT3 = OFF
GPIO.output(LED1, OFF)
GPIO.output(LED2, OFF)
GPIO.output(LED3, OFF)
while (Exit == False):
#Detect if Switch 1 is turned pressed
if GPIO.input(SWI1):
print "1 has been pushed"
if (OUT1==OFF):
GPIO.output(LED1, ON)
time.sleep(2)
else:
GPIO.output(LED1, OFF)
time.sleep(2)
OUT1 = not OUT1
#Detect if Switch 2 is turned pressed
if GPIO.input(SWI2):
print "2 has been pushed"
if (OUT2==OFF):
GPIO.output(LED2, ON)
time.sleep(2)
else:
GPIO.output(LED2, OFF)
time.sleep(2)
OUT2 = not OUT2

#Detect if Switch 3 is turned pressed
if GPIO.input(SWI3):
print "3 has been pushed"
if (OUT3==OFF):
GPIO.output(LED3, ON)
time.sleep(2)
else:
GPIO.output(LED3, OFF)
time.sleep(2)
OUT3 = not OUT3
if (OUT1 and OUT2 and not OUT3):
Exit = True
print "Program exit"
print "Goodbye :)"

GPIO.cleanup()

The above program will toggle the LED on and off as the cosponsoring switch is pressed. When the lights are ON, ON, OFF the program will exit.

No comments:

Post a Comment

Thank you fro reading my page.
Let me know if there is anything that I could add or change.
Please let me know if this information is helpful.