A successful way to control infrared equipment via an Arduino and an infrared LED

Controlling and LED-stripe with IR light
This is the second part of my little tutorial on how to measure an IR pulse sequence from a remote control (see this article) and then copy and send this sequence to control equipment like a TV, a lamp or other IR equipment. The adarfuit.com website gives us once more a possible way on how to implement this, but in the following article we want to discuss on how to make it easier and again somehow more convenient in many cases. We can easily use the comma-separated values we got from our pulse measurements from the previous article to control the LED and output the needed 38 kHz signal sequence.

Again, first the code, and afterwards an explanation:

int irledpin=7;
int pulsmatrix[200]=        { // in the next line we put our pulse sequence, starting with the time of our first pulse:
long matrixsum[200];             

void setup()                                                               {
  pinMode(irledpin, OUTPUT); Serial.begin(9600);
  for(int i=1;i<200;i++){matrixsum[i]=pulsmatrix[i]+matrixsum[i-1];}       }   // The values are added, what we get is the ending times in microseconds for pulses and pulse-free times.

void pulse()                        {
    digitalWrite(irledpin, HIGH);
    delayMicroseconds(9);         // the value can be 10 or 8 also, but 9 should work quite well as it gives us a pulse very near to 38 kHz.
    digitalWrite(irledpin, LOW);
    delayMicroseconds(9);           }

void loop(){

  delay(1000);              // delay between pulses, may not be too little, at best stay over 300.
  Serial.println("Pulse sequence starts");
  long start=micros();
  for(int i=0;i<200;i+=1)                                     {
    while(micros()<start+matrixsum[i]){pulse();}                  // here we send our 38 kHz pulse
    while(micros()<start+matrixsum[i]){}                      }   // a pause, until we start our next pulse 

In the following a little explanation:

The code is easy to understand. We define our pulse sequence at the beginning, where the first time is the length of the first pulse, the next value is the pulse-free time, then the next pulse value and so on. The matrixsum adds all the values in some kind of a ladder, so we have the ending times of the pulses in a very accurate way. That avoids a problematic stretching of the whole sequence and was very accurate in first tests - see the hi-res video on Youtube shown below:

In this video the Arduino sends the command to change the color to red every 1 second, and in between I switch to other colors with the "real" remote.

You can download the program as .ino file here.


  1. I enjoyed reading your article :) PLease continue publishing helpful topics like this. Regards, from Let's get gadget, a company whose into selling
    remote extender


Kommentar veröffentlichen

Kommentare verfassen ist hier sehr einfach, man kann sogar ohne Anmeldung anonym Kommentare hinterlassen. Also, zeig' uns deine Meinung ...

Beliebte Posts aus diesem Blog

Die Hinterradachse beim Fahrrad aus- und einbauen - Reparatur z.B. nach einem Achsbruch

Erfahrungen mit Springlane.de - einmal und sicher nie wieder!