Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the basis for the use of RPi.GPIO modules?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail what is the basis for the use of the RPi.GPIO module, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

The RPi.GPIO module package provides a class that controls GPIO in Raspberry Pi.

Note that this module is not suitable for applications that pursue real-time or counting cycles. This is because you cannot predict when Ptyhon will be busy and when resources will be reclaimed. And it runs on systems based on Linux cores and is not suitable for real-time applications-because other processes may get higher CPU priorities. If you must pursue the performance of real-time operation, you can consider purchasing an Arduino motherboard http://www.arduino.cc!

It should also be noted that the current version does not support Raspberry Pi's SPI, I2C, 1-wire, serial port and other functions. The plan will come true in the near future!

Import module

Import the RPi.GPIO module:

one

Import RPi.GPIO as GPIO

With this, you can map the module name to GPIO so that you can use it later in your other scripts.

To import the module and check whether it was imported successfully, you can try:

one

two

three

four

Try:

Import RPi.GPIO as GPIO

Except RuntimeError:

Print ("error importing RPi.GPIO! this may be due to lack of superuser privileges. You can use 'sudo' to run your script.")

Pin number

There are currently two ways to number IO pins on Raspberry Pi through RPi.GPIO.

The first way is to use the BOARD numbering system. This method refers to the pin number of the P1 terminal on the Raspberry Pi motherboard. The advantage of using this approach is that you don't have to worry about the revised version of the motherboard, and your hardware is always available. You will not need to reconnect the line and change your code.

The second way is to use BCM numbering. This is a lower-level way of working-this way refers to the channel number of the Broadcom SOC. During use, you should always make sure that the pins on the motherboard correspond to the channel number marked on the chart. Your script may not work when the Raspberry Pi motherboard is updated with a revised version.

Specify the method you use (must be specified):

one

GPIO.setmode (GPIO.BOARD)

Or

one

GPIO.setmode (GPIO.BCM)

Warning

There may be multiple scripts / loops on your Raspberry Pi's GPIO at the same time. Therefore, if RPi.GPIO detects that a pin is set to a different purpose than the default state (the default is input), you will get a warning message when you try to configure a script.

Disable the warning message:

one

GPIO.setwarnings (False)

Configure Channel

You need to configure a channel for each pin used for input or output.

The channel configured as input:

one

GPIO.setup (channel, GPIO.IN)

(the channel number is assigned based on the numbering system you are using (BOARD or BCM). )

More advanced information about the input channel can be found here.

Channels configured for output:

one

GPIO.setup (channel, GPIO.OUT)

(the channel number is assigned based on the numbering system you are using (BOARD or BCM). )

You can also specify the initial value of the output channel:

one

GPIO.setup (channel, GPIO.OUT, initial=GPIO.HIGH)

Input

Read the value of the GPIO pin:

one

GPIO.input (channel)

(the channel number is assigned based on the numbering system you are using (BOARD or BCM). This will return either 0 / GPIO.LOW / False or 1 / GPIO.HIGH / True.

Output

Set the output status of the GPIO pins:

one

GPIO.output (channel, state)

(the channel number is assigned based on the numbering system you are using (BOARD or BCM). )

The status can be 0 / GPIO.LOW / False or 1 / GPIO.HIGH / True.

Clear

At the end of any program, please form a good habit of cleaning up used resources. The same is true for using RPi.GPIO. By restoring all used channel states to input, you can avoid accidental damage to your Raspberry Pi pins due to short circuit. Note that this will only clean up the GPIO channels used by your script.

Clean up after your script is finished:

one

GPIO.cleanup ()

Raspberry Pi revised version and RPi.GPIO version

Detect the revised version of the Raspberry Pi motherboard:

one

GPIO.RPI_REVISION

Detect the version of RPi.GPIO:

one

GPIO.VERSION

On the basis of the use of the RPi.GPIO module is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report