Note: ALL OSOYOO Products for Arduino are Third Party Board which is fully compatible with Arduino

Authorized Online Retailers:

AMAZON
amzuk

Content

  1. Introduction
  2. Preparations
  3. About the IR
  4. Examples

Introduction

IR, or infrared, communication is a common, inexpensive, and easy to use wireless communication technology. IR light is very similar to visible light, except that it has a slightlty longer wavelength. This means IR is undetectable to the human eye – perfect for wireless communication. For example, when you hit a button on your TV remote, an IR LED repeatedly turns on and off, 38,000 time a second, to transmit information (like volume or channel control) to an IR photo sensor on your TV.

This tutorial will first explain the inner workings of common IR communication protocols. Then we will go over three examples that will allow you to transmit and receive IR data using an Arduin. In the first example, we will read incoming IR data from a common remote control using the 1838B IR photo sensor. The next example will show you how to test the IR remote and print the corrsponding button name on the Serial Monitor. The third example will show you how to transmit data from an IR receiver to control a common appliance, such as turn on an LED.

Preparations

Hardware

Software

About the IR

What is IR?

Infra-Red light is actually normal light with a particular colour. We humans can’t see this colour because its wave length of about 950nm is below the visible spectrum. That’s one of the reasons why IR is chosen for remote control purposes, we want to use it but we’re not interested in seeing it. Another reason is because IR LEDs are quite easy to make, and therefore can be very cheap, thus making it ideal for us hobbyists to use IR control for our own projects.

We need to konw there are many more sources of Infra-Red light. The sun is the brightest source of all, but there are many others, like: light bulbs, candles, central heating system, and even our body radiates Infra-Red light.

A common modulation scheme for IR communication is something called 38kHz modulation. There are very few natural sources that have the regularity of a 38kHz signal, so an IR transmitter sending data at that frequency would stand out among the ambient IR. 38kHz modulated IR data is the most common, but other frequencies can be used.

When you hit a key on your remote, the transmitting IR LED will blink very quickly for a fraction of a second, transmitting encoded data to your appliance.

If you were to hook an oscilloscope up to your TV remote’s IR LED, you would see a signal similar to the one above. This modulated signal is exactly what the receiving system sees. However, the point of the receiving device is to demodulate the signal and output a binary waveform that can be read by a microcontroller. When you read the OUT pin of the VS1838B with the wave from above, you will see something like the second.

Modulation

As everything that radiates heat, also radiates Infra-Red light. Therefore we have to take some precautions to guarantee that our IR message gets across to the receiver without errors.Modulation of the signal on a carrier frequency is the answer to make our signal stand out above the noise. With modulation we make the IR light source blink in a particular frequency. The IR receiver will be tuned to that frequency, so it can ignore everything else.

In the picture below you can see a modulated signal driving the IR LED of the transmitter on the left side. The detected signal is coming out of the receiver at the other side.

(Thanks to SBProjects.com for the gif and excellent IR resource!)

Technical Details of  VS1838B IR Receiver

The pinout for VS1838B IR Receiver:

About the IR control

Infrared remotes are still the cheapest way to wirelessly control a device. We have designed the remote to be small, very simple, and low-cost.There are many different IR remote controls. all of these may have different encoding methods and number of physical buttons, and different codes received when a button is pressed.

IR-REMOTE LIBRARY:

Note: The following library must be installed in your installation for this to work!

CLICK HERE – IR REMOTE CONTROL: ARgithub.com/shirriff/IRremoteDUINO LIBRARY

NOTE!!

  1. If you have a late version of Arduino IDE with a library IRRobotRemote, it may conflict and you may have to remove that library.Make sure to delete Arduino(root)/libraries/RobotIRremote. Where Arduino(root) refers to the install directory of Arduino. The library RobotIRremote has similar definitions to IRremote and causes errors.
  2. Learn how to install an Arduino library on Arduino IDE, please visit https://osoyoo.com/2017/05/08/how-to-install-additional-arduino-libraries/

