树莓派从入门到送人——RGB_LED 信号灯实验

演示视频

布线图

显示红色、蓝色、绿色三种基础颜色

#!/usr/bin/env python
# encoding: utf-8

import RPi.GPIO as GPIO
import time

R_PIN = 11
G_PIN = 12
B_PIN = 36
pins = (R_PIN, G_PIN, B_PIN)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pins, GPIO.OUT)
GPIO.output(pins, GPIO.LOW)

pwmR = GPIO.PWM(R_PIN, 70)
pwmG = GPIO.PWM(G_PIN, 70)
pwmB = GPIO.PWM(B_PIN, 70)
pwmR.start(0)
pwmG.start(0)
pwmB.start(0)

def Red(duration):
  pwmR.ChangeDutyCycle(100)
  pwmG.ChangeDutyCycle(0)
  pwmB.ChangeDutyCycle(0)
  time.sleep(duration)

def Green(duration):
  pwmR.ChangeDutyCycle(0)
  pwmG.ChangeDutyCycle(100)
  pwmB.ChangeDutyCycle(0)
  time.sleep(duration)

def Blue(duration):
  pwmR.ChangeDutyCycle(0)
  pwmG.ChangeDutyCycle(0)
  pwmB.ChangeDutyCycle(100)
  time.sleep(duration)

Red(2)
Green(2)
Blue(2)

pwmR.stop()
pwmG.stop()
pwmB.stop()
GPIO.cleanup()

显示更多的颜色

     #!/usr/bin/env python
# encoding: utf-8

import RPi.GPIO as GPIO
import time

R_PIN = 11
G_PIN = 12
B_PIN = 36
pins = (R_PIN, G_PIN, B_PIN)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pins, GPIO.OUT)
GPIO.output(pins, GPIO.LOW)

pwmR = GPIO.PWM(R_PIN, 70)
pwmG = GPIO.PWM(G_PIN, 70)
pwmB = GPIO.PWM(B_PIN, 70)
pwmR.start(0)
pwmG.start(0)
pwmB.start(0)

# 红色
def Red(duration):
  pwmR.ChangeDutyCycle(100)
  pwmG.ChangeDutyCycle(0)
  pwmB.ChangeDutyCycle(0)
  time.sleep(duration)

# 绿色
def Green(duration):
  pwmR.ChangeDutyCycle(0)
  pwmG.ChangeDutyCycle(100)
  pwmB.ChangeDutyCycle(0)
  time.sleep(duration)

# 蓝色
def Blue(duration):
  pwmR.ChangeDutyCycle(0)
  pwmG.ChangeDutyCycle(0)
  pwmB.ChangeDutyCycle(100)
  time.sleep(duration)

# 黄色
def Yellow(duration):
  pwmR.ChangeDutyCycle(100)
  pwmG.ChangeDutyCycle(100)
  pwmB.ChangeDutyCycle(0)
  time.sleep(duration)

# 洋红色
def Magenta(duration):
  pwmR.ChangeDutyCycle(100)
  pwmG.ChangeDutyCycle(0)
  pwmB.ChangeDutyCycle(100)
  time.sleep(duration)

# 青色
def Cyan(duration):
  pwmR.ChangeDutyCycle(0)
  pwmG.ChangeDutyCycle(100)
  pwmB.ChangeDutyCycle(100)
  time.sleep(duration)

# 关灯
def Dark(duration):
  pwmR.ChangeDutyCycle(0)
  pwmG.ChangeDutyCycle(0)
  pwmB.ChangeDutyCycle(0)
  time.sleep(duration)

# 白色
def White(duration):
  pwmR.ChangeDutyCycle(100)
  pwmG.ChangeDutyCycle(100)
  pwmB.ChangeDutyCycle(100)
  time.sleep(duration)

# 自定义一个颜色
def Diy(duration):
  pwmR.ChangeDutyCycle(0)
  pwmG.ChangeDutyCycle(100)
  pwmB.ChangeDutyCycle(20)
  time.sleep(duration)

