Raspberry Pi advanced GPIO library, wiringpi2 for python Use note (3) GPIO operations, gpiowiringpi2
The core function of the GPIO library is to operate GPIO. GPIO is the "general input/output" interface, such as lighting up an LED or relay, or using iic spi 1-wire and other protocols, reading and writing data are all useful for GPIO. It can be said that without GPIO, Raspberry Pi can only be used as a small computer. With GPIO, it will be upgraded to a controller. Let's talk about how to operate a number (high and low ).
First look at the Code:
Import wiringpi2 as gpiofrom wiringpi2 import GPIOgpio. wiringPiSetup () # initialize gpio. pinMode (25, GPIO. OUTPUT) # Set pin25 to the OUTPUT mode gpio. digitalWrite (25, GPIO. HIGH) # The output of pin25 is a HIGH-level print (gpio. digitalRead (25) # print the status of pin25
You can also read the GPIO status in the output mode.
Wiringpi defines the GPIO of Raspberry Pi 2 as follows:
[root@RasPi ~/testcode]# gpio readall +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+ | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM | +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ | | | 3.3v | | | 1 || 2 | | | 5v | | | | 2 | 8 | SDA.1 | IN | 1 | 3 || 4 | | | 5V | | | | 3 | 9 | SCL.1 | IN | 1 | 5 || 6 | | | 0v | | | | 4 | 7 | GPIO. 7 | IN | 1 | 7 || 8 | 1 | ALT0 | TxD | 15 | 14 | | | | 0v | | | 9 || 10 | 1 | ALT0 | RxD | 16 | 15 | | 17 | 0 | GPIO. 0 | IN | 0 | 11 || 12 | 0 | IN | GPIO. 1 | 1 | 18 | | 27 | 2 | GPIO. 2 | IN | 0 | 13 || 14 | | | 0v | | | | 22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23 | | | | 3.3v | | | 17 || 18 | 0 | IN | GPIO. 5 | 5 | 24 | | 10 | 12 | MOSI | IN | 0 | 19 || 20 | | | 0v | | | | 9 | 13 | MISO | IN | 0 | 21 || 22 | 0 | IN | GPIO. 6 | 6 | 25 | | 11 | 14 | SCLK | IN | 0 | 23 || 24 | 1 | IN | CE0 | 10 | 8 | | | | 0v | | | 25 || 26 | 1 | IN | CE1 | 11 | 7 | | 0 | 30 | SDA.0 | IN | 1 | 27 || 28 | 1 | IN | SCL.0 | 31 | 1 | | 5 | 21 | GPIO.21 | IN | 1 | 29 || 30 | | | 0v | | | | 6 | 22 | GPIO.22 | IN | 1 | 31 || 32 | 0 | IN | GPIO.26 | 26 | 12 | | 13 | 23 | GPIO.23 | IN | 0 | 33 || 34 | | | 0v | | | | 19 | 24 | GPIO.24 | IN | 0 | 35 || 36 | 0 | IN | GPIO.27 | 27 | 16 | | 26 | 25 | GPIO.25 | IN | 0 | 37 || 38 | 0 | IN | GPIO.28 | 28 | 20 | | | | 0v | | | 39 || 40 | 0 | IN | GPIO.29 | 29 | 21 | +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM | +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+
We can see that wringpi has three definitions of pins. BCM represents the GPIO mode, wPi represents the pin mode, and Physical represents the Physical pin mode.
For example, for the Pin25 we operate above, the physical pin is 37, and the GPIO mode is 26.
The following code accesses the same pin:
view plaincopy to clipboardprint?# GPIO port numbers import wiringpi2 as wiringpi wiringpi.wiringPiSetupGpio() wiringpi.pinMode(26, 0) # sets GPIO 26 to input # wiringpi numbers import wiringpi2 as wiringpi wiringpi.wiringPiSetup() wiringpi.pinMode(25, 0) # sets WP pin 25 to input # Physical P1 header pin numbers import wiringpi2 as wiringpi wiringPiSetupPhys() wiringpi.pinMode(37, 0) # sets P1 pin 37 to input
Changing the initialization function can be switched in various modes. We use the wiringPiSetup () function, so it is in pin mode. pin25 is in the lower left corner of the pin and adjacent to 0 V. The wiring is as follows:
From wiringpi2 import GPIOgpio = GPIO () # create a new GPIO object. The default value is pin mode. You can add parameters to switch to another mode gpio. pinMode (25, GPIO. OUTPUT) # Set GPIO25 to OUTPUTgpio. digitalWrite () # Set GPIO25 to a high-level print (gpio. digitalRead (25 ))
This method uses the wiringpi interface encapsulated by a class, which is slower than calling a function directly. If the program requires a higher time series, this method is not recommended.