Raspberry Pi LInux LESSON 26: Controlling GPIO Pins in Python

In this lesson we will actually begin to control the GPIO pins from the Raspberry Pi. We will start by looking at how to write a pin high or low. We will be doing this in the Python programming language. A really important thing to remember is that the default “Pi” user does not have access to the pins, so for these examples to work, you must run the programs with “sudo”. The sudo command executes as super user, and will give the program access to the GPIO pins.

To begin with, lets build a simple circuit. If you purchased the kit we showed in lesson 1, you should have all the components you need to follow along with these examples. If you have not purchased a kit yet, you can get one on amazon.com HERE.  In this first lesson we will just be looking at blinking an LED. So, you can now go ahead and hook up the following circuit. For reference, we show below the pinout of the Raspberry Pi.

Raspberry Pi 2 Pinout
This figure shows the Raspberry Pi GPIO pinout

In this example we will be using physical pin 9 as a ground, and physical pin 11 as the control pin. You can now go ahead and hook up the following circuit. Please remember the direction you plug the LED in matters . . . the long leg needs to connect to pin 11. The resistor used should be about 330 ohms.

Raspberry Pi Circuit
This Circuit Will Blink a Red LED

In order to become familiar with the commands, I like to start in the Python shell. Basically, we give the commands to python one line at a time and watch what happens. Then later, we can write and run programs.

Note that recent versions of the Raspberry Pi distribution include the RPi library, but if you have an older distribution, update your system using these commands:

$ sudo apt-get update


$ sudo apt-get upgrade

If you have not done this is a while, it can take some time to download and install the updates.

We are now ready to begin to work with the GPIO pins. To enter the python shell, open a terminal window on the Raspberry Pi, and you will want to type:

$ sudo python

Be sure and use the sudo command above, as that will give you administrative access to the GPIO pins. Now, you should get the python command shell prompt, that looks like this:


At this point, any command you type will be executed by the python interpreter. You can basically execute a python program one line at a time. Note, to exit the python shell type Ctrl-d.

OK, so lets see if we can control the LED!

First, we need to import the RPi library. Note this is case sensitive, so be careful to do capitalization exactly:

>>> import RPi.GPIO as GPIO

Now, we need to initialize the GPIO to use either the BOARD or the BCM pin numbering schemes. In the diagram at the top of this lesson, the BOARD numbering convention is shown in the center two columns. If you want to use the BCM numbering scheme, you would use the numbers indicated in the outer two columns. In these examples, I want to use the physical pin numbers, as that is easier to me to keep track of things. Hence, I will want to use the BOARD scheme. I can do that with this command:


As you can imagine, if you want to use BCM, BOARD should be replaced with BCM in the command above.

If you remember in our Arduino lessons, we had to do pinmode commands to tell the arduino whether pins are inputs or outputs. We do an analogous thing in Raspberry Pi. We need to tell the Pi whether we will be using a pin as an input or output. In the wiring diagram above, you can see that we want to power the LED from physical pin 11, so we need to set that as an output.


We are now ready to turn the LED on. We can do this by setting pin 11 to True:


Now to turn the LED off, we can do:


You can now play around with different GPIO pins, and turn the LED on and off as you like. Before leaving the Python shell, be sure to clean up the GPIO. You do this by giving the cleanup command:


This will ensure you do not get error messages if you try to work with the GPIO pins again. It is a good practice to always cleanup after you are done.

9 thoughts on “Raspberry Pi LInux LESSON 26: Controlling GPIO Pins in Python”

  1. Hi Paul
    Thank you do much for all your patient and spending time to teach us, I m actually thankful for it!
    I did successfully some of your Raspberry projects, and all these worked fine.
    I just wondering if you have a time to teach us, about how to write text on these new OLED display 128×64 with the chipset SSD1306, because these devices are relatively cheap and low system resources consuming. They are for the Adafriut for the Raspberry using I2C or SPI protocols to communicate.
    I couldn’t find on the internet some simple and clear code examples.
    I’ve read your Bible studies too.
    best regards
    God bless

    1. Hugo,
      thanks for the kind words. I have not played around with the OLED displays so can not be much help. I will take a look when I get some time.

  2. Hi Paul

    Thanks for your tutorials – Id like to know how can you communicate the GPIO between two Pis through ssh?

  3. Hello Dear professor
    your videos are very useful and very nice
    but i have an request :
    if it possible please learn us I2C and SPI protocols on raspberry pi – and learn me how to use raspberry pi camera;

    Thanks very very …. much

  4. Hey professor,

    Your tutorials are great, but I’m finding some issues in your tutorials, you’re doing something which isn’t the best thing to do.

    As in this post, you’re running python via sudo python which is a very very bad idea.

    You can just run python and run as a normal user. I assume most of your students to be beginner, and teaching them to use sudo everywhere isn’t a good idea.

    1. I am not using sudo everywhere, I am using it to run python. I need those priviledges for python to access certain hardware. It is being done for a reason.

  5. Paul,
    Another great lesson.I don’t know how you find the time to create such good quality lessons, but keep doing it. I like the way that you do the occasional mistake to reinforce the lesson. A very good series.

  6. I’m an old guy engineering from before CMOS. But I’ve always been taught that you avoid sourcing voltage from any device because they typically can sink more current than they can source. But every tutorial on the Python and Arduino that I’ve absorbed source the LED voltage from the CPU.

    Did I miss a paradigm shift in design?

    1. Yes you have to be concerned about current, but you should not say never. The arduino output pins can output a few tens of milliamps. More than enough to light an LED (with current limiting resistor), or drive high input impedence devices. No you should not try and run a motor or big lights or an oven, but yes, you can drive low current devices like an LED.

Comments are closed.