Examples

Read codes from IR Remote

This example will show you how to read IR remote codes from any IR remote using the VS1838B IR receiver and an board for Arduino. Once you can receive codes from individual button presses, your remote control and Arduin0 become a general purpose, short range, communication interface!

The first thing you need to do is make sure the IR library has been installed, Learn how to install an Arduino library on Arduin IDE, please visit here.

Connection

Build the circuit as below:

Code Program

After above operations are completed, connect the board to your computer using the USB cable. The green power LED (labelled PWR) should go on.Open the Arduino IDE and choose corresponding board type and port type for you project. Then load up the following sketch onto your board.

#include "IRremote.hpp"

#define IR_RECEIVE_PIN 2

void setup() {
  Serial.begin(9600);
  IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);  
}

void loop() {
  if (IrReceiver.decode()) {
    IrReceiver.printIRResultShort(&Serial);
    IrReceiver.resume();
  }
}

When specific buttons are pressed, you can use the incoming values to do something else in your code, for example turn on and off a motor or LED.
In your program you check for a completly received IR frame with:
if (IrReceiver.decode()) {}
This also decodes the received data.
After successful decoding, the IR data is contained in the IRData structure, available as IrReceiver.decodedIRData.

You can print the values to the Serial Monitor window:

IrReceiver.printIRResultShort(&Serial);

Running Result

The sketch will automatically decode the type of remote you are using and identify which button on your remote is pressed. Open the Serial Monitor in the Arduino IDE at 9600 bps and hit different buttons on your remote.

The Serial Monitor displaying random button presses on my remote. Different buttons show different codes:

Note: for remotes with unknown protocols you will also see the “Overflow” error in addition to the protocol info. That is simply because the end of the IR command could not be detected, since the protocol was unknown.

Finding the key codes for your remote

Since there are many different types of remote controls on the market (with different numbers of buttons and different button functions), we need to determine which received signal corresponds to which button.

The IRremote library will read the signal and returns all information about the signal in the following structure:

struct IRData {
    decode_type_t protocol;          // UNKNOWN, NEC, SONY, RC5, PULSE_DISTANCE, ...
    uint16_t address;                // Decoded address
    uint16_t command;                // Decoded command
    uint16_t extra;              
    uint16_t numberOfBits;           // Number of bits received for data 
    uint8_t flags;                   // IRDATA_FLAGS_IS_REPEAT, IRDATA_FLAGS_WAS_OVERFLOW 
    IRRawDataType decodedRawData;    // Up to 32  bit decoded raw data,
    uint32_t decodedRawDataArray[RAW_DATA_ARRAY_SIZE]; // 32 bit decoded raw data for send function.
    irparams_struct *rawDataPtr;     // Pointer of the raw timing data to be decoded. 
};

We are particularly interested in the command part of the structure. This section will contain the code that the remote control sends based on the key pressed. By printing the command values in the serial monitor, we can create a conversion table.

The code below does just that. Note that we have added a delay of 100 milliseconds in order to slow down the printing on successive key presses, which will result in duplicate prints.

#include "IRremote.hpp"

#define IR_RECEIVE_PIN 2

void setup() {
  Serial.begin(9600);
  IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
}

void loop() {
  if (IrReceiver.decode()) {
    uint16_t command = IrReceiver.decodedIRData.command;
    Serial.println(command);
    delay(100);  // wait a bit
    IrReceiver.resume();
  }
}

After uploading the code, open the serial monitor. Now press each key on the remote control and record the corresponding value you see in the serial monitor.

IR Remote Test

When specific buttons are pressed, you can use the incoming values to do something else in your code, for example turn on and off a motor or LED.

This example is to test the IR receiver, we modify the last sketch, when specific buttons are pressed, we will use the incoming values to print the corrseponding buttons’ name of the IR control. The hardware connection is same as above example, before uplod the code, make sure you have installed the IRremote library!

Code Program

