树莓派基础实验22:红外遥控传感器实验

一、介绍

   红外接收头的主要功能为IC化的一种受光元件,其内部是将光电二极管(俗称接收管)和集成IC共同组合封装而成,其IC设计主要以类比式控制,一般主要接收38KHZ的频率的红外线,而对其他频率段的红外信号不敏感。这样,遥控器发出载波在38KHZ的频率,接收管接受遥控器发送过来的信息,从而构成通讯。


二、组件

★Raspberry Pi主板*1

★树莓派电源*1

★40P软排线*1

★红外接收模块*1

★红外遥控器模块*1

★RGB LED模块*1

★面包板*1

★跳线若干

三、实验原理

树莓派基础实验22:红外遥控传感器实验_第1张图片

树莓派基础实验22:红外遥控传感器实验_第2张图片

树莓派基础实验22:红外遥控传感器实验_第3张图片

  在本实验中,我们将使用PWM脉宽调制技术来控制RGB的亮度。详情可以查看前面的实验:树莓派基础实验2:RGB-LED实验。

  我们使用lirc库读取遥控器按钮返回的红外信号,并将它们转换为按钮值,然后使用pylirc来简化从远程控制中读取值的过程。在本实验中,使用遥控器顶部的9个按钮来控制RGB LED模块的颜色。每行代表一种颜色,每列代表亮度。

四、实验步骤

  第1步: 连接电路。

树莓派 T型转接板 红外接收器模块
GPIO4 G23 SIG
5V 5V VCC
GND GND GND
树莓派 T型转接板 RGB LED灯
GPIO0 G17 R
GPIO1 G18 G
GPIO2 G27 B
GND GND GND

树莓派基础实验22:红外遥控传感器实验_第4张图片

树莓派基础实验22:红外遥控传感器实验_第5张图片

  第2步: 安装lirc库,配置详情这里不作介绍。

sudo apt-get install python-pylirc

  检查该模块是否已加载,你应该看到“/dev/lirc0”。

ls /dev/li*

  然后使用“irw”命令测试,按遥控器上的按钮,看屏幕上是否打印按钮名称,如下图所示:
树莓派基础实验22:红外遥控传感器实验_第6张图片

python lirc模块,有关LIRC的更多信息,请参见http://www.lirc.org

lirc模块的函数及功能介绍如下:

Initialization
Before you can receive any commands from lirc, you’ll need to initialize the module. After importing pyLirc, call the pylirc.init() function:

import pylirc

integer = pylirc.init(string name[, string configuration [, integer blocking ]])

the returnvalue is the returnvalue of lircs client library
lirc_init(), ie a socket, or zero on failure.

The socket can be used with select.select() to wait for data if you don’t want to use blocking. This is especially useful in multithreaded programs as blocking mode of pylirc will block all threads, whereas select() will only block the current and with optional timeout.

name: the name used for your program in the lirc configuration file, must be supplied.

configuration: a filename to a lirc configuration file in case you wish not to use lircs default configuration file (usually ~/.lircrc).

blocking: a flag indicating whether you want blocking mode or not. See also blocking() and select.select() (latter in python docs)

Polling
If initialization was ok, you can poll lirc for commands. To read any commands in queue call pylirc.nextcode():

list = pylirc.nextcode([integer Exteneded])

The returnvalue is ‘None’, if no commands were in the queue, or a list containing the commands read.

To get the commands one by one enumerate the list:

for code in list:
print code

If you supply the optional argument Extended as true, code will be a dictionary otherwise it will be a string (old behaviour).

The dictionary currently contains:
“config”: The config string from lirc config file - the same string you’d get in non-extended mode.
“repeat”: The repeat count of the buttonpress.

Note, that there can still be more commands on queue after a call to pylirc.nextcode(). You should call it in a loop until you get ‘None’ back.

Exiting
When you’re done using pyLirc and before you exit your program you should clean up:

pylirc.exit()

Changing mode
When you initialize pyLirc, you can chose whether you want blocking or non-blocking mode. Blocking mode means pylirc.nextcode() waits until there is a command to be read until it returns.
To change mode after initialization, use blocking():

