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:
- #include "HCIRNEC.h"
- 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:
- 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:
- HCIRNEC.initRX(IRRxPin);
Where:
IRRxPin is the digital pin used to connect to the data output pin of the IR receiver.
HCIRNEC library functions:
- 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:
- byte address = HCIRNEC.address;
- byte command = HCIRNEC.command;
- 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:
- /* FILE: HCIRNEC_Transmit_Example
- DATE: 07/05/19
- VERSION: 1.0
- AUTHOR: Andrew Davies
- BY: HobbyComponents.com
- 07/05/19 version 1.0: Original version
- This example sketch demonstrates how to use the HCIRNEC library to transmit
- IR codes to an NEC compatible device. To use this example sketch you
- will need connect and IR LED (via a resistor to pin3 as follows:
- IR LED.....................Arduino
- Anode (via resistor).......5V
- Cathode....................Digital pin 3
- 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 may copy, alter and reuse this code in any way you like, but please leave
- reference to HobbyComponents.com in your comments if you redistribute this code.
- This software may not be used directly for the purpose of selling products that
- directly compete with Hobby Components Ltd's own range of products.
- THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
- EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
- MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
- HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
- INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
- REASON WHATSOEVER.
- */
- // Include the library
- #include "HCIRNEC.h"
- //Digital pin connected to the IR LED.
- #define IRTXPIN 3
- //Address and command codes to be sent
- #define ADDRESS 0x20
- #define CODE 0x5C
- // Create an instance of the library
- HCIRNEC HCIRNEC;
- void setup()
- {
- // Initialise the library to send Tx codes
- // TX_IDLE_HIGH = pin will go high when not transmitting
- // TX_IDLE_LOW = pin will go low when not transmitting - use when driving the LED via a transistor
- HCIRNEC.initTX(IRTXPIN, TX_IDLE_HIGH);
- }
- void loop()
- {
- //Send the address and command bytes once every 2 seconds
- HCIRNEC.sendCode(ADDRESS, CODE);
- delay(2000);
- }
Example Rx Sketch:
- /* FILE: HCIRNEC_Receive_Example.h
- DATE: 07/05/19
- VERSION: 1.0
- AUTHOR: Andrew Davies
- BY: HobbyComponents.com
- 07/05/19 version 1.0: Original version
- This example sketch demonstrates how to use the HCIRNEC library to receive
- IR codes from an NEC compatible remote control. To use this example sketch you
- will need the following IR receiver:
- https://hobbycomponents.com/opto-electronics/463-1838b-infrared-ir-receiver
- Connect the receiver to your Arduino as follows:
- 1838B.......Arduino
- OUT........Digital pin 2
- GND........GND
- VCC........5V for 5V Arduinos (Uno/Nano etc) or 3.3V for 3.3V Arduinos (Due)
- 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 may copy, alter and reuse this code in any way you like, but please leave
- reference to HobbyComponents.com in your comments if you redistribute this code.
- This software may not be used directly for the purpose of selling products that
- directly compete with Hobby Components Ltd's own range of products.
- THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
- EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
- MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
- HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
- INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
- REASON WHATSOEVER.
- */
- // Include the library
- #include "HCIRNEC.h"
- //Digital pin connected to the OUT pin of the 1838B
- #define IRRXPIN 2
- // Create an instance of the library
- HCIRNEC HCIRNEC;
- void setup()
- {
- Serial.begin(9600);
- // Initialise the library to receive Rx codes
- HCIRNEC.initRX(IRRXPIN);
- }
- void loop()
- {
- // Check for a new code
- byte state = HCIRNEC.newCode();
- switch(state)
- {
- case(NEWCODE): // A new code has been received
- Serial.print("New code: Address = 0x");
- Serial.print(HCIRNEC.address, HEX);
- Serial.print(" code = 0x");
- Serial.println(HCIRNEC.command, HEX);
- break;
- case(REPEATCODE): // A repeat code has been received
- Serial.println("Repeat code received");
- break;
- case(RECEIVEERROR): //An invalid code had been received
- Serial.println("Invalid code received");
- break;
- }
- }
LATEST VERSION 1.1
OLD VERSION 1.0
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.