树莓派学习基础实验————双色LED灯实验

一、实验接线图

接线图

二、案例程序

c语言

#include <wiringPi.h>
#include <softPwm.h>
#include <stdio.h>

#define uchar unsigned char

#define LedPinRed    0
#define LedPinGreen  1

void ledInit(void)
{
	softPwmCreate(LedPinRed,  0, 100);
	softPwmCreate(LedPinGreen,0, 100);
}

void ledColorSet(uchar r_val, uchar g_val)
{
	softPwmWrite(LedPinRed,   r_val);
	softPwmWrite(LedPinGreen, g_val);
}

int main(void)
{
	int i;

	if(wiringPiSetup() == -1){ //when initialize wiring failed,print messageto screen
		printf("setup wiringPi failed !");
		return 1; 
	}
	//printf("linker LedPin : GPIO %d(wiringPi pin)\n",LedPin); //when initialize wiring successfully,print message to screen

	ledInit();

	while(1){
		ledColorSet(0xff,0x00);   //red	
		delay(500);
		ledColorSet(0x00,0xff);   //green
		delay(500);
		ledColorSet(0xff,0x45);	
		delay(500);
		ledColorSet(0xff,0xff);	
		delay(500);
		ledColorSet(0x7c,0xfc);	
		delay(500);
	}

	return 0;
}

python

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time

colors = [0xFF00, 0x00FF, 0x0FF0, 0xF00F]
pins = {'pin_R':11, 'pin_G':12}  # pins is a dict

GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
for i in pins:
	GPIO.setup(pins[i], GPIO.OUT)   # Set pins' mode is output
	GPIO.output(pins[i], GPIO.HIGH) # Set pins to high(+3.3V) to off led

p_R = GPIO.PWM(pins['pin_R'], 2000)  # set Frequece to 2KHz
p_G = GPIO.PWM(pins['pin_G'], 2000)

p_R.start(0)      # Initial duty Cycle = 0(leds off)
p_G.start(0)

def map(x, in_min, in_max, out_min, out_max):
	return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min

def setColor(col):   # For example : col = 0x112233
	R_val = (col & 0x1100) >> 8
	G_val = (col & 0x0011) >> 0
	
	R_val = map(R_val, 0, 255, 0, 100)
	G_val = map(G_val, 0, 255, 0, 100)
	
	p_R.ChangeDutyCycle(R_val)     # Change duty cycle
	p_G.ChangeDutyCycle(G_val)

def loop():
	while True:
		for col in colors:
			setColor(col)
			time.sleep(0.5)

def destroy():
	p_R.stop()
	p_G.stop()
	for i in pins:
		GPIO.output(pins[i], GPIO.HIGH)    # Turn off all leds
	GPIO.cleanup()

if __name__ == "__main__":
	try:
		loop()
	except KeyboardInterrupt:
		destroy()

三、实验成功

试验成功图

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你好!关于树莓派实验双色LED,你需要准备以下材料和步骤: 材料: 1. 树莓派(任意型号) 2. 双色LED 3. 面包板 4. 杜邦线(公对公和公对母) 步骤: 1. 将双色LED插入面包板中,确保正极(长脚)和负极(短脚)分别连接到面包板的不同行。 2. 使用杜邦线将树莓派的GPIO引脚与双色LED连接起来。具体连接方式如下: - 将树莓派的GPIO 17引脚连接到双色LED的正极。 - 将树莓派的GPIO 27引脚连接到双色LED的负极。 3. 确保树莓派已连接到电源,并启动。 4. 在树莓派上运行Python编程环境,并创建一个新的Python文件。 5. 在Python文件中使用RPi.GPIO库控制GPIO引脚。如果你还没有安装RPi.GPIO库,可以使用以下命令进行安装: ``` sudo pip install RPi.GPIO ``` 6. 在Python文件中编写代码以控制GPIO引脚来控制双色LED。以下是一个示例代码: ```python import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.OUT) GPIO.setup(27, GPIO.OUT) def red_on(): GPIO.output(17, GPIO.HIGH) GPIO.output(27, GPIO.LOW) def green_on(): GPIO.output(17, GPIO.LOW) GPIO.output(27, GPIO.HIGH) def yellow_on(): GPIO.output(17, GPIO.HIGH) GPIO.output(27, GPIO.HIGH) def all_off(): GPIO.output(17, GPIO.LOW) GPIO.output(27, GPIO.LOW) try: while True: red_on() time.sleep(1) green_on() time.sleep(1) yellow_on() time.sleep(1) all_off() time.sleep(1) except KeyboardInterrupt: GPIO.cleanup() ``` 这段代码会使双色LED在红色、绿色和黄色之间交替闪烁。 7. 保存并运行Python文件,观察双色LED的状态变化。 希望以上步骤对你有所帮助!如果还有其他问题,请随时提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值