二、树莓派RGB LED实验

注:本文只做记录,参考。缺乏详细记录,但有疑问可以留言。

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;
}

 

 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值