HCIRNEC Arduino Library For Sending & Receiving IR Codes Using NEC Protocol

Useful guides, libraries, and example sketches to support our Arduino based products.
Post Reply
admin
Site Admin
Posts: 866
Joined: Sun Aug 05, 2012 4:02 pm

HCIRNEC Arduino Library For Sending & Receiving IR Codes Using NEC Protocol

Post by admin » Wed May 08, 2019 3:41 pm

[IMAGE TBA]




This Arduino compatible library adds the ability for an Arduino to send and receive infrared remote control codes using the NEC protocol. To receive IR commands the library requires the use of an IR receiver such as the following:

https://hobbycomponents.com/opto-electr ... r-receiver

For transmitting codes from an Arduino the library modulate the codes with a 38KHz carrier frequency meaning other than an IR LED no other hardware is needed.

In both cases the library uses software bit banging which means any spare digital pins can be used and also makes the library compatible with most Arduinos development boards.

This library is provided free to support the open source community. PLEASE SUPPORT HOBBY COMPONENTS so that we can continue to provide free content like this by purchasing items from our store - HOBBYCOMPONENTS.COM


You will need to download (please log in to download the library) and unzip this library to the Arduino development environments library area.

On Windows:
My Documents\Arduino\libraries\

On Mac:
Documents/Arduino/libraries/

Linux:
Usually found within the users home area under /Arduino/libraries/




Using the HCIRNEC library


To use the library just include the HCIRNEC.h header file and then create an instance of the library. E.g:
  1. #include "HCIRNEC.h"
  2. HCIRNEC HCIRNEC;


Before using the library it must first be initialised. To initialise the Tx part of the library (for sending IR codes) place the following line in the Setup() loop at the top of the sketch:

  1. HCIRNEC.initTX(IRTxPin, TxIdleState);

Where:

IRTxPin is the digital pin used to drive the IR LED

TxIdleState is a boolean value which specifies the idle state of the pin (high/low). Valid values are
true (pin will be high when not transmitting)
false (default value - pin will be low when not transmitting - use this option if driving the LED via a transistor))


To initialise the Rx part of the library (for receiving IR codes) place the following line in the Setup() loop at the top of the sketch:
  1. HCIRNEC.initRX(IRRxPin);

Where:

IRRxPin is the digital pin used to connect to the data output pin of the IR receiver.




HCIRNEC library functions:


  1. byte HCIRNEC.newCode(void);

Checks the receiver for a new code.

Returns:

NEWCODE if a new IR code has been received.
REPEATCODE if an IR repeat code has been received.
NOCODE if no new code has been received
RECEIVEERROR if an invalid code has been received.

Note: this function must be continuously run in your main loop to receive new codes.


If a valid code has been received (NEWCODE) the new address and code can obtained using the following commands:

  1. byte address = HCIRNEC.address;
  2. byte command = HCIRNEC.command;


  1. void HCIRNEC.sendCode(address, command)

Sends a full IR remote code (address + command) where:

address is a byte value which contains the address of the receiving device.
command is a byte value containing the IR code to send to the receiving device.




Example Tx Sketch:

  1. /* FILE:    HCIRNEC_Transmit_Example
  2.    DATE:    07/05/19
  3.    VERSION: 1.0
  4.    AUTHOR:  Andrew Davies
  5.    BY:      HobbyComponents.com
  6.    
  7. 07/05/19 version 1.0: Original version
  8.  
  9. This example sketch demonstrates how to use the HCIRNEC library to transmit
  10. IR codes to an NEC compatible device. To use this example sketch you
  11. will need connect and IR LED (via a resistor to pin3 as follows:
  12.  
  13. IR LED.....................Arduino
  14. Anode (via resistor).......5V
  15. Cathode....................Digital pin 3
  16.  
  17. This library is provided free to support the open source community.
  18. PLEASE SUPPORT HOBBY COMPONENTS so that we can continue to provide free content
  19. like this by purchasing items from our store - HOBBYCOMPONENTS.COM
  20.  
  21.  
  22. You may copy, alter and reuse this code in any way you like, but please leave
  23. reference to HobbyComponents.com in your comments if you redistribute this code.
  24. This software may not be used directly for the purpose of selling products that
  25. directly compete with Hobby Components Ltd's own range of products.
  26. THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
  27. EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
  28. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
  29. HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
  30. INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
  31. REASON WHATSOEVER.
  32. */
  33.  
  34.  
  35. // Include the library
  36. #include "HCIRNEC.h"
  37.  
  38. //Digital pin connected to the IR LED.
  39. #define IRTXPIN 3
  40.  
  41. //Address and command codes to be sent
  42. #define ADDRESS 0x20
  43. #define CODE    0x5C
  44.  
  45.  
  46. // Create an instance of the library
  47. HCIRNEC HCIRNEC;
  48.  
  49.  
  50. void setup()
  51. {
  52.   // Initialise the library to send Tx codes
  53.   // TX_IDLE_HIGH = pin will go high when not transmitting
  54.   // TX_IDLE_LOW  = pin will go low when not transmitting - use when driving the LED via a transistor
  55.  
  56.   HCIRNEC.initTX(IRTXPIN, TX_IDLE_HIGH);
  57. }
  58.  
  59.  
  60. void loop()
  61. {
  62.   //Send the address and command bytes once every 2 seconds
  63.   HCIRNEC.sendCode(ADDRESS, CODE);
  64.   delay(2000);
  65. }



