注:本文只做记录,参考。缺乏详细记录,但有疑问可以留言。
RGB LED模块可以发出各种颜色的光。红色,绿色和蓝色的三个LED被封装到透明或半透明塑料外壳中,并带有四个引脚。红色,绿色和蓝色三原色可以按照亮度混合并组合各种颜色,因此可以通过控制电路使RGB LED发出彩色光。
接线:
将树莓派通过T型转接板连接到面包板。
树莓派GPIO 11,“红白线”连接RGB LED模块R端子;
树莓派GPIO 12,“绿白线”连接RGB LED模块G端子;
树莓派GPIO 13 ,“蓝白线”连接RGB LED模块B端子;
树莓派GND,“黑线”连接RGB LED模块GND端子。
效果图:
C语言程序:
#include <wiringPi.h>
#include <softPwm.h>
#include <stdio.h>
#define uchar unsigned char
#define makerobo_Led_PinRed 0 // 红色LED 管脚
#define makerobo_Led_PinGreen 1 // 绿色LED 管脚
#define makerobo_Led_PinBlue 2 // 蓝色LED 管脚
// LED 初始化
void makerobo_led_Init(void)
{
softPwmCreate(makerobo_Led_PinRed, 0, 100);
softPwmCreate(makerobo_Led_PinGreen,0, 100);
softPwmCreate(makerobo_Led_PinBlue, 0, 100);
}
// LED 颜色设置
void makerobo_led_Color_Set(uchar r_val, uchar g_val, uchar b_val)
{
softPwmWrite(makerobo_Led_PinRed, r_val);
softPwmWrite(makerobo_Led_PinGreen, g_val);
softPwmWrite(makerobo_Led_PinBlue, b_val);
}
//-------------主程序-----------------
int main(void)
{
//初始化连接失败时,将消息打印到屏幕
if(wiringPiSetup() == -1){
printf("setup wiringPi failed !");
return 1;
}
makerobo_led_Init();
while(1)
{
makerobo_led_Color_Set(0xff,0x00,0x00); // 红色
delay(500); // 延时500ms
makerobo_led_Color_Set(0x00,0xff,0x00); // 绿色
delay(500); // 延时500ms
makerobo_led_Color_Set(0x00,0x00,0xff); // 蓝色
delay(500);
makerobo_led_Color_Set(0xff,0xff,0x00); //黄色
delay(500); // 延时500ms
makerobo_led_Color_Set(0xff,0x00,0xff); //pick
delay(500); // 延时500ms
makerobo_led_Color_Set(0xc0,0xff,0x3e);
delay(500); // 延时500ms
makerobo_led_Color_Set(0x94,0x00,0xd3);
delay(500);
makerobo_led_Color_Set(0x76,0xee,0x00);
delay(500);
makerobo_led_Color_Set(0x00,0xc5,0xcd);
delay(500);
}
return 0;
}