简介
IR(Infrared Remote)即红外遥控。每按下一个键,即产生具有不同编码的数字脉冲,这种代码指令信号通过调制激励红外光二级管产生具有脉冲串的红外波,通过空间的传送到受控机内的遥控接收器。在接收过程中,红外波信号通过光学滤波器和光电二极管转换为电信号,此信号经过放大、检波、整形、解调、送到解码与接口电路,从而完成相应的遥控功能。
IR的种类及编码
很多半导体公司推出了自己制定的编码方式和其专用的遥控发射芯片,根据厂家分类有PHILIPS码、SANYO码、TOSHIBA码、NEC码等,另外,还有根据芯片名称分类,有TC9012码、L7461码、M34280码等。
所有码的编码方式都是大同小异的,一般都由引导码,用户码和键数据码组成,引导码的作用是“引导”接收器开始接收数据,用户码的作用是用来区分不同的遥控器,让接收设备分辨是否为本机的遥控器,键数据码是用来区分不同的按键。不同编码方式的主要不同点在于引导码的长度和数据位的多少,以及表示“0”和“1”的脉冲的宽度。
硬件模块
模块接口很简单只有电源、GND、和一个数字引脚
Arduino ide中安装IRremote红外开发第三方库
主程序代码
主程序代码ir_test.ino(IRremote 4.0 + 版本)
#include <IRremote.hpp> // include the library
const int IR_RECEIVE_PIN = 12; //红外线接收器 接在 pin 12
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
// Start the receiver and if not 3. parameter specified, take LED_BUILTIN pin from the internal boards definition as default feedback LED
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
Serial.println("---------setup ok--------");
}
void loop() {
if (IrReceiver.decode()) { //如果收到信号
IrReceiver.printIRResultShort(&Serial); //显示基本的信息
Serial.println("print boundary...");
IrReceiver.printIRSendUsage(&Serial);
Serial.println("IrReceiver.decodedIRData.decodedRawData");
Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX);
Serial.println("IrReceiver.decodedIRData");
Serial.println(IrReceiver.decodedIRData.command, HEX);
IrReceiver.resume();
}
delay(100);
}
主程序代码ir_test.ino(IRremote 3.9 及以下版本)
测试发现部分版本不好用返回0,3.2 ~ 3.9版本可用,2.5版本可用,2.8始终返回0,使用时请注意版本可以多试几个版本
#include <IRremote.h>
const int IR_RECEIVE_PIN = 12; //红外线接收器 接在 pin 12
IRrecv irrecv(IR_RECEIVE_PIN); // 红外接收对象
decode_results results; // 红外解码结果
void setup() {
Serial.begin(9600);
irrecv.enableIRIn(); // 启动红外线解码
}
void loop() {
if (irrecv.decode(&results)) {
// 解码成功,收到码值
Serial.println(results.value, HEX);
irrecv.resume(); // 继续接收红外信号
}
delay(100);
}
注意:网上文章大多数使用的是IRremote3或IRremote2版本的旧代码,旧代码 + IRremote 4.0+版本库,可能会出现以下错误提示。请注意区分安装的IRremote版本和使用的实例代码版本
Thank you for using the IRremote library!
It seems, that you are using a old version 2.0 code / example.
This version is no longer supported!
Please use one of the new code examples from the library,
available at "File > Examples > Examples from Custom Libraries / IRremote".
Start with the SimpleReceiver or SimpleSender example.
The examples are documented here:
<https://github.com/Arduino-IRremote/Arduino-IRremote#examples-for-this-library>
A guide how to convert your 2.0 program is here:
<https://github.com/Arduino-IRremote/Arduino-IRremote#converting-your-2x-program-to-the-4x-version>
Thanks