Red(2)
Green(2)
Blue(2)
Yellow(2)
Magenta(2)
Cyan(2)
Dark(2)
White(2)
Diy(2)

pwmR.stop()
pwmG.stop()
pwmB.stop()
GPIO.cleanup()

设置一个简单的信号灯(红灯10秒、黄灯3秒、绿灯5秒)

#!/usr/bin/env python
# encoding: utf-8

import RPi.GPIO as GPIO
import time

R_PIN = 11
G_PIN = 12
B_PIN = 36
pins = (R_PIN, G_PIN, B_PIN)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pins, GPIO.OUT)
GPIO.output(pins, GPIO.LOW)

pwmR = GPIO.PWM(R_PIN, 70)
pwmG = GPIO.PWM(G_PIN, 70)
pwmB = GPIO.PWM(B_PIN, 70)
pwmR.start(0)
pwmG.start(0)
pwmB.start(0)

# 红色
def Red(duration):
  pwmR.ChangeDutyCycle(100)
  pwmG.ChangeDutyCycle(0)
  pwmB.ChangeDutyCycle(0)
  time.sleep(duration)

# 绿色
def Green(duration):
  pwmR.ChangeDutyCycle(0)
  pwmG.ChangeDutyCycle(100)
  pwmB.ChangeDutyCycle(0)
  time.sleep(duration)

# 黄色
def Yellow(duration):
  pwmR.ChangeDutyCycle(100)
  pwmG.ChangeDutyCycle(100)
  pwmB.ChangeDutyCycle(0)
  time.sleep(duration)

Red(10)
Yellow(3)
Green(5)
GPIO.cleanup()

使用循环设置倒计时

#!/usr/bin/env python
# encoding: utf-8

import RPi.GPIO as GPIO
import time

R_PIN = 11
G_PIN = 12
B_PIN = 36
pins = (R_PIN, G_PIN, B_PIN)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pins, GPIO.OUT)
GPIO.output(pins, GPIO.LOW)

pwmR = GPIO.PWM(R_PIN, 70)
pwmG = GPIO.PWM(G_PIN, 70)
pwmB = GPIO.PWM(B_PIN, 70)
pwmR.start(0)
pwmG.start(0)
pwmB.start(0)

# 红色
def Red(duration):
  pwmR.ChangeDutyCycle(100)
  pwmG.ChangeDutyCycle(0)
  pwmB.ChangeDutyCycle(0)
  time.sleep(duration)

# 黄色
def Yellow(duration):
  pwmR.ChangeDutyCycle(100)
  pwmG.ChangeDutyCycle(100)
  pwmB.ChangeDutyCycle(0)
  time.sleep(duration)

# 绿色
def Green(duration):
  pwmR.ChangeDutyCycle(0)
  pwmG.ChangeDutyCycle(100)
  pwmB.ChangeDutyCycle(0)
  time.sleep(duration)

# 红灯设置为10秒
redRenum = 10
while redRenum >= 1:
    Red(1)
    print(redRenum)
    redRenum -= 1

# 黄灯设置为3秒
yellowRenum = 3
while yellowRenum >= 1:
    Yellow(1)
    print(yellowRenum)
    yellowRenum -= 1

# 绿灯设置为10秒
greenRenum = 10
while greenRenum >= 1:
    Green(1)
    print(greenRenum)
    greenRenum -= 1

pwmR.stop()
pwmG.stop()
pwmB.stop()
GPIO.cleanup()

打印更多信息

#!/usr/bin/env python
# encoding: utf-8

import RPi.GPIO as GPIO
import time

R_PIN = 11
G_PIN = 12
B_PIN = 36
pins = (R_PIN, G_PIN, B_PIN)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pins, GPIO.OUT)
GPIO.output(pins, GPIO.LOW)

pwmR = GPIO.PWM(R_PIN, 70)
pwmG = GPIO.PWM(G_PIN, 70)
pwmB = GPIO.PWM(B_PIN, 70)
pwmR.start(0)
pwmG.start(0)
pwmB.start(0)