success = pylirc.blocking(int)

  第3步: 编写控制程序。遥控器上的前三行按钮中的每一行代表一种颜色,即从上到下一次控制红色、绿色和蓝色。每列代表关灯、亮和暗。例如,按第一行的第二个按钮,是控制红色灯亮。
  你可以使用遥控器共生产27种颜色,包括关闭所有led灯。

树莓派基础实验22:红外遥控传感器实验_第7张图片

#!/usr/bin/python

import pylirc, time
import RPi.GPIO as GPIO

Rpin = 17
Gpin = 18
Bpin = 27
blocking = 0;

Lv = [100, 20, 90] # Light Level
color = [100, 100, 100]  #默认100时,占空比,100-100=0,是关闭灯

def setColor(color):
#	global p_R, p_G, p_B
	#更改占空比
	p_R.ChangeDutyCycle(100 - color[0])     # color[0]为控制红灯的颜色
	p_G.ChangeDutyCycle(100 - color[1])		# color[1]为控制绿灯的颜色
	p_B.ChangeDutyCycle(100 - color[2])		# color[2]为控制蓝灯的颜色

def setup():
	global p_R, p_G, p_B
	GPIO.setmode(GPIO.BCM)
	GPIO.setup(Rpin, GPIO.OUT)
	GPIO.setup(Gpin, GPIO.OUT)
	GPIO.setup(Bpin, GPIO.OUT)
	
	p_R = GPIO.PWM(Rpin, 2000) # Set Frequece to 2KHz
	p_G = GPIO.PWM(Gpin, 2000)
	p_B = GPIO.PWM(Bpin, 2000)
	
	p_R.start(0)
	p_G.start(0)
	p_B.start(0)
	pylirc.init("pylirc", "./conf", blocking) #初始化lirc


def RGB(config):
	global color
	if config == 'KEY_CHANNELDOWN':
		color[0] = Lv[0]	#Lv[0]为=100,占空比为100-100=0,红灯灭
		print 'Red OFF'

	if config == 'KEY_CHANNEL':
		color[0] = Lv[1]	#Lv[1]为=20,占空比为100-20=80,红灯亮
		print 'Light Red'

	if config == 'KEY_CHANNELUP':
		color[0] = Lv[2]	#Lv[2]为=90,占空比为100-90=10,红灯暗
		print 'Red'

	if config == 'KEY_PREVIOUS':
		color[1] = Lv[0]	#Lv[0]为=100,占空比为100-100=0,绿灯灭
		print 'Green OFF'

	if config == 'KEY_NEXT':
		color[1] = Lv[1]	#Lv[1]为=20,占空比为100-20=80,绿灯亮
		print 'Light Green'

	if config == 'KEY_PLAYPAUSE':
		color[1] = Lv[2]	#Lv[2]为=90,占空比为100-90=10,绿灯暗
		print 'Green'

	if config == 'KEY_VOLUMEDOWN':
		color[2] = Lv[0]	#Lv[0]为=100,占空比为100-100=0,蓝灯灭
		print 'Blue OFF'

	if config == 'KEY_VOLUMEUP':
		color[2] = Lv[1]	#Lv[1]为=20,占空比为100-20=80,蓝灯亮
		print 'Light Blue'

	if config == 'KEY_EQUAL':
		color[2] = Lv[2]	#Lv[2]为=90,占空比为100-90=10,蓝灯暗
		print 'BLUE'

def loop():
	while True:
		s = pylirc.nextcode(1)
#如果初始化是ok的,您可以轮询lirc的命令。要读取队列中的任何命令,请调用pylirc.nextcode()
#如果队列中没有命令,或者列表中包含读取的命令,则returnvalue为'None'。
		while(s):
			for (code) in s:
#				print 'Command: ', code["config"] #For debug: Uncomment this
#				line to see the return value of buttons
				RGB(code["config"])
				setColor(color)
			if(not blocking):
				s = pylirc.nextcode(1)
			else:
				s = []

def destroy():
	p_R.stop()
	p_G.stop()
	p_B.stop()
	GPIO.output(Rpin, GPIO.LOW)    # Turn off all leds
	GPIO.output(Gpin, GPIO.LOW)
	GPIO.output(Bpin, GPIO.LOW)
	GPIO.cleanup()
	pylirc.exit()

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



  

你可能感兴趣的:(树莓派基础实验,raspberry,pi,python,物联网)