In this example you will point an infrared remote at your Circuit Playground Express and we will decode the signal. It will tell you the name and number of the protocol, the number of bits in the protocol, and a hex number up to 32 bits long that represents the received data.

You can find the program in the Adafruit_CircuitPlayground library in the "examples" subfolder. Or here is a listing of the program that you can cut and paste and we will use for reference in our discussion.

/* Infraread_Read.ino Example sketch for IRLib2 and Circuit Playground Express
   Illustrates how to receive an IR signal, decode it and print
   information about it to the serial monitor.
*/
#include <Adafruit_CircuitPlayground.h>

#if !defined(ADAFRUIT_CIRCUITPLAYGROUND_M0)
  #error "Infrared support is only for the Circuit Playground Express, it doesn't work with the Classic version"
#endif


void setup() {
  CircuitPlayground.begin();
  Serial.begin(9600);

  while (!Serial); // Wait until serial console is opened
  
  CircuitPlayground.irReceiver.enableIRIn(); // Start the receiver
  Serial.println("Ready to receive IR signals");
}

void loop() {
  //Continue looping until you get a complete signal received
  if (CircuitPlayground.irReceiver.getResults()) {
    CircuitPlayground.irDecoder.decode();           //Decode it
    CircuitPlayground.irDecoder.dumpResults(false);  //Now print results. Use false for less detail
    CircuitPlayground.irReceiver.enableIRIn();      //Restart receiver
  }
}

You should upload the sketch to your Circuit Playground Express using the Arduino IDE. Once it has successfully uploaded, you should open your serial monitor. It will prompt you to send an IR signal. Take any TV or DVD or cable box remote or the recommended Adafruit Mini Remote and point it at your Circuit Playground Express and press a button.

Here is some sample output.

Ready to receive IR signals
Decoded NEC(1): Value:FD807F Adrs:0 (32 bits)

This was a result after pressing the play button on the Adafruit Mini Remote. It tells us that we successfully decoded NEC protocol. That is protocol number 1. The hex value returned was "0xFD807F". This is a 32-bit value. It so happens that all NEC protocols are always 32 bit values but some protocols such as Sony might have a variable number of bits of data so that will be useful information. The "Address" value is also used by some protocols to provide additional information. For example the Samsung36 protocol is 32 bits long and is separated into a 16-bit address and a 20 bit value. For the NEC protocol "Address" is and used.

If the protocol is not recognized you will see…

Decoded Unknown(0): Value:0 Adrs:0 (0 bits)

In the next example we will show you how to use the received data from the IR receiver to control the pattern of lights on the NeoPixels.

How it works

Let's look at the specifics of the program to see how it works.

At the top we include the Adafruit_CircuitPlayground library. Then in the setup function we initialize the CircuitPlayground object and the serial monitor. We wait for the serial monitor to initialize. Then we call…

CircuitPlayground.irReceiver.enableIRIn()

This tells the IR receiver portion of IRLib2 to begin monitoring the Circuit Playground Express IR receiver circuitry using a pin change interrupt. Every time it detects an IR signal going from low to high or high to low it generates interrupt and starts a timer. It measures the intervals of Marks and Spaces received.

In the "loop()" portion of the program we have an if statement that checks…

if (CircuitPlayground.irReceiver.getResults()) {

this function checks to see if a full frame of our data has been received and if it has it will return true. In this example we do nothing but wait for that signal to turn true but in other applications you can go off and do something else in the loop as long as you periodically check "getResults()" to determine when the data has been received and then to do something with it.

Once a full frame of data has been received, the receiver stops listening for more data until you tell it to start up again.

Inside the if statement we call the CircuitPlayground.irDecoder.decode() method. It looks at the timing data and determines what protocol was used and decodes it. To see the results we call the CircuitPlayground.irDecoder.dumpResults(false) method. It will print the information on the serial monitor. If you change the parameter to "true" it will give you a more verbose listing of the information received.

Finally now that we have decoded the information and printed it out, we want to resume receiving so we have to call CircuitPlayground.irReceiver.enableIRIn() again to restart the receiver.

Details of the signal

For most applications, once you know the protocol number, the number of bits, and the data value that is all you need to to know in order to do whatever you want to do with the information. But if you're trying to analyze an unknown protocol or are just curious about what goes on behind the scenes we can use the verbose option on the dumpResults method to get more information. Edit the line in your sketch that says

CircuitPlayground.irDecoder.dumpResults(false)

and change the parameter to "true". Here is the output you would see by pressing the play button on the Adafruit Mini Remote.

Ready to receive IR signals
Decoded NEC(1): Value:FD807F Adrs:0 (32 bits)
Raw samples(68): Gap:52026
Head: m9014 s4560
0:m538 s602 1:m567 s574 2:m565 s577 3:m563 s577
4:m533 s607 5:m542 s597 6:m543 s598 7:m541 s599
8:m560 s1711 9:m539 s1734 10:m564 s1706 11:m564 s1707
12:m562 s1710 13:m569 s1702 14:m567 s573 15:m566 s1704

16:m536 s1737 17:m562 s578 18:m542 s598 19:m561 s580
20:m559 s581 21:m559 s581 22:m539 s602 23:m538 s602
24:m537 s604 25:m536 s1735 26:m534 s1737 27:m562 s1709
28:m541 s1731 29:m568 s1702 30:m567 s1704 31:m536 s1736

32:m534
Extent=67563
Mark min:533 max:569
Space min:573 max:1737

The first line of the output is identical to the non-verbose version of the output. It tells us this is the NEC protocol, the hex value, and the number of bits. It then goes on to give us other information.

There were 68 samples of marks and spaces. The gap between the time we called enableIRIn() and the beginning of the signal was 52026 microseconds. All of the values in this dump are given in microseconds.

The header of the signal consisted of a mark that was 9014 and space of 4500. This is followed by the mark and space values for each of the 32 bits. The marks are pretty much consistent around the middle 500 range. Some of the spaces are about that same value while others are are three times that value. The bits with short spaces are interpreted as a "0" and the bits with a long space are interpreted as a "1".

Other information at the end of the dump includes "Extent" which is the total length of the signal. It also tell you the minimum and maximum length of marks and spaces. This information can be used to identify unsupported protocols.

For more information about all the protocols supported by IRLib2 and how to write your own protocol decoders for protocols we do not yet support there is extensive information in the IRLib2 manual available on GitHub at https://github.com/cyborg5/IRLib2/tree/master/IRLib2/manuals

The manual is available in Microsoft Word .docx, Adobe acrobat PDF, or e-book .epub formats.

This guide was first published on Jul 27, 2017. It was last updated on Jul 27, 2017.

This page (Infrared_Read) was last updated on Oct 18, 2021.

Text editor powered by tinymce.