找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6356|回复: 1
收起左侧

Arduino红外接收模块源程序+电路

[复制链接]
ID:470429 发表于 2019-1-19 10:18 | 显示全部楼层 |阅读模式
内涵原理图和源码,欢迎下载学习。

在使用前一定要记得给红外遥控器上电池,还有红外遥控器要结合红外接收模块使用,它负责将接收红外遥控器发射过来的信息并将其解码成十六进制码,这样才能实现既定的通信。
将红外接收模块与 Arduino正确连接,其中S 连接D11,VCC 接+5V,GND接 GND,并将其固定好;

与 Arduino 具体的电路原理图如下:
0.png 0.png

Arduino源程序如下:
  1. /*
  2. * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv
  3. * An IR detector/demodulator must be connected to the input RECV_PIN.
  4. * Version 0.1 July, 2009
  5. * Copyright 2009 Ken Shirriff
  6. * http://arcfn.com
  7. */

  8. #include <IRremote.h>

  9. int RECV_PIN = 11;

  10. int LED1 = 2;
  11. int LED2 = 3;
  12. int LED3 = 4;
  13. int LED4 = 5;
  14. int LED5 = 6;
  15. int LED6 = 7;
  16. long on1  = 0x00FFA25D;
  17. long off1 = 0x00FFE01F;
  18. long on2 = 0x00FF629D;
  19. long off2 = 0x00FFA857;
  20. long on3 = 0x00FFE21D;
  21. long off3 = 0x00FF906F;
  22. long on4 = 0x00FF22DD;
  23. long off4 = 0x00FF6897;
  24. long on5 = 0x00FF02FD;
  25. long off5 = 0x00FF9867;
  26. long on6 = 0x00FFC23D;
  27. long off6 = 0x00FFB047;
  28. long all_on = 0x00FF30CF;
  29. long all_off = 0x00FF18E7;

  30. IRrecv irrecv(RECV_PIN);
  31. decode_results results;

  32. // Dumps out the decode_results structure.
  33. // Call this after IRrecv::decode()
  34. // void * to work around compiler issue
  35. //void dump(void *v) {
  36. //  decode_results *results = (decode_results *)v
  37. void dump(decode_results *results) {
  38.   int count = results->rawlen;
  39.   if (results->decode_type == UNKNOWN)
  40.     {
  41.      Serial.println("Could not decode message");
  42.     }
  43.   else
  44.    {
  45.     if (results->decode_type == NEC)
  46.       {
  47.        Serial.print("Decoded NEC: ");
  48.       }
  49.     else if (results->decode_type == SONY)
  50.       {
  51.        Serial.print("Decoded SONY: ");
  52.       }
  53.     else if (results->decode_type == RC5)
  54.       {
  55.        Serial.print("Decoded RC5: ");
  56.       }
  57.     else if (results->decode_type == RC6)
  58.       {
  59.        Serial.print("Decoded RC6: ");
  60.       }
  61.      Serial.print(results->value, HEX);
  62.      Serial.print(" (");
  63.      Serial.print(results->bits, DEC);
  64.      Serial.println(" bits)");
  65.    }
  66.      Serial.print("Raw (");
  67.      Serial.print(count, DEC);
  68.      Serial.print("): ");

  69.   for (int i = 0; i < count; i++)
  70.      {
  71.       if ((i % 2) == 1) {
  72.       Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
  73.      }
  74.     else  
  75.      {
  76.       Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
  77.      }
  78.     Serial.print(" ");
  79.      }
  80.       Serial.println("");
  81.      }

  82. void setup()
  83. {
  84.   pinMode(RECV_PIN, INPUT);   
  85.   pinMode(LED1, OUTPUT);
  86.   pinMode(LED2, OUTPUT);
  87.   pinMode(LED3, OUTPUT);
  88.   pinMode(LED4, OUTPUT);
  89.   pinMode(LED5, OUTPUT);
  90.   pinMode(LED6, OUTPUT);  
  91.   pinMode(13, OUTPUT);
  92.   Serial.begin(9600);
  93.   
  94.   irrecv.enableIRIn(); // Start the receiver
  95. }

  96. int on = 0;
  97. unsigned long last = millis();

  98. void loop()
  99. {
  100.   if (irrecv.decode(&results))
  101.    {
  102.     // If it's been at least 1/4 second since the last
  103.     // IR received, toggle the relay
  104.     if (millis() - last > 250)
  105.       {
  106.        on = !on;
  107. //       digitalWrite(8, on ? HIGH : LOW);
  108.        digitalWrite(13, on ? HIGH : LOW);
  109.        dump(&results);
  110.       }
  111.     if (results.value == on1 )
  112.        digitalWrite(LED1, HIGH);
  113.     if (results.value == off1 )
  114.        digitalWrite(LED1, LOW);
  115.     if (results.value == on2 )
  116.        digitalWrite(LED2, HIGH);
  117.     if (results.value == off2 )
  118.        digitalWrite(LED2, LOW);
  119.     if (results.value == on3 )
  120.        digitalWrite(LED3, HIGH);
  121.     if (results.value == off3 )
  122.        digitalWrite(LED3, LOW);
  123.     if (results.value == on4 )
  124.        digitalWrite(LED4, HIGH);
  125.     if (results.value == off4 )
  126.        digitalWrite(LED4, LOW);
  127.     if (results.value == on5 )
  128.        digitalWrite(LED5, HIGH);
  129.     if (results.value == off5 )
  130.        digitalWrite(LED5, LOW);
  131.     if (results.value == on6 )
  132.        digitalWrite(LED6, HIGH);
  133.     if (results.value == off6 )
  134.        digitalWrite(LED6, LOW);
  135.     if (results.value == all_on )
  136.       {
  137.        digitalWrite(LED1, HIGH);
  138.        digitalWrite(LED2, HIGH);
  139.        digitalWrite(LED3, HIGH);
  140.        digitalWrite(LED4, HIGH);
  141.        digitalWrite(LED5, HIGH);
  142.        digitalWrite(LED6, HIGH);
  143.       }      
  144.     if (results.value == all_off )
  145.       {
  146.        digitalWrite(LED1, LOW);
  147.        digitalWrite(LED2, LOW);
  148.        digitalWrite(LED3, LOW);
  149.        digitalWrite(LED4, LOW);
  150.        digitalWrite(LED5, LOW);
  151.        digitalWrite(LED6, LOW);
  152.       }      
  153.     last = millis();      
  154.     irrecv.resume(); // Receive the next value
  155.   }
  156. }
复制代码

所有资料51hei提供下载:
红外接收模块.rar (2.04 MB, 下载次数: 34)
回复

使用道具 举报

ID:813417 发表于 2020-8-15 17:08 | 显示全部楼层
感谢楼主分享,当然有注释更好啦
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表