1,点亮LED小灯

学习重点:

1,GPIO的两种分布方式

GPIO.setmode(GPIO.BOARD) (The advantage of using this numbering system is that your hardware will always work)
GPIO.setmode(GPIO.BCM) (it refers to the channel numbers on the Broadcom SOC. You have to always work with a diagram of which channel number goes to which pin on the RPi board.)
mode = GPIO.getmode()


1,点亮LED小灯_第1张图片
image.png

1,点亮LED小灯_第2张图片
image.png

2,close Warning

GPIO.setwarnings(False)

3,set out

GPIO.setup(LED,GPIO.OUT)
GPIO.output(LED, GPIO.HIGH)
GPIO.output(LED, GPIO.LOW)

4, clearup

GPIO.cleanup(pinNum)
or
GPIO.cleanup() also clears the pin numbering system in use.

电路链接

使用了39 Ground 与 40 GPIO21引脚
LED灯长脚的正极,短脚为负极
注意面包板的使用
注意LED灯的不要接反了,接反了,灯不会亮。
最好LED灯接个小电阻,但只是实验不接也没关系。但可能会影响LED灯的寿命。


1,点亮LED小灯_第3张图片
图片发自App

参考代码 led.py

import RPi.GPIO as GPIO
import time
LED = 40
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(LED,GPIO.OUT)

GPIO.output(LED, GPIO.HIGH)
time.sleep(2)
GPIO.output(LED, GPIO.LOW)

GPIO.cleanup(LED)

用 sudo python led.py 命令执行

参考:
https://sourceforge.net/p/raspberry-gpio-python/wiki/BasicUsage/

你可能感兴趣的:(1,点亮LED小灯)