After above operations are completed, connect the board to your computer using the USB cable. The green power LED (labelled PWR) should go on.Open the Arduino IDE and choose corresponding board type and port type for you project. Then load up the following sketch onto your board.

#include "IRremote.hpp"
#define IR_RECEIVE_PIN 2

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  Serial.begin(9600);
  Serial.println("IR Receiver Raw Data + Button Decode Test");
  IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
}/*--(end setup )---*/

void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  if (IrReceiver.decode()) {
    translateIR();
    IrReceiver.resume();
  }  
}/* --(end main loop )-- */

/*-----( Declare User-written Functions )-----*/
void translateIR() // takes action based on IR code received
// describing Car MP3 IR codes
{
  uint16_t command = IrReceiver.decodedIRData.command;
  switch(command)
  {
  case 69:  
    Serial.println(" CH-            ");
    break;
  case 70:  
    Serial.println(" CH             ");
    break;
  case 71:  
    Serial.println(" CH+            ");
    break;
  case 68:  
    Serial.println(" PREV           ");
    break;
  case 64:  
    Serial.println(" NEXT           ");
    break;
  case 67:  
    Serial.println(" PLAY/PAUSE     ");
    break;
  case 7:  
    Serial.println(" VOL-           ");
    break;
  case 21:  
    Serial.println(" VOL+           ");
    break;
  case 9:  
    Serial.println(" EQ             ");
    break;
  case 22:  
    Serial.println(" 0              ");
    break;
  case 25:  
    Serial.println(" 100+           ");
    break;
  case 13:  
    Serial.println(" 200+           ");
    break;
  case 12:  
    Serial.println(" 1              ");
    break;
  case 24:  
    Serial.println(" 2              ");
    break;
  case 94:  
    Serial.println(" 3              ");
    break;
  case 8:  
    Serial.println(" 4              ");
    break;
  case 28:  
    Serial.println(" 5              ");
    break;
  case 90:  
    Serial.println(" 6              ");
    break;
  case 66:  
    Serial.println(" 7              ");
    break;
  case 82:  
    Serial.println(" 8              ");
    break;
  case 74:  
    Serial.println(" 9              ");
    break;
  default:
    Serial.println(" other button   ");
  }
  delay(100);
} //END translateIR

Running Result

A few seconds after the upload finishes,  hit different buttons on your remote.

open the serial port in the Arduino IDE at 9600 bps, you should see the software below displays the button that was pressed:

Note: The Serial Monitor printing “other button” means the incoming values are not corresponding to your IR remote codes, it may appear if you hold the button down or there are other disturbances.

IR Control LED

Control a certain key (for example, Power key) via a remote controller by programming. When you press the key, infrared rays will be emitted from the remote control and received by the infrared receiver, and the LED on the Osoyoo basic board will light up. Connect an LED to pin 3 on the Osoyoo basic board so that you can see remotely whether the Power  key is pressed down.

Connection

Build the circuit as below:

Code Program

After above operations are completed, connect the board to your computer using the USB cable. The green power LED (labelled PWR) should go on. Load up the following sketch onto your board.

#include "IRremote.hpp"
#define IR_RECEIVE_PIN 2
const int ledPin = 3;

void setup()
{
  pinMode(ledPin,OUTPUT);//set ledpin as OUTPUT
  Serial.begin(9600);//initialize serial
  IrReceiver.begin(IR_RECEIVE_PIN, DISABLE_LED_FEEDBACK);
}
void loop()
{
  if (IrReceiver.decode()) //if the ir receiver module receiver data
  { uint16_t command = IrReceiver.decodedIRData.command;
  if(command == 69)
  {
    digitalWrite(ledPin,HIGH);//turn on the led
  }
  else
  {
    digitalWrite(ledPin,LOW);//turn off the led
  }
    IrReceiver.resume();
  }
  delay(100); //delay 600ms
}

Running Result

A few seconds after the upload finishes, press the “CH-” button of a remote control, and both the LED attached and that connected to pin 3 on the Osoyoo basic board will light up. Then press any other key, and the LEDs will go out.