在Arduino上测试一块增强长距红外线IR收发模块

几年前做了用18DS20bi测室温,用红外线IR发射模块发射空调遥控器的红外编码,控制空调开,关,升温,降温等动作,并用ESP8266把室温上传物联网的Ardunio的小项目。测室温,上传物联网这些功能都比较满意。但是IR发射模块因为是用Ardunio的端口直接驱动,IR发射管的,这样IR发射模块发射IR信号太弱,IR发射模块要距离空调很近,空调才能接收信号。远了就不行了。同时红外IR收发是两块模块,接电源要接两组,这也比较麻烦。最近搞到了一块红外IR收发一体的,带放大发射红外功能的模块。测试了一下确实IR发射模块离空调,电视很远,空调,电视也能接收到遥控信号。现把测试过程记一下。
测试用的是Arduino,主要是它的库比较多,STM32,C51都可以的。
1.先接线
模块 arduino
VCC 5v
GND GND
TX PIN 3
RX PIN 11
如下图:

2.下载并安装Arduino的IRremote的库。
就用IRremote的实例来测试,
先测接收IR部分,把遥控器对准模块的IR接收头,用IRremote库的IRrecvDump程序,接收遥控器发射的IR源码,制式16进制编码。
IRrecvDump程序如下:

#include <IRremote.h>

int RECV_PIN = 11;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}

// Dumps out the decode_results structure.
// Call this after IRrecv::decode()
// void * to work around compiler issue
//void dump(void *v) {
// decode_results *results = (decode_results *)v
void dump(decode_results *results) {
int count = results->rawlen;
if (results->decode_type == UNKNOWN) {
Serial.print("Unknown encoding: ");
}
else if (results->decode_type == NEC) {
Serial.print("Decoded NEC: ");
}
else if (results->decode_type == SONY) {
Serial.print("Decoded SONY: ");
}
else if (results->decode_type == RC5) {
Serial.print("Decoded RC5: ");
}
else if (results->decode_type == RC6) {
Serial.print("Decoded RC6: ");
}
else if (results->decode_type == PANASONIC) {
Serial.print("Decoded PANASONIC - Address: “);
Serial.print(results->address,HEX);
Serial.print(” Value: ");
}
else if (results->decode_type == LG) {
Serial.print("Decoded LG: ");
}
else if (results->decode_type == JVC) {
Serial.print("Decoded JVC: ");

}
else if (results->decode_type == AIWA_RC_T501) {
Serial.print(“Decoded AIWA RC T501: “);
}
else if (results->decode_type == WHYNTER) {
Serial.print(“Decoded Whynter: “);
}
Serial.print(results->value, HEX);
Serial.print(” (”);
Serial.print(results->bits, DEC);
Serial.println(” bits)”);
Serial.print(“Raw (”);
Serial.print(count, DEC);
Serial.print("): ");

for (int i = 0; i < count; i ) {
if ((i % 2) == 1) {
Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
}
else {
Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
}
Serial.print(" “);
}
Serial.println(”");
}

void loop() {
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
dump(&results);
irrecv.resume(); // Receive the next value
}
}

接收到的IR编码如下:

再测发射IR部分:
如果是NEC制式遥控机,用下面程序段即可:

 #include <IRremote.h>  IRsend irsend; for (int i = 0; i < 3; i  ) {  irsend.sendNEC(0xaf5e817, 32);  delay(40);

其它制式遥控机的也一样,只要改一下 irsend.sendNEC(0xaf5e817, 32)就可。

 如果是非制式遥控机,就要发射IR源码,用下面程序段即可 if (mode == SENDER) {Serial.println(label);irsend.sendRaw(rawbuf, rawlen, 38 /* kHz */);delay(200);

}
rawbuf是放IR 源码的数组, rawlen是源码数组的长度。

来源:https://www.icode9.com/content-4-814251.html

(0)

相关推荐