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.

Ähnliches Produkt:

Raspberry Pi 3 Model B

Raspberry Pi Ltd Eingebettete Systementwicklungsboards und -kits Anzeigen

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.

Ähnliche Beiträge

Neue Beiträge

Leider ergab Ihre Suche kein Ergebnis

Aktuelles über Elektronikkomponenten­

Wir haben unsere Datenschutzbestimmungen aktualisiert. Bitte nehmen Sie sich einen Moment Zeit, diese Änderungen zu überprüfen. Mit einem Klick auf "Ich stimme zu", stimmen Sie den Datenschutz- und Nutzungsbedingungen von Arrow Electronics zu.

Wir verwenden Cookies, um den Anwendernutzen zu vergrößern und unsere Webseite zu optimieren. Mehr über Cookies und wie man sie abschaltet finden Sie hier. Cookies und tracking Technologien können für Marketingzwecke verwendet werden.
Durch Klicken von „RICHTLINIEN AKZEPTIEREN“ stimmen Sie der Verwendung von Cookies auf Ihrem Endgerät und der Verwendung von tracking Technologien zu. Klicken Sie auf „MEHR INFORMATIONEN“ unten für mehr Informationen und Anleitungen wie man Cookies und tracking Technologien abschaltet. Das Akzeptieren von Cookies und tracking Technologien ist zwar freiwillig, das Blockieren kann aber eine korrekte Ausführung unserer Website verhindern, und bestimmte Werbung könnte für Sie weniger relevant sein.
Ihr Datenschutz ist uns wichtig. Lesen Sie mehr über unsere Datenschutzrichtlinien hier.