Malina Pi

Working with Raspberry Pi 3 GPIO with Python

Working with Raspberry Pi 3 GPIO with Python
Raspberry Pi is a single board computer that runs on Linux. It is mainly used for IoT (Internet of Things) projects. As it is very cheap, people also use it to learn about computers and programming.  Raspberry Pi has GPIO (General Purpose Input Output) pins used for controlling or interacting with electronic components and integrated circuits. You can use Python programming language for interacting with the components connected to the GPIO pins of Raspberry Pi.

The version of Raspberry Pi I have is Raspberry Pi 3 Model B. It has 40 GPIO pin header and not all of them can be used for interacting with other electronic components. You will have to hook up your electronic components to the right pin number. Otherwise, you won't be able to interact or control the electronic component connected to your Raspberry Pi. I will refer you to a diagram that you can use to find out which pin to use for what purpose. Don't worry about it.

In this article, I will make a simple LED blinking project with Raspberry Pi using the GPIO pins. I will show you how to use Python to interact with the GPIO pins of Raspberry Pi. I will use Raspberry Pi 3 Model B for the demonstration as this is what I have. If you're using another version of Raspberry Pi, then you may have to make necessary changes for it to work. So, let's get started.

Things You Need:

To successfully build the Raspberry Pi LED blinking project with me, you need,

Circuit Diagram:

You should connect all the components to your Raspberry Pi 3 as shown in the circuit diagram below.

On breadboard it looks something like this.

Note that I connected one end of the resistor to the GPIO0 which is PIN 11 on Raspberry Pi 3, and one end of the red LED to the GND (ground) pin which is PIN 6 on the Raspberry Pi 3.

Connecting to the Raspberry Pi 3 Remotely:

If you own a Raspberry Pi and use Raspbian, then you most likely have SSH and VNC enabled already. If you've followed one of my articles on how to install Raspbian on Raspberry Pi, then you should know how to enable them. If you are in doubt on whether these are enabled, please read the article at (Link of the article Install Raspbian on Raspberry Pi).

If you prefer to work with the command line, then SSH into your Raspberry Pi 3 with the following command:

$ ssh [email protected]

If you prefer to work graphically, use VNC Viewer to connect to your Raspberry Pi 3. Just type in the IP address of your Raspberry Pi and press .

Now, type in pi as the username and the password that you set up while installing Raspbian as the password. Then click on OK.

You should be connected.

Writing Codes for the Raspberry Pi Blink LED Project:

First, open up a Terminal (if you're using VNC Viewer) and create a new file blink.py

Now, open blink.py with a text editor (I will use nano) as follows:

$ nano blink.py

Now, type in the following lines of Python codes and save the file by pressing + x and then press y followed by .

Here, on line 1, I imported the the Python RPi.GPIO from the RPi library to work with the GPIO pins of Raspberry Pi.

On line 2, I imported the Python time and time library.

On line 4-6, I defined 3 variables.

Line 9 is used to set up the GPIO pin ledPin which is pin 11 for output using the GPIO.setup() library function. GPIO.setup() accepts two arguments. One is the pin number, and the other is the mode at which this pin will work. In this case, it is GPIO.OUT or output mode. If you want to take inputs, then it should be GPIO.IN.

On line 11-19, a try-except block is used. So, when I stop the Python script, it will print the text Thanks for using blink.py (line 19) instead of error messages. I also made sure the GPIO pin is cleaned up in line 18 with GPIO.cleanup() library function.

The actual code for the Blink project is in line 12-16. Here, an infinite loop is used.

At each iteration of the loop,

Once you're done writing the code, run blink.py script with the following command:

$ python blink.py

As you can see, the LED statuses are being displayed every 500ms.

As you can see, the LED is blinking every 500ms.

[Include the video file in files/output.3gp]

If you want to stop the blink.py script, just press + c.

As you can see, the script execution has stopped.

Raspberry Pi 3 Model B GPIO Pinouts:

You can find the Raspberry Pi 3 Model B GPIO pinouts at http://pi4j.com/pins/model-3b-rev1.html

Be sure to check it out.

So, that's how you work with the GPIO pins of Raspberry Pi 3 with Python. Thanks for reading this article.

Gry Darmowe i otwarte silniki gier do tworzenia gier na Linuksa
Darmowe i otwarte silniki gier do tworzenia gier na Linuksa
Ten artykuł zawiera listę darmowych i otwartych silników gier, których można używać do tworzenia gier 2D i 3D w systemie Linux. Istnieje wiele takich ...
Gry Samouczek Shadow of the Tomb Raider dla systemu Linux
Samouczek Shadow of the Tomb Raider dla systemu Linux
Shadow of the Tomb Raider to dwunasty dodatek do serii Tomb Raider - przygodowej serii gier akcji stworzonej przez Eidos Montreal. Gra została dość do...
Gry Jak zwiększyć FPS w Linuksie??
Jak zwiększyć FPS w Linuksie??
FPS oznacza Klatki na sekundę. Zadaniem FPS jest pomiar liczby klatek na sekundę podczas odtwarzania wideo lub wydajności gier. W prostych słowach lic...