zoukankan      html  css  js  c++  java
  • 树莓派+RGB彩灯实现跑马灯

    RGB彩灯跟普通的LED不同,它可以根据RGB的色值自定义颜色,从而轻松的实现跑马灯的效果,我们选用HW-478 3色全彩LED smd模块来实现。

    一、硬件接线
    RGB彩灯硬件接线比较简单,负极接GND,R/G/B分别接树莓派定义的针脚即可。

    二、软件实现:

    import RPi.GPIO
    import time
    
    R,G,B = 18,15,14
    
    RPi.GPIO.setmode(RPi.GPIO.BCM)
    
    RPi.GPIO.setup(R,RPi.GPIO.OUT)
    RPi.GPIO.setup(G,RPi.GPIO.OUT)
    RPi.GPIO.setup(B,RPi.GPIO.OUT)
    
    pwmR = RPi.GPIO.PWM(R,50)
    pwmG = RPi.GPIO.PWM(G,50)
    pwmB = RPi.GPIO.PWM(B,50)
    
    pwmR.start(0)
    pwmG.start(0)
    pwmB.start(0)
    
    t = 1
    #no light on 
    pwmR.ChangeDutyCycle(0)
    pwmG.ChangeDutyCycle(0)
    pwmB.ChangeDutyCycle(0)
    time.sleep(t)
    
    try:
        while(True):
            pwmR.ChangeDutyCycle(100)
            pwmG.ChangeDutyCycle(0)
            pwmB.ChangeDutyCycle(0)
            time.sleep(t)
            
            pwmR.ChangeDutyCycle(0)
            pwmG.ChangeDutyCycle(100)
            pwmB.ChangeDutyCycle(0)
            time.sleep(t)
            
            pwmR.ChangeDutyCycle(0)
            pwmG.ChangeDutyCycle(0)
            pwmB.ChangeDutyCycle(100)
            time.sleep(t)
            
            pwmR.ChangeDutyCycle(100)
            pwmG.ChangeDutyCycle(100)
            pwmB.ChangeDutyCycle(0)
            time.sleep(t)
            
            pwmR.ChangeDutyCycle(100)
            pwmG.ChangeDutyCycle(0)
            pwmB.ChangeDutyCycle(100)
            time.sleep(t)
            
            pwmR.ChangeDutyCycle(0)
            pwmG.ChangeDutyCycle(100)
            pwmB.ChangeDutyCycle(100)
            time.sleep(t)
            
            pwmR.ChangeDutyCycle(100)
            pwmG.ChangeDutyCycle(100)
            pwmB.ChangeDutyCycle(100)
            time.sleep(t)
            
            pwmR.ChangeDutyCycle(0)
            pwmG.ChangeDutyCycle(0)
            pwmB.ChangeDutyCycle(0)
            time.sleep(t)
            
    except keyboardInterrupt:
        pass
    
    pwmR.stop()
    pwmG.stop()
    pwmB.stop()
    
    RPi.GPIO.cleanup()
            

    参考资料:
    https://shumeipai.nxez.com/2014/11/13/rpi-gpio-module-pwm-basic-function.html

  • 相关阅读:
    自学python:python学习笔记之Ubuntu 16.04网络的配置
    Python 3 中字符串和 bytes 的区别
    Python map学习笔记
    Python lambda 知识点
    Eclipse中Activiti插件的安装
    Activiti简介
    Jquery ajax回调函数不执行
    Spring Boot安装及入门实现
    mysql给root开启远程访问权限
    linux下安装Tomcat
  • 原文地址:https://www.cnblogs.com/guwei4037/p/14366091.html
Copyright © 2011-2022 走看看