IR signal recorder with Arduino Uno
Sun, Apr 24, 2016 in post Arduino Electronics arduino uno infrared ir tsop382 tsop38238
I’ve been tinkering with IR and the TSOP38238 IR receiver modules I got from Adafruit and Sparkfun. That’s right, plural, as I burned the first one — be REALLY sure not to mix ground and VCC with this one! I ordered 10 more from AliExpress just to make sure I have spares in case I burn my second one as well…
There are IR libraries for Arduino already, but they were a bit complex to my taste, as I’m first planning just to record one IR code from my bulky Sony projector remote and make a small trinket to send that on button press. The TSOP382 already demodulates the signal, so I just want to record the times the remote IR led is on, off, on, off, and so on. So I made an Arduino sketch to do just that: Count loop cycles, detect when signal goes from high (no IR signal detected) to low and vice versa:
#define MAX_LEN 256
int seq[MAX_LEN], slen = 0, cnt = 0;
int irPin = 2, val = HIGH;
int ledPin = 13, strobe = LOW;
void setup() {
Serial.begin(9600);
pinMode(irPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.println("Start!");
}
void loop() {
int val2 = digitalRead(irPin);
if(val2 == val) {
cnt++;
// Following line is a dummy to make if and else sections
// take about the same time
if(slen < MAX_LEN) seq[0] = cnt;
if(cnt > 30000) {
if(slen) {
Serial.print("int onoff[");
Serial.print(slen-1);
Serial.println("] = {");
for(int i=1; i<slen; i++) {
Serial.print(seq[i]);
if(i+1 < slen) Serial.print(", ");
if(i%10 == 0) Serial.println();
}
Serial.println("\n};");
}
cnt = 0;
slen = 0;
}
} else { // transition
if(slen < MAX_LEN) seq[slen++] = cnt; // store if space left
val = val2;
cnt = 1;
}
delayMicroseconds(5); // results in around 20 us per loop
digitalWrite(ledPin, strobe); // strobe a LED
strobe = (strobe == LOW) ? HIGH : LOW; // toggle
}
Using it is easy. Just double-check the IR sensor pins, wire GND to Arduino ground, VCC to Arduino 5V (if your detector is 5V tolerant) and signal pin to digital pin 2. Point a remote towards the detector and quickly press a button (most remotes repeat signals and we have only a small buffer for storing transitions). The serial monitor will print out timings for on and off cycles of the remote’s IR signal (first entry is “on”, second “off”, then “on”, “off”, …):
int onoff[95] = {
118, 29, 59, 28, 30, 29, 58, 29, 29, 29,
59, 29, 29, 29, 29, 29, 30, 28, 30, 29,
58, 29, 29, 29, 59, 28, 30, 29, 58, 29,
30, 1013, 118, 29, 58, 29, 29, 29, 58, 30,
29, 29, 58, 29, 29, 29, 30, 29, 29, 29,
29, 29, 59, 29, 29, 29, 58, 29, 29, 29,
59, 29, 29, 1014, 118, 29, 58, 29, 29, 30,
58, 29, 29, 29, 58, 29, 30, 29, 29, 29,
29, 29, 29, 30, 57, 30, 29, 29, 58, 29,
29, 30, 58, 29, 29
};
To translate these numbers to actual timings, you’ll have to know how fast the Arduino is running the loop()
method. That’s why I strobe the LED pin (13) on the Arduino, so you can use a scope or multimeter to measure the frequency of digital pin 13. I got 24.4 kHz on Arduino Uno, which translates to 48 800 transitions/sec, so 20.5 microseconds per loop. So the first number in onoff
array, 118, means that first “on” pulse from remote is 118*20.5us = 2419 us long. Measuring with my Picoscope confirms, that the pulse is 2445 us, so there is about 1 % error. Not bad!
With the onoff `array, you can easily make a transmitter application to replicate the IR signal. I'm going to do that next, so stay tuned for "part two"!`
4 comments
Alfredo:
M = Mega (x 1000000)
S = Siemens
MS = MegaSiemens
In mks:
m = mili (x 0.001)
s = seconds
ms = mili-secons
MS/s = MegaSiemens/seg (What is this???)
ms/s = resolution in time
Joonas Pihlajamaa:
I didn’t see it here, but at least I have used MS/s as “megasamples per second” meaning the resolution of PicoScope. They use the same acronym in their product brochures, I’ve suspected its some kind of standard scope terminology. :)
Rich:
Where’s part 2?
Joonas Pihlajamaa:
I think I unfortunately never got around to writing the continuation to this. :( Hope you find an alternative resource to propel your project further!