Access to GPIO with Python on Your Raspberry Pi 3

Published By

Raspberry Pi was mainly designed for educational purposes, allowing young people to take their first steps in programming with an inexpensive Linux computer. But as electronics become more powerful every day and with the addition of Bluetooth 4.1 and WIFI to the Raspberry Pi 3, the device can now meet professional requirements.

A standard PC does not give access to processor pins – there is no way to connect a new sensor communicating through an I2C bus to a PC, for instance. However, the Raspberry Pi gives access to GPIO. This kind of feature is normally reserved for microcontrollers. But, this requires some embedded programming knowledge, which can be tedious for non-experts. The Raspberry Pi allows for the possibility to program the GPIO thanks to a few lines of code in Python. Python is among the easiest languages to learn and has a very large community that is not composed only of computer science engineers. The Raspberry Pi has created its own market between standard PCs and microcontrollers. Let’s take a look at the procedure that must be followed in order to toggle a GPIO on a Raspberry.

The Raspberry Pi has Raspbian as the default OS where the Python library RPi.GPIO is already loaded. It is currently using the version 0.6.2, which can be downloaded at pypi.python.org.

Examples explaining how to use this library are available on open source websites.

As it’s a Python program, the libraries must be imported. Two libraries are necessary: the RPI.GPIO library to drive pins and the time library, which allows users to create timing between each transition of the pin.

Then, it is necessary to declare the type of numbering system. The BOARD option uses the pins exactly as they are laid out on the Pi. This connector and numbering don’t change between versions. The BCM option uses the Broadcom SoC numbering, which differs between Raspberry pin versions. The table below shows the differences between the two numbering systems.

In our program, the board number is used and selected by the command GPIO.setmode function.

Then, the pin 12 is configured as the output, and we’ll toggle this pin. In an infinite loop, the pin is put on high (3V) for one second, then grounded (0V) for one second.

The form factor of the Raspberry Pi allows for easy physical access to the GPIOs, and a scope is connected to a ground pin (pin 25) and the pin 12.

 

As the screen shot of the scope confirms, the pin 12 toggles between 0 and 3V at a frequency of 0.5Hz, as programmed in the Python script.

Python script cannot be used in Real Time system as the Linux OS does not guarantee the exact timing to drive the pin. Other priorities can delay the driving of a GPIO, which is not a high priority. However, to communicate with the external world, it’s a quick, efficient, and easy way that traditional PCs don’t offer.

Latest News

Sorry, your filter selection returned no results.

We've updated our privacy policy. Please take a moment to review these changes. By clicking I Agree to Arrow Electronics Terms Of Use  and have read and understand the Privacy Policy and Cookie Policy.

Our website places cookies on your device to improve your experience and to improve our site. Read more about the cookies we use and how to disable them here. Cookies and tracking technologies may be used for marketing purposes.
By clicking “Accept”, you are consenting to placement of cookies on your device and to our use of tracking technologies. Click “Read More” below for more information and instructions on how to disable cookies and tracking technologies. While acceptance of cookies and tracking technologies is voluntary, disabling them may result in the website not working properly, and certain advertisements may be less relevant to you.
We respect your privacy. Read our privacy policy here