How to access Raspberry Pi 3 GPIO with Python

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.

Raspberry Pi 3 GPIO

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 3 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.

Ver Productos relacionados

Raspberry Pi 3 Model B

Raspberry Pi Ltd Placas y kits de desarrollo del sistema integrado Ver

Python RPI.GPIO library

The Raspberry Pi has Raspbian as the default OS where the Python RPi.GPIO library 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.

GPIO pin numbering

Next, 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.


Pros and cons of Python with Raspberry Pi GPIO

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.


Are you on the list?
Sign up to receive exclusive offers, product announcements, and the latest industry news.

Artículos informativos relacionados

Últimas noticias

Lo sentimos, pero su selección de filtros no devolvió resultados.

Hemos actualizado nuestra política de privacidad. Por favor tome un momento para revisar estos cambios. Al hacer clic en Acepto, usted está de acuerdo con la Politica de Privacidad de Arrow Electronics y sus condiciones de uso.

Nuestro sitio Web coloca cookies en su dispositivo para mejorar su experiencia y nuestro sitio. Lea más sobre las cookies que utilizamos y cómo desactivarlas aquió. Es posible que se utilicen las cookies y tecnologías de seguimiento con fines de marketing.
Al hacer clic en "Aceptar", usted está consintiendo la colocación de cookies en su dispositivo y el uso de tecnologías de seguimiento. Haga clic en "Leer más" a continuación para obtener más información e instrucciones sobre cómo desactivar las cookies y tecnologías de seguimiento. Si bien la aceptación de cookies y tecnologías de seguimiento es voluntaria, la desactivación de estos puede resultar en que el sitio web no funcione correctamente, y es posible que ciertos anuncios sean menos relevantes para usted.
Respetamos su privacidad. Lea nuestra política de privacidad aquió