Home

How to Use Python to Access GPIO Pins on Your Raspberry Pi

|
|  Updated:  
2017-11-08 12:10:46
Getting Started with Raspberry Pi
Explore Book
Buy On Amazon
You can access GPIO pins on your Raspberry Pi with Python. Unlike Scratch's graphic-based program blocks, Python uses entirely text-based instructions. Its great power is that the basic Python language can be extended to do more things by the use of libraries. These are functions that can be written in Python or any other language to extend what Python can do.

When using Python to access the GPIO pins, you have a number of different libraries you could choose that can give you access to them. They can provide not only normal input/output access but also access to some of the special functions or capabilities of certain pins.

In the early days of the Raspberry Pi, the only access to the GPIO pins was if you were running in Supervisor, or Root, mode. Fortunately, this has now been changed so that you can run in normal User mode with most libraries.

While the newly introduced Thonny Python IDE (Integrated Development Environment) is popular with beginners, our preferred environment for writing Python is IDLE3 (it has more features), but either can be used. IDLE3 and Thonny can both be accessed from the main Desktop Raspberry menu. So, pick the one of your choice and create a new file, and then type in the program you see below. Remember that, in Python, the case of a word matters as does the spaces at the start of a line, so be sure to get them right; otherwise, you will get errors when trying to run your code.

#!/usr/bin/python3

import time

import RPi.GPIO as io # using RPi.GPIO

io.setmode(io.BCM)

io.setup(4,io.OUT) # make pin into an output

print("LED blinker - By Mike Cook")

print("Ctrl C to quit")

while True:

io.output(4,0)

time.sleep(0.30)

io.output(4,1)

time.sleep(0.30)

Save the file and run it. Your LED blinks just like the Scratch example.

Take a look at the code line by line. The program starts by importing the support packages you need. In this case, it’s the time package used to achieve a Delay, or Wait, function, and the RPi.GPIO package to access the GPIO pins. Note here that a lot of examples use as GPIO in the import statement — but you can use the simpler and shorter as io because it reduces the amount of typing and you don't have to keep switching to uppercase.

Then you have to tell the library what sort of numbering system you want to use. We’re using the now-standard BCM system. The next line tells the GPIO pins that you want to use Pin 4 as an output. The setup method takes two values: the pin number and a number that tells the library to make that pin an output. This is conveniently hidden by the library, by using a predefined constant that’s defined in the library — that’s why it’s prefixed with io.

Next, the loop forever of Scratch is carried out in Python with the use of the while True statement, with all statements in this loop indented.

This loop then commands the GPIO Pin 4 to be a 0, turning the LED off. Then there’s a delay (or sleep) for 300mS, followed by turning the LED on by making Pin 4 go high. Finally, there’s another delay, so you can see the LED in the On state. A common mistake is to leave out this last delay; as a result, the LED looks to be off all the time. So, controlling a GPIO output is very simple.

Now, on to the second example of a switch-controlled blink speed. This is shown below, so open up a new file and type it in.

#!/usr/bin/python3

io.setmode(io.BCM)

io.setup(4,io.OUT) # make pin into an output

io.setup(24,io.IN, pull_up_down=io.PUD_UP) # make pin an input

print("LED blinker - By Mike Cook")

print("Ctrl C to quit")

while True:

io.output(4,0)

if io.input(24) == 1:

time.sleep(0.30)

else :

time.sleep(1.00)

io.output(4,1)

if io.input(24) == 1:

time.sleep(0.30)

else :

time.sleep(1.00)

This code has the same commands for the output pin, but the input pin setup is new. This command has three parameters: the number of the pin to use, a number in the form of a constant defined by the library, specifying that this pin should function as an input, and, finally, some commands telling the computer to activate the internal pull-up resistor (although, to our ears, PUD_UP sounds more like a mother calling children to tell them their pudding is being served).

The LED is first turned off as before, and then the input from the push button is read by the input(24) method, which returns a value of 0 or 1, depending on whether the pin is connected to the ground. This returned value is then compared to 1, and if it’s equal, a 300mS delay is made; otherwise, it produces a 1-second delay. The LED is turned on, and the Conditional Delay code is repeated.

About This Article

This article is from the book: 

About the book author:

Mike Cook is a former professor in physics at Manchester Metropolitan University. His other books include Raspberry Pi Projects and Raspberry Pi Projects For Dummies.

Sean McManus is an expert technology and business author. His previous books include Mission Python, Coder Academy, and Cool Scratch Projects in Easy Steps.