Published Book on Amazon
|All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 1|
|All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 2|
출판된 한글판 도서
|최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (상)|
|최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (하)|
Original Book Contents
220.127.116.11 Overview of <RPi.GPIO> Library
This library provides a class module that allows you to manipulate the GPIO of Raspberry Pi in Python development language.
This library is not suitable for the applications that are in real-time or where time-synchronization is important. Because python can not predict when garbage collect will occur and it runs in a Linux kernel that is not suitable for real-time processing. This is that because Linux is a multitasking O/S, other processors may have a higher priority than the GPIO processing program for the CPU and in that case the GPIO processing program may be confused.
The current version does not support SPI, I2C, hardware PWM or serial functionality in Raspberry Pi. It plans to apply it in the near future.
If you need more information, please refer to the following resources:
Raspbian in Raspberry Pi has <RPi.GPIO> library installed by default. Therefore, there is no need to install it unless there is a special reason. The next is checking whether the library is installed.
pi@raspberrypi ~ $ dpkg -l *gpio
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
ii python-rpi.gpio 0.5.11-1 armhf Python GPIO module for Raspberry Pi
ii python3-rpi.gpio 0.5.11-1 armhf Python 3 GPIO module for Raspberry Pi
If you need additional installation, install the program package as follows. "python-rpi.gpio" is a library for Python 2 IDLE and "python3-rpi.gpio" is a library for Python 3 IDLE.
sudo apt-get install python-rpi.gpio python3-rpi.gpio
18.104.22.168 Usage of <RPi.GPIO> Library
● Processing permission of GPIO
Most Linux distributions of Raspberry Pi restrict the use of GPIO port to only super user. Therefore, to run a program developed in Python, you must use "sudo" command together.
● Pin numbering structure
There are two ways to number the GPIO pins in <RPi.GPIO> library.
■ How to use BOARD numbering system
This means the pin number in the P1 header on the board of Raspberry Pi. It is usually called physical number and is numbered from left to right and from top to bottom. With this method, hardware always works well regardless of the board revision of Raspberry Pi. You do not need to rewrite the connector or modify the program code.
■ How to use BCM numbering system
This method uses channel number in Broadcom SOC and requires a lower level of work. To use this method, you will need an association diagram between the pin on the Rasberry Pi board and channel number, and if the board is revised, you need to modify the program.
You have to specify in advance what numbering system you want to use in the following way.
The following values can be specified to GPIO mode.
■ GPIO.BOARD -- BOARD numbering system
■ GPIO.BCM -- BCM numbering system
For another Python module to determine the pin numbering system currently set, use the following function.
mode = GPIO.getmode()
The result of the execution will be "GPIO.BOARD", "GPIO.BCM" or "GPIO.UNKNOWN".
To use the GPIO library, you have to import it beforehand. Process it at the beginning of the program.
import RPi.GPIO as GPIO
● GPIO setup
GPIO ports must be initialized to input or output before use.
GPIO.setup( <port>, <pin-mode> )
Pin-mode specifies whether to use the port as input or output.
■ GPIO.IN -- Input
■ GPIO.OUT -- Output
Example) GPIO.setup(11, GPIO.OUT)
● GPIO output
For the port specified as the output port, the specified value can be output.
GPIO.output(<port>, < output > )
The following values can be specified to output:
■ GPIO.HIGH, True, 1 -- Current On
■ GPIO.LOW, False, 0 -- Current Off
Example) GPIO.output(11, True)
● GPIO input
For the port specified as the input port, the value coming from the port can be read.
variable = GPIO.input( <port> )
Intput has the following meanings
■ True -- Current On
■ False -- Current Off
Example) input_value = GPIO.input(12)
For an example of interface processing using <RPi.GPIO> library, see [22.214.171.124 Example of <RPi.GPIO> Library].