Example Rx Sketch:

  1. /* FILE:    HCIRNEC_Receive_Example.h
  2.    DATE:    07/05/19
  3.    VERSION: 1.0
  4.    AUTHOR:  Andrew Davies
  5.    BY:      HobbyComponents.com
  6.    
  7. 07/05/19 version 1.0: Original version
  8.  
  9. This example sketch demonstrates how to use the HCIRNEC library to receive
  10. IR codes from an NEC compatible remote control. To use this example sketch you
  11. will need the following IR receiver:
  12.  
  13. https://hobbycomponents.com/opto-electronics/463-1838b-infrared-ir-receiver
  14.  
  15. Connect the receiver to your Arduino as follows:
  16.  
  17. 1838B.......Arduino
  18. OUT........Digital pin 2
  19. GND........GND
  20. VCC........5V for 5V Arduinos (Uno/Nano etc) or 3.3V for 3.3V Arduinos (Due)
  21.  
  22.  
  23. This library is provided free to support the open source community.
  24. PLEASE SUPPORT HOBBY COMPONENTS so that we can continue to provide free content
  25. like this by purchasing items from our store -
  26.  
  27. HOBBYCOMPONENTS.COM
  28.  
  29.  
  30. You may copy, alter and reuse this code in any way you like, but please leave
  31. reference to HobbyComponents.com in your comments if you redistribute this code.
  32. This software may not be used directly for the purpose of selling products that
  33. directly compete with Hobby Components Ltd's own range of products.
  34. THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
  35. EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
  36. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
  37. HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
  38. INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
  39. REASON WHATSOEVER.
  40. */
  41.  
  42.  
  43. // Include the library
  44. #include "HCIRNEC.h"
  45.  
  46. //Digital pin connected to the OUT pin of the 1838B
  47. #define IRRXPIN 2
  48.  
  49. // Create an instance of the library
  50. HCIRNEC HCIRNEC;
  51.  
  52.  
  53. void setup()
  54. {
  55.   Serial.begin(9600);
  56.  
  57.   // Initialise the library to receive Rx codes
  58.   HCIRNEC.initRX(IRRXPIN);
  59. }
  60.  
  61.  
  62. void loop()
  63. {
  64.   // Check for a new code
  65.   byte state = HCIRNEC.newCode();
  66.  
  67.   switch(state)
  68.   {
  69.     case(NEWCODE):  // A new code has been received
  70.       Serial.print("New code: Address = 0x");
  71.       Serial.print(HCIRNEC.address, HEX);
  72.       Serial.print(" code = 0x");
  73.       Serial.println(HCIRNEC.command, HEX);
  74.       break;
  75.  
  76.     case(REPEATCODE): // A repeat code has been received
  77.       Serial.println("Repeat code received");
  78.       break;
  79.  
  80.     case(RECEIVEERROR): //An invalid code had been received
  81.       Serial.println("Invalid code received");
  82.       break;
  83.   }
  84. }
  85.  





Image

LATEST VERSION 1.1
HCIRNEC_V1_1.zip

OLD VERSION 1.0
HCIRNEC.zip


Disclaimer: Libraries, example code, and diagrams within this forum thread are provided as an additional free service by Hobby Components and are not sold as part of any product. We do not provide any guarantees or warranties as to their accuracy or fitness for purpose.

Descriptions and diagrams on this page are copyright Hobby Components Ltd and may not be reproduced without permission.
You do not have the required permissions to view the files attached to this post.

Post Reply

Return to “Arduino”