# 红色
def Red(duration):
  pwmR.ChangeDutyCycle(100)
  pwmG.ChangeDutyCycle(0)
  pwmB.ChangeDutyCycle(0)
  time.sleep(duration)

# 黄色
def Yellow(duration):
  pwmR.ChangeDutyCycle(100)
  pwmG.ChangeDutyCycle(100)
  pwmB.ChangeDutyCycle(0)
  time.sleep(duration)

# 绿色
def Green(duration):
  pwmR.ChangeDutyCycle(0)
  pwmG.ChangeDutyCycle(100)
  pwmB.ChangeDutyCycle(0)
  time.sleep(duration)

# 红灯设置为10秒
redRenum = 10
while redRenum >= 1:
    Red(1)
        message= "红灯还剩余 " + str(redRenum) + " 秒。"
    print(message)
    redRenum -= 1

# 黄灯设置为3秒
yellowRenum = 3
while yellowRenum >= 1:
    Yellow(1)
        message= "黄灯还剩余 " + str(yellowRenum) + " 秒。"
    print(message)
    yellowRenum -= 1

# 绿灯设置为10秒
greenRenum = 10
while greenRenum >= 1:
    Green(1)
        message= "绿灯还剩余 " + str(greenRenum) + " 秒。"
    print(message)
    greenRenum -= 1

pwmR.stop()
pwmG.stop()
pwmB.stop()
GPIO.cleanup()

循环缩进和无限循环

#!/usr/bin/env python
# encoding: utf-8

import RPi.GPIO as GPIO
import time

R_PIN = 11
G_PIN = 12
B_PIN = 36
pins = (R_PIN, G_PIN, B_PIN)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pins, GPIO.OUT)
GPIO.output(pins, GPIO.LOW)

pwmR = GPIO.PWM(R_PIN, 70)
pwmG = GPIO.PWM(G_PIN, 70)
pwmB = GPIO.PWM(B_PIN, 70)
pwmR.start(0)
pwmG.start(0)
pwmB.start(0)

# 红色
def Red(duration):
  pwmR.ChangeDutyCycle(100)
  pwmG.ChangeDutyCycle(0)
  pwmB.ChangeDutyCycle(0)
  time.sleep(duration)

# 黄色
def Yellow(duration):
  pwmR.ChangeDutyCycle(100)
  pwmG.ChangeDutyCycle(100)
  pwmB.ChangeDutyCycle(0)
  time.sleep(duration)

# 绿色
def Green(duration):
  pwmR.ChangeDutyCycle(0)
  pwmG.ChangeDutyCycle(100)
  pwmB.ChangeDutyCycle(0)
  time.sleep(duration)

# 错误的循环开始
errorLoop = 1
while errorLoop >= 0:

    # 红灯设置为10秒
    redRenum = 10
    while redRenum >= 1:
    Red(1)
        message= "红灯还剩余 " + str(redRenum) + " 秒。"
    print(message)
    redRenum -= 1

    # 黄灯设置为3秒
    yellowRenum = 3
    while yellowRenum >= 1:
    Yellow(1)
        message= "黄灯还剩余 " + str(yellowRenum) + " 秒。"
    print(message)
    yellowRenum -= 1

    # 绿灯设置为10秒
    greenRenum = 10
    while greenRenum >= 1:
    Green(1)
        message= "绿灯还剩余 " + str(greenRenum) + " 秒。"
    print(message)
    greenRenum -= 1

    errorLoop +=  1

    # 错误的循环结束

pwmR.stop()
pwmG.stop()
pwmB.stop()
GPIO.cleanup()
树莓派

树莓派 40Pin 引脚对照表

2019-11-14 21:48:04

树莓派

树莓派从入门到送人——继电器控制双色LED灯

2019-12-2 17:29:06

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
今日签到
有新私信 私信列表
搜索