Access to GPIO with Python on Your Raspberry Pi 3

게시자

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.

최신 뉴스

Sorry, your filter selection returned no results.

개인정보 보호정책이 업데이트되었습니다. 잠시 시간을 내어 변경사항을 검토하시기 바랍니다. 동의를 클릭하면 Arrow Electronics 개인정보 보호정책 및 이용 조건에 동의하는 것입니다.

당사의 웹사이트에서는 사용자의 경험 향상과 사이트 개선을 위해 사용자의 기기에 쿠키를 저장합니다. 당사에서 사용하는 쿠키 및 쿠키 비활성화 방법에 대해 자세히 알아보십시오. 쿠키와 추적 기술은 마케팅 목적으로 사용될 수 있습니다. '동의'를 클릭하면 기기에 쿠키를 배치하고 추적 기술을 사용하는 데 동의하는 것입니다. 쿠키 및 추적 기술을 해제하는 방법에 대한 자세한 내용과 지침을 알아보려면 아래의 '자세히 알아보기'를 클릭하십시오. 쿠키 및 추적 기술 수락은 사용자의 자발적 선택이지만, 웹사이트가 제대로 작동하지 않을 수 있으며 사용자와 관련이 적은 광고가 표시될 수 있습니다. Arrow는 사용자의 개인정보를 존중합니다. 여기에서 당사의 개인정보 보호정책을 읽을 수 있습니다.