用python控制树莓派的GPIO

2020年6月25日 / 68次阅读 / Last Modified 2020年6月25日

树莓派这个命令的最后一个字(Pi),就是来自python,树莓派官方发布的OS版本,都集成了python,可见树莓派和python的联系很紧密。用python来控制GPIO,也是自然而然的事情了。

首先需要安装一个package,RPi.GPIO

然后就可以控制GPIO了:

>>> import RPi.GPIO as GPIO
>>> GPIO.setmode(GPIO.BCM)  # use BCM number system
>>> GPIO.setup(18, GPIO.OUT)
>>> GPIO.output(18, GPIO.HIGH)  # high voltage
>>> GPIO.output(18, GPIO.LOW)  # low voltage
>>> GPIO.cleanup()

很简单。

This package provides a class to control the GPIO on a Raspberry Pi.
Note that this module is unsuitable for real-time or timing critical applications. This is because you can not predict when Python will be busy garbage collecting. It also runs under the Linux kernel which is not suitable for real time applications - it is multitasking O/S and another process may be given priority over the CPU, causing jitter in your program. If you are after true real-time performance and predictability, buy yourself an Arduino.

因为Python和Linux多task的原因,不适合用来做realtime应用。但为啥要建议购买Arduino呢?

-- EOF --

本文链接:https://www.pynote.net/archives/2092

相关文章

    留言区

    电子邮件地址不会被公开。 必填项已用*标注


    前一篇:
    后一篇:

    More


    ©Copyright 麦新杰 Since 2019 Python笔记

    go to top