掘金 人工智能 07月23日 11:07
Arduino Uno 接收红外遥控
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文档提供了一个使用Arduino Uno控制红外模块的完整示例。通过连接红外接收器到Arduino的数字引脚2,并利用IRremote库,可以读取和解析来自红外遥控器的信号。程序会通过串口监视器显示接收到的协议类型、地址、命令码以及原始数据,并根据不同的按键命令执行相应操作。示例代码详细展示了如何初始化接收器、解码数据以及处理重复信号,为DIY红外控制项目提供了基础。

📱 **硬件连接与库安装**:将红外接收器的VCC、GND和OUT引脚分别连接到Arduino Uno的5V、GND和数字引脚2。使用前需要通过Arduino IDE安装IRremote库,这是解析红外信号的关键。

📡 **程序初始化与协议识别**:在`setup()`函数中,通过`Serial.begin()`初始化串口通信,并使用`IrReceiver.begin()`函数启动红外接收器,指定连接引脚并启用LED反馈。程序还会打印出当前支持的IR协议类型,方便调试。

🔍 **红外信号接收与解码**:在`loop()`函数中,`IrReceiver.decode()`用于检测是否有红外信号被接收。如果接收到信号,程序会判断协议是否未知,并打印详细的原始数据信息;若协议已知,则会打印简短的接收摘要,包括协议、地址和命令码,并提供重发指令的示例。

💡 **按键命令处理与应用**:程序能够识别不同的红外遥控器按键,例如CH-、CH+、数字键等,并将其对应的命令码(如0x45、0xC、0x18等)打印出来。在`else if`语句块中,可以根据接收到的特定命令码实现自定义功能,如控制LED、电机或其他设备。

🔄 **重复信号处理**:当遥控器按键被长按时,会发送重复信号。程序通过检查`IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT`来识别重复信号,并可以据此实现相应的功能,例如重复执行之前的操作。

本文介绍使用Arduino Uno控制红外模块的示例程序。这个程序将读取红外接收器接收到的数据,并通过串口监视器显示出来,并打印各按键。

1.连接红外接收器到Arduino:

2.安装IRremote库。

3.代码

