Ir Sensor
Ir Sensor
https://learn.adafruit.com/ir-sensor
Overview 3
• Some Stats
• What You Can Measure
Testing an IR Sensor 5
IR Remote Signals 8
Using an IR Sensor 11
Making an Intervalometer 15
Reading IR Commands 19
CircuitPython 31
• Hardware & Setup
• Usage
Python Docs 38
IR detectors are little microchips with a photocell that are tuned to listen to infrared
light. They are almost always used for remote control detection - every TV and DVD
player has one of these in the front to listen for the IR signal from the clicker. Inside
the remote control is a matching IR LED, which emits IR pulses to tell the TV to turn
on, off or change channels. IR light is not visible to the human eye, which means it
takes a little more work to test a setup.
There are a few difference between these and say a CdS Photocells (https://adafru.it/
aHA):
• IR detectors are specially filtered for Infrared light, they are not good at
detecting visible light. On the other hand, photocells are good at detecting
yellow/green visible light, not good at IR light
• IR detectors have a demodulator inside that looks for modulated IR at 38 KHz.
Just shining an IR LED wont be detected, it has to be PWM blinking at 38KHz.
Photocells do not have any sort of demodulator and can detect any frequency
(including DC) within the response speed of the photocell (which is about 1KHz)
• IR detectors are digital out - either they detect 38KHz IR signal and output low
(0V) or they do not detect any and output high (5V). Photocells act like resistors,
the resistance changes depending on how much light they are exposed to
Some Stats
These stats are for the IR detector in the Adafruit shop (https://adafru.it/aIH) also
known as PNA4602. Nearly all photocells will have slightly different specifications,
although they all pretty much work the same. If there's a datasheet, you'll want to
refer to it
As you can see from these datasheet graphs, the peak frequency detection is at 38
KHz and the peak LED color is 940 nm. You can use from about 35 KHz to 41 KHz but
the sensitivity will drop off so that it wont detect as well from afar. Likewise, you can
use 850 to 1100 nm LEDs but they wont work as well as 900 to 1000nm so make sure
to get matching LEDs! Check the datasheet for your IR LED to verify the wavelength.
Try to get a 940nm - remember that 940nm is not visible light (its Infra Red)!
Testing an IR Sensor
Because there is a semiconductor/chip inside the sensor, it must be powered with 3 -
5V to function. Contrast this to photocells and FSRs where they act like resistors and
thus can be simply tested with a multimeter.
When the detector sees IR signal, it will pull the output low, turning on the LED - since
the LED is red its much easier for us to see than IR!
2 batteries (3V) may be too little. 3 Batteries should be fine if you have a triple-AA
holder
You can also get 5V from a microcontroller like an Arduino if you have one around.
Ground goes to the middle pin.
The positive (longer) head of the Red LED connects to the +6V pin and the negative
(shorter lead) connects through a 200 to 1000 ohm resistor to the first pin on the IR
sensor.
Now grab any remote control like for a TV, DVD, computer, etc. and point it at the
detector while pressing some buttons, you should see the LED blink a couple times
whenever the remote is pressed.
For this example we will use the Sony power on/off IR code from a Sony TV remote.
Its very simple and commonly documented!
Lets pretend we have a Sony remote, and we can look at exactly what light is being
blasted out of the IR LED. We'll hookup a basic light sensor (like a basic photocell!)
and listen in. We won't use a decoder like a PNA4602 (just yet) because we want to
see the undecoded signal. What we see is the following:
Basically we see pulses or IR signal. the yellow 'blocks' are when the IR LED is
transmitting and when there is only a line, the IR LED is off. (Note that the voltage
being at 3VDC is just because of the way I hooked up the sensor, if I had swapped the
pullup for a pulldown it would be at ground.)
The first 'block' is about 2.5ms long (see the cursors and the measurement on the
side)
You can measure the frequency of the IR pulses. As you can tell by the cursors and
the measurements on the side, the frequency is about 37.04KHz
OK so now we can understand how IR codes are sent. The IR transmitter LED is
quickly pulsed (PWM - pulse width modulated) at a high frequency of 38KHz and then
that PWM is likewise pulsed on and off much slower, at times that are about 1-3 ms
long.
One reason is that this lets the LED cool off. IR LEDs can take up to 1 Amp (1000
milliamps!) of current. Most LEDs only take 20mA or so. This means IR LEDs are
designed for high-power blasting BUT they can only take it for a few microseconds.
By PWM'ing it, you let the LED cool off half the time
Another reason is that the TV will only listen to certain frequencies of PWM. So a Sony
remote at 37KHz wont be able to work with a JVC DVD player that only wants say
50KHz.
Finally, the most important reason is that by pulsing a carrier wave, you reduce the
affects of ambient lighting. The TV only looks for changes in light levels that clock in
around 37KHz. Just like its easier for us to tell differences between audio tones than
to pin down the precsise pitch of a tone (well, for most people at least)
OK so now we know the carrier frequency. Its 37KHz. Next lets find the pulse widths!
The first pulse is 2.5ms. We can use the cursors to measure the remaining pulses. I'll
spare you the 12 images and let you know that the pulses are:
PWM ON OFF
2.4 ms 0.6 ms
So lets say you don't have a $1000 oscilloscope, how else can you read these
signals? Well the IR decoder such as the PNA4602 does us one favor, it 'filters out' the
38KHz signal so that we only get the big chunks of signal in the milliscond range. This
is much easier for a microcontroller to handle. Thats what we'll do in the next section!
Using an IR Sensor
The good news is that it is very easy to hook up this sensor. Just connect the output
to a digital pin. The bad news is that the Arduino's friendly digitalRead() procedure is
a tad too slow to reliably read the fast signal as its coming in. Thus we use the
hardware pin reading function directly from pin D2, that's what the line "IRpin_PIN &
BV(IRpin))" does. This trick is specific to ATmega328 based boards such as Arduino
Uno, Adafruit Metro, etc.
void setup(void) {
Serial.begin(9600);
Serial.println("Ready to decode IR!");
}
void loop(void) {
uint16_t highpulse, lowpulse; // temporary storage timing
highpulse = lowpulse = 0; // start out with no pulse length
// same as above
while (! (IRpin_PIN & _BV(IRpin))) {
// pin is still LOW
lowpulse++;
delayMicroseconds(RESOLUTION);
if ((lowpulse >= MAXPULSE) && (currentpulse != 0)) {
printpulses();
currentpulse=0;
return;
}
}
pulses[currentpulse][1] = lowpulse;
void printpulses(void) {
Serial.println("\n\r\n\rReceived: \n\rOFF \tON");
for (uint8_t i = 0; i < currentpulse; i++) {
Serial.print(pulses[i][0] * RESOLUTION, DEC);
Serial.print(" usec, ");
Serial.print(pulses[i][1] * RESOLUTION, DEC);
Serial.println(" usec");
}
}
//Serial.print("\t"); // tab
Serial.print("pulseIR(");
Serial.print(pulses[currentpulse-1][1] * RESOLUTION, DEC);
Serial.print(");");
If you ignore the first OFF pulse (its just the time from when the Arduino turned on to
the first IR signal received) and the last ON pulse (it the beginning of the next code)
you'll find the Sony power code:
PWM ON OFF
2.5 ms 0.6 ms
1.2 ms 0.6 ms
0.6 ms 0.6 ms
1.2 ms 0.6 ms
0.6 ms 0.6 ms
1.2 ms 0.6 ms
0.6 ms 0.6 ms
0.6 ms 0.6 ms
1.2 ms 0.6 ms
0.6 ms 0.6 ms
0.6 ms 0.6 ms
0.6 ms 0.6 ms
0.6 ms 27.2 ms
The camera we'll be using has an IR remote you can use to set it off (most higher-end
cameras have these).
OK step one is easy, point the remote control at the IR sensor and press the button,
we got the following for our ML-L3 Nikon remote.
PWM ON OFF
2.0 ms 27 ms
0.4 ms 1.5 ms
0.5 ms 3.5 ms
0.5 ms 62.2 ms
2.0 ms 27 ms
0.5 ms 1.5 ms
0.5 ms 3.5 ms
0.5 ms
PWM ON OFF
2.0 ms 27 ms
0.4 ms 1.5 ms
0.5 ms 3.5 ms
sent twice. Sending the same signal twice is very common - doubling up to make sure
it gets received
Next up we'll need to connect an IR 940nm LED to the output of the Arduino
Then we'll write a sketch which will pulse pin #13 on and off very fast in the proper
code sequence.
// This sketch will send out a Nikon D50 trigger signal (probably works with most
Nikons)
// See the full tutorial at https://learn.adafruit.com/ir-sensor/making-an-
intervalometer
// MIT License, attribution appreciated Limor Fried, Adafruit Industries
void setup() {
// initialize the IR digital pin as an output:
pinMode(IRledPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
Serial.println("Sending IR signal");
// so 26 microseconds altogether
microsecs -= 26;
}
void SendNikonCode() {
// This is the code for my particular Nikon, for others use the tutorial
// to 'grab' the proper code from the remote
pulseIR(2080);
delay(27);
pulseIR(440);
delayMicroseconds(1500);
pulseIR(460);
delayMicroseconds(3440);
pulseIR(480);
pulseIR(2000);
delay(27);
pulseIR(440);
delayMicroseconds(1500);
pulseIR(460);
delayMicroseconds(3440);
pulseIR(480);
}
void pulseIR(long microsecs) is our helper procedure, it will create the PWM IR
signal like we saw before. I used my scope to fine-tune it so that the delays added up
right. We use the not-often-discussed cli() and sei() procedures to turn off
interrupts. The Arduino does a couple things in the background like looking for serial
data to read or write, keeping track of time, etc. Most of the time we can just ignore it
but for delicate high speed signals like this we want to keep quiet so that we get a
nice clean signal
If you look at SendNikonCode() you will see the IR command code that we deduced
in the previous project by timing the pulses from the IR sensor.
Reading IR Commands
For our final project, we will use a remote control to send messages to a
microcontroller. For example, this might be useful for a robot that can be directed with
an IR remote. It can also be good for projects that you want to control from far away,
without wires.
For a remote in this example we'll be using an Apple clicker remote. You can use any
kind of remote you wish, or you can steal one of these from an unsuspecting hipster.
void printpulses(void) {
Serial.println("\n\r\n\rReceived: \n\rOFF \tON");
for (uint8_t i = 0; i < currentpulse; i++) {
Serial.print(pulses[i][0] * RESOLUTION, DEC);
Serial.print(" usec, ");
Serial.print(pulses[i][1] * RESOLUTION, DEC);
Serial.println(" usec");
}
I uploaded the new sketch and pressed the Play button on the Apple remote and got
the following:
Let's start a new sketch called IR Commander (you can download the final code from
GitHub at the green button below or click Download Project Zip in the complete
code listing).
/* Raw IR commander
#include "ircommander.h"
void setup(void) {
Serial.begin(9600);
void loop(void) {
int numberpulses;
numberpulses = listenForIR();
Serial.print("Heard ");
Serial.print(numberpulses);
Serial.println("-pulse long IR signal");
if (IRcompare(numberpulses, ApplePlaySignal,sizeof(ApplePlaySignal)/4)) {
Serial.println("PLAY");
}
if (IRcompare(numberpulses, AppleRewindSignal,sizeof(AppleRewindSignal)/4)) {
Serial.println("REWIND");
}
if (IRcompare(numberpulses, AppleForwardSignal,sizeof(AppleForwardSignal)/4)) {
Serial.println("FORWARD");
}
delay(500);
}
//KGO: added size of compare sample. Only compare the minimum of the two
boolean IRcompare(int numpulses, int Signal[], int refsize) {
int count = min(numpulses,refsize);
Serial.print("count set to: ");
Serial.println(count);
for (int i=0; i< count-1; i++) {
int oncode = pulses[i][1] * RESOLUTION / 10;
int offcode = pulses[i+1][0] * RESOLUTION / 10;
#ifdef DEBUG
Serial.print(oncode); // the ON signal we heard
Serial.print(" - ");
Serial.print(Signal[i*2 + 0]); // the ON signal we want
#endif
#ifdef DEBUG
Serial.print(" \t"); // tab
Serial.print(offcode); // the OFF signal we heard
Serial.print(" - ");
Serial.print(Signal[i*2 + 1]); // the OFF signal we want
#endif
#ifdef DEBUG
Serial.println();
#endif
}
// Everything matched!
return true;
}
int listenForIR(void) {
currentpulse = 0;
while (1) {
uint16_t highpulse, lowpulse; // temporary storage timing
highpulse = lowpulse = 0; // start out with no pulse length
// same as above
while (! (IRpin_PIN & _BV(IRpin))) {
// pin is still LOW
lowpulse++;
delayMicroseconds(RESOLUTION);
// KGO: Added check for end of receive buffer
if (((lowpulse >= MAXPULSE) && (currentpulse != 0))|| currentpulse ==
NUMPULSES) {
return currentpulse;
}
}
pulses[currentpulse][1] = lowpulse;
int ApplePlaySignal[] = {
// ON, OFF (in 10's of microseconds)
912, 438,
68, 48,
68, 158,
68, 158,
68, 158,
68, 48,
68, 158,
68, 158,
68, 158,
70, 156,
70, 158,
68, 158,
68, 48,
68, 46,
70, 46,
68, 46,
68, 160,
68, 158,
70, 46,
68, 158,
68, 46,
70, 46,
68, 48,
68, 46,
68, 48,
66, 48,
68, 48,
66, 160,
66, 50,
66, 160,
66, 50,
64, 160,
66, 50,
66, 3950,
908, 214,
66, 3012,
908, 212,
68, 0};
int AppleForwardSignal[] = {
// ON, OFF (in 10's of microseconds)
908, 444,
64, 50,
66, 162,
64, 162,
64, 162,
64, 52,
64, 162,
int AppleRewindSignal[] = {
// ON, OFF (in 10's of microseconds)
908, 442,
66, 48,
66, 162,
66, 160,
66, 160,
66, 50,
66, 160,
66, 160,
66, 160,
68, 158,
68, 160,
66, 160,
66, 50,
66, 48,
66, 50,
66, 48,
66, 162,
66, 160,
66, 48,
68, 48,
66, 160,
66, 50,
66, 50,
66, 48,
66, 50,
66, 48,
68, 48,
66, 160,
66, 50,
66, 160,
66, 50,
66, 160,
66, 48,
68, 3936,
906, 214,
This code uses parts of our previous sketch. The first part we'll do is to create a
function that just listens for an IR code an puts the pulse timings into
the pulses[] array. It will return the number of pulses it heard as a return-value.
int listenForIR(void) {
currentpulse = 0;
while (1) {
uint16_t highpulse, lowpulse; // temporary storage timing
highpulse = lowpulse = 0; // start out with no pulse length
// same as above
while (! (IRpin_PIN & _BV(IRpin))) {
// pin is still LOW
lowpulse++;
delayMicroseconds(RESOLUTION);
if ((lowpulse >= MAXPULSE) && (currentpulse != 0)) {
return currentpulse;
}
}
pulses[currentpulse][1] = lowpulse;
Our new loop() will start out just listening for pulses
void loop(void) {
int numberpulses;
numberpulses = listenForIR();
Serial.print("Heard ");
Serial.print(numberpulses);
Serial.println("-pulse long IR signal");
}
As you can see, there is some variation. So when we do our comparison we can't look
for preciesely the same values, we have to be a little 'fuzzy'. We'll say that the values
can vary by 20% - that should be good enough.
void loop(void) {
int numberpulses;
numberpulses = listenForIR();
Serial.print("Heard ");
Serial.print(numberpulses);
Serial.println("-pulse long IR signal");
Serial.println();
}
}
We found we had to tweak the stored values a little to make them match up 100%
each time. IR is not a precision-timed protocol so having to make the FUZZINESS 20%
or more is not a bad thing
Finally, we can turn the loop() into its own function which will
return true or false depending on whether it matched the code we ask it to. We also
commented out the printing functions
/*
Serial.print(oncode); // the ON signal we heard
Serial.print(" - ");
Serial.print(Signal[i*2 + 0]); // the ON signal we want
*/
/*
Serial.print(" \t"); // tab
Serial.print(offcode); // the OFF signal we heard
Serial.print(" - ");
Serial.print(Signal[i*2 + 1]); // the OFF signal we want
*/
//Serial.println();
}
// Everything matched!
return true;
}
We then took more IR command data for the 'rewind' and 'fastforward' buttons and
put all the code array data into ircodes.h to keep the main sketch from being too long
and unreadable (you can get all the code from github) (https://adafru.it/aKg)
void loop(void) {
int numberpulses;
numberpulses = listenForIR();
Serial.print("Heard ");
Serial.print(numberpulses);
Serial.println("-pulse long IR signal");
if (IRcompare(numberpulses, ApplePlaySignal)) {
Serial.println("PLAY");
}
if (IRcompare(numberpulses, AppleRewindSignal)) {
Serial.println("REWIND");
}
if (IRcompare(numberpulses, AppleForwardSignal)) {
Serial.println("FORWARD");
}
}
CircuitPython
With CircuitPython you can easily read IR sensor pulses from Python code. Built-in to
CircuitPython is a special pulseio module which actually does most of the work of
reading fast IR receiver pulses for you. Even better with Python code you can very
easily store and manipulate large lists of pulse lengths. There's even a handy Adafruit
CircuitPython IRRemote (https://adafru.it/BBm) module which simplifies some of the
processing logic for reading generic remote controls. CircuitPython makes it very
easy to read IR signals!
As mentioned you'll also need to install the Adafruit CircuitPython IRRemote (https://
adafru.it/BBm) library on your CircuitPython board.
Next you'll need to install the necessary libraries to use the hardware--carefully follow
the steps to find and install these libraries from Adafruit's CircuitPython library
bundle (https://adafru.it/zdx). Our introduction guide has a great page on how to
install the library bundle (https://adafru.it/ABU) for both express and non-express
boards.
Remember for non-express boards like the, you'll need to manually install the
necessary libraries from the bundle:
• adafruit_irremote.mpy
Or download the file from the latest release on the Adafruit CircuitPython IRRemote
releases page (https://adafru.it/BBn).
Before continuing make sure your board's lib folder or root filesystem has
the adafruit_irremote.mpy module copied over.
Usage
Next connect to the board's serial REPL (https://adafru.it/Awz)so you are at the
CircuitPython >>> prompt.
import board
import pulseio
Now create an instance of the PulseIn class (https://adafru.it/BBo) which reads pulses
from the IR sensor's output. A pulse is simply a change from high to low or vice-versa
and the PulseIn class will record the microsecond duration of each pulse. Let's create
a pulse input that can remember the duration of up to 200 pulses (enough to record
most remote control codes):
• Board pin - This is a required parameter which indicates which pin is connected
to the output of the IR receiver.
• maxlen - This specifies the number of pulse durations to record. For most
remote controls a value of 200 will be more than enough pulse durations to
store. If you set this too high you might use more memory than your board has
available so be careful with what value you pick.
• idle_state - This is a boolean that indicates the 'default' or idle state of the pulse
pin. For IR receivers they typically idle in a high logic or True state so setting the
idle_state to True indicates the normal state is high logic level.
Once you have a pulse input object you can interact with it as if it were a list of
duration values. Internally the PulseIn class is always listening for pulses from the pin
(i.e. a change from the current high/low logic level to the opposite level) and saving
the duration of the pulse. You can list then number of received pulses just like
reading the length of a list:
len(pulses)
A value of zero means the sensor hasn't yet received a pulse. Try pointing a remote
control at the sensor and pressing a button. Then read the pulse length again:
len(pulses)
Now we have some pulse durations to investigate! First let's tell the pulse class to
temporarily stop listening for pulses. This is useful so that you can operate on the last
seen pulse without other pulses adding more noise or artifacts:
pulses.pause()
pulses[0]
pulses[1]
pulses[2]
Each duration is the time in milliseconds that the pulse was at a specific logic level.
The very first pulse is a maximum value of 65535 because it represents the amount
of time the sensor was waiting for the pulse to start (i.e. how long it was in the default
high logic level idle state). Just like with the Arduino code on the previous page you
can ignore this first value.
The next two values are interesting, the next pulse value shows the sensor received a
pulse that was about 9 milliseconds long (or ~9000 microseconds). Then the sensor
received no pulse for about 4 milliseconds. This pair of values represents a single
pulse and the start of the remote control signal. It's good to see a value of ~9ms on
and ~4m off as that's a common preamble or start for IR codes!
It turns out these pairs of pulses are so common between different remote controls
that many of them can be read with similar code. The Adafruit CircuitPython
IRRemote library is a very simple IR remote control decoding library that simplifies
much of the pulse and remote decoding logic. Let's use this module to simplify our
pulse analysis, first import it and then create a remote decoder:
import adafruit_irremote
decoder = adafruit_irremote.GenericDecode()
The decoder class allows you to easily wait for and read a list of pulses from a remote
control press. Before you use it lets turn the pulse input back on (remember it's
currently paused) and clear its previous input:
Now we're ready to use the decoder to wait for and return pulses. Run this code and
notice the REPL stops and waits for further input:
pulse = decoder.read_pulses(pulses)
Aim your remote control at the receiver and press a button. You should see the REPL
return to normal operation. This means the decoder was able to detect an IR remote
signal and returned the raw list of pulse values.
This list of pulses is an array which contains the length in microseconds of each high
and low pulse from the receiver. For example you can check how many pulse
changes were detected and see their lengths by using the standard array length and
printing operations:
len(pulse)
pulse
One very useful thing the decoder is doing internally is detecting and ignoring noise
or extraneous pulse widths, like a long starting pulse width before the remote control
is detected. This is very useful as it simplifies your IR processing code--you can focus
on just looking at the 'cleaned up' pulse lengths!
pulse2 = decoder.read_pulses(pulses)
Now let's compare the first and second pulse list to see if they match. A simple
comparison might be to check every single value in each list and verify they're the
same. Let's try it with a simple Python function we define:
simple_pulse_compare(pulse, pulse2)
Oh no, the comparison failed and returned false! What happened, wasn't the same
button pressed? It turns out the timing between pulses can vary in small ways. If you
look at the individual pulse lengths of each array you'll see they're close but not
exactly the same. If you compare raw pulses you need to add a 'fuzzyness' that
compares values that are close but not exactly the same.
Let's make a new fuzzy compare function that will check for pulses that are close to
each other (within 20% of one another for example):
fuzzy_pulse_compare(pulse, pulse2)
Success! Both pulses appear to be the same when using a fuzzy comparison. By
default the comparison will consider pulses the same if they're within 20% of each
other, but you can change that fuzzyness by setting the fuzzyness keyword to a
different value. The fuzzyness value is a percentage from 0 to 1.0 (or 0 to 100%)
where the pulses must be within that percent of each other's timing. Lower values are
stricter and require more similar pulses, whereas higher values are less strict and
might allow noise or incorrect pulses to appear the same. In general stick with the
20% fuzzyness unless you run into more problematic IR signals.
Let's tie everything together by making a complete program that waits for the button
above to be pressed and prints a message.
You can use the recorded pulse list in your program to remember the previously
recorded pulse and compare new ones against it. To detect a different key press just
record it with the steps above and update the pulse list in the code.
Change the pulse list at the top in the code below to the value you recorded (just
copy and paste it from the REPL) and save it as a code.py on your board:
import board
import pulseio
import adafruit_irremote
print('IR listener')
# Fuzzy pulse comparison function:
def fuzzy_pulse_compare(pulse1, pulse2, fuzzyness=0.2):
if len(pulse1) != len(pulse2):
return False
for i in range(len(pulse1)):
threshold = int(pulse1[i] * fuzzyness)
if abs(pulse1[i] - pulse2[i]) > threshold:
return False
return True
Now when you press the remote control button you should see a message printed at
the REPL! That's all there is to basic raw IR pulse detection and comparison with
CircuitPython!
The code on this page can be handy for basic or unknown remote control protocol
detection. However be aware that remote controls are actually quite advanced and
sometimes don't behave the way you expect--like pressing a button multiple times
might not actually send the full code each time, instead the remote might send a
shorter repeat code! This means the basic raw IR detection shown here could fail
because it doesn't expect a repeat code when one is seen.
Python Docs
Python Docs (https://adafru.it/C54)