Raspberry Pi Advanced Gpio Library, WIRINGPI2 for Python use Note (iii) GPIO operations

Source: Internet
Author: User

Gpio Library core function, of course, is the operation of Gpio, Gpio is the "general input/Output" interface, such as lighting a LED, relays, or through the IIC SPI 1-wire and other protocols, read, write data, this is the use of Gpio, can say no gpio, Raspberry Pi can only be used as a small computer, with a gpio, it is upgraded to a controller. First of all, how to operate a digital volume (high and low level).

Look at the code first:

Import Wiringpi2 as Gpio  from Import gpiogpio.wiringpisetup ()   # Initialize # set the Pin25 to output mode #pin 25 output to High level Print # Print the status of the Pin25

The value of the note is that GPIO can also read the Gpio state when in output mode.

WIRINGPI the Gpio for Raspberry Pi 2 is defined as follows:

[Email protected] ~/testcode]# Gpio ReadAll+-----+-----+---------+------+---+---Pi2---+---+------+---------+-----+-----+ | 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 | the| -| |     |      |      0v |   | |9||Ten|1| ALT0 | RxD | -| the| | -|0| GPIO.0| In |0| One|| A|0| In | GPIO.1|1| -| | -|2| GPIO.2| In |0| -|| -|      | |     0v |     | | | A|3| GPIO.3| In |0| the|| -|0| In | GPIO.4|4| at| |     | |3.3v |   | | -|| -|0| In | GPIO.5|5| -| |Ten| A|   MOSI | In |0| +|| -|      | |     0v |     | | |9| -|   Miso | In |0| +|| A|0| In | GPIO.6|6| -| | One| -|   SCLK | In |0| at|| -|1| In | CE0 |Ten|8| |     |      |      0v |   | | -|| -|1| In | CE1 | One|7| |0| -| Sda.0| In |1| -|| -|1| In | Scl.0| to|1| |5| +| GPIO. +| In |1| in|| -|      | |     0v |     | | |6| A| GPIO. A| In |1| to|| +|0| In | GPIO. -| -| A| | -| at| GPIO. at| In |0| -|| the|      | |     0v |     | | | +| -| GPIO. -| In |0| *|| $|0| In | GPIO. -| -| -| | -| -| GPIO. -| In |0|Panax Notoginseng|| -|0| In | GPIO. -| -| -| |     |      |      0v |   | | the|| +|0| In | GPIO. in| in| +| +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ | BCM |   WPi | Name | Mode | V | Physical | V | Mode | Name | WPi | BCM | +-----+-----+---------+------+---+---Pi2---+---+------+---------+-----+-----+

We see that WRINGPI has three definitions for pins, BCM stands for Gpio mode, WPI stands for pin mode, and physical represents physical pin mode.

For example, we operate on the PIN25, according to the physical pin is the 37,gpio mode is 26

The same pin that the code below accesses:

view Plaincopy to Clipboardprint?#GPIO Port NumbersImportWiringpi2 as Wiringpi Wiringpi.wiringpisetupgpio () Wiringpi.pinmode (26, 0)#sets GPIO to input  #WIRINGPI NumbersImportWiringpi2 as Wiringpi wiringpi.wiringpisetup () Wiringpi.pinmode (, 0)#sets WP pin to input  #physical P1 Header pin numbersImportWiringpi2 as Wiringpi Wiringpisetupphys () wiringpi.pinmode (PNS), 0)#sets P1 pin PNS to input

Replace the initialization function, you can switch in various modes, we use the Wiringpisetup () function, so is the pin mode, pin25 in the lower left corner of the pin, with 0v adjacent, wiring as follows:

In addition, there is another way to operate the Gpio:

 from Import Gpiogpio=gpio ()  #新建GPIO对象, the default is PIN mode, you can add parameters to another mode Gpio.pinmode (. Gpio). OUTPUT) #把GPIO25设为OUTPUTgpio. Digitalwrite (25,1)   #把GPIO25设为高电平 print(Gpio.digitalread ( 25))

This method is a class-encapsulated WIRINGPI interface, which is slower than the direct call function, and is not recommended if the program requires high timing.

Raspberry Pi Advanced Gpio Library, WIRINGPI2 for Python use Note (iii) GPIO operations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.