#include <Arduino.h>#include <IRremote.hpp> // Include the library// Define the pin connected to the IR receiver#define IR_RECEIVE_PIN 2void setup() {    Serial.begin(115200);    // Just to know which program is running on my Arduino    Serial.println(F("START " __FILE__ " from " __DATE__ "\r\nUsing library version " VERSION_IRREMOTE));    // Start the receiver and if not 3rd parameter specified, take LED_BUILTIN pin from the internal boards definition as default feedback LED    IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);    Serial.print(F("Ready to receive IR signals of protocols: "));    printActiveIRProtocols(&Serial);    // Serial.println(F("at pin " STR(IR_RECEIVE_PIN)));}void loop() {    /*     * Check if received data is available and if yes, try to decode it.     * Decoded result is in the IrReceiver.decodedIRData structure.     *     * E.g. command is in IrReceiver.decodedIRData.command     * address is in command is in IrReceiver.decodedIRData.address     * and up to 32 bit raw data in IrReceiver.decodedIRData.decodedRawData     */    if (IrReceiver.decode()) {        /*         * Print a summary of received data         */        if (IrReceiver.decodedIRData.protocol == UNKNOWN) {            Serial.println(F("Received noise or an unknown (or not yet enabled) protocol"));            // We have an unknown protocol here, print extended info            IrReceiver.printIRResultRawFormatted(&Serial, true);            IrReceiver.resume(); // Do it here, to preserve raw data for printing with printIRResultRawFormatted()        } else {            IrReceiver.resume(); // Early enable receiving of the next IR frame            IrReceiver.printIRResultShort(&Serial);            IrReceiver.printIRSendUsage(&Serial);        }        Serial.println();        /*         * Finally, check the received data and perform actions according to the received command         */        if (IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT) {            Serial.println(F("Repeat received. Here you can repeat the same action as before."));        } else {            if (IrReceiver.decodedIRData.command == 0x45) {                Serial.println(F("Received command 0x45 = Btn CH-"));                // do something            } else if (IrReceiver.decodedIRData.command == 0x46) {                Serial.println(F("Received command 0x46 = Btn CH"));                // do something else            } else if (IrReceiver.decodedIRData.command == 0x47) {                Serial.println(F("Received command 0x47 = Btn CH+"));                // do something            } else if (IrReceiver.decodedIRData.command == 0x44) {                Serial.println(F("Received command 0x44 = Btn <<"));                // do something else            } else if (IrReceiver.decodedIRData.command == 0x40) {                Serial.println(F("Received command 0x40 = Btn >>"));                // do something            } else if (IrReceiver.decodedIRData.command == 0x43) {                Serial.println(F("Received command 0x43 = Btn >II"));                // do something else            }else if (IrReceiver.decodedIRData.command == 0x7) {                Serial.println(F("Received command 0x7 = Btn -."));                // do something else            } else if (IrReceiver.decodedIRData.command == 0x15) {                Serial.println(F("Received command 0x15 = Btn +."));                // do something            } else if (IrReceiver.decodedIRData.command == 0x9) {                Serial.println(F("Received command 0x9 = Btn EQ."));                // do something else            }else if (IrReceiver.decodedIRData.command == 0x16) {                Serial.println(F("Received command 0x16 = Btn 0."));                // do something else            } else if (IrReceiver.decodedIRData.command == 0x19) {                Serial.println(F("Received command 0x19 = Btn 100+."));                // do something            } else if (IrReceiver.decodedIRData.command == 0xD) {                Serial.println(F("Received command 0xD = Btn 200+."));                // do something else            } else if (IrReceiver.decodedIRData.command == 0xC) {                Serial.println(F("Received command 0xC = Btn 1."));                // do something else            } else if (IrReceiver.decodedIRData.command == 0x18) {                Serial.println(F("Received command 0x18 = Btn 2."));                // do something            } else if (IrReceiver.decodedIRData.command == 0x5E) {                Serial.println(F("Received command 0x5E = Btn 3."));                // do something else            } else if (IrReceiver.decodedIRData.command == 0x8) {                Serial.println(F("Received command 0x8  = Btn 4."));                // do something else            } else if (IrReceiver.decodedIRData.command == 0x1C) {                Serial.println(F("Received command 0x1C = Btn 5."));                // do something            } else if (IrReceiver.decodedIRData.command == 0x5A) {                Serial.println(F("Received command 0x5A = Btn 6."));                // do something else            } else if (IrReceiver.decodedIRData.command == 0x42) {                Serial.println(F("Received command 0x42  = Btn 7."));                // do something else            } else if (IrReceiver.decodedIRData.command == 0x52) {                Serial.println(F("Received command 0x52 = Btn 8."));                // do something            } else if (IrReceiver.decodedIRData.command == 0x4A) {                Serial.println(F("Received command 0x4A = Btn 9."));                // do something else            }        }    }}

4.输出信息

Repeat received. Here you can repeat the same action as before.Protocol=NEC Address=0x0 Command=0xC Raw-Data=0xF30CFF00 32 bits LSB first Gap=499200us Duration=68750usSend with: IrSender.sendNEC(0x0, 0xC, <numberOfRepeats>);Received command 0xC = Btn 1.Protocol=NEC Address=0x0 Command=0xC Repeat Gap=39550us Duration=12200usRepeat received. Here you can repeat the same action as before.Protocol=NEC Address=0x0 Command=0x18 Raw-Data=0xE718FF00 32 bits LSB first Gap=424050us Duration=68800usSend with: IrSender.sendNEC(0x0, 0x18, <numberOfRepeats>);Received command 0x18 = Btn 2.Protocol=NEC Address=0x0 Command=0x18 Repeat Gap=39700us Duration=12250usRepeat received. Here you can repeat the same action as before.Protocol=NEC Address=0x0 Command=0x5E Raw-Data=0xA15EFF00 32 bits LSB first Gap=349000us Duration=68800usSend with: IrSender.sendNEC(0x0, 0x5E, <numberOfRepeats>);Received command 0x5E = Btn 3.Protocol=NEC Address=0x0 Command=0x5E Repeat Gap=39800us Duration=12250us

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

Arduino 红外接收 IRremote库 嵌入式开发 DIY项目
相关文章