Joris Bijnens
Published © CC BY-NC

Analyse the IR Protocol For a Toy Remote

Find out how the protocol for a toy IR remote works and use it to drive your remote controlled project!

BeginnerFull instructions provided1 hour3,166
Analyse the IR Protocol For a Toy Remote

Things used in this project

Hardware components

Wemos D1 Mini
Espressif Wemos D1 Mini
×1
Infra red sensor
×1

Story

Read more

Code

Wemos code

Arduino
Run this code to analyse your remote control
#ifndef UNIT_TEST
#include <Arduino.h>
#endif
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRutils.h>

// Using the D6 pin on the Wemos D1 mini
uint16_t RECV_PIN = 12;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn();  // Start the receiver
  Serial.println("Start");
}

String GetIRBinaryValue(decode_results *results, unsigned int level)
{
  String result = "";
  for (int i = 0;  i < results->rawlen - 2;  i++)
  {
    if (results->rawbuf[i] > level)
    {
      result += "1";
    }
    else
    {
      result += "0";
    }
  }
  return result;
}

void DumpIRBinaryValue(decode_results *results, unsigned int level)
{
  int c = 0;
  for (int i = 0;  i < results->rawlen - 2;  i++)
  {
    if (level == 0)
    {
      Serial.print(String(results->rawbuf[i]) + " ");
    }
    else if (results->rawbuf[i] > level)
    {
      Serial.print("1");
    }
    else
    {
      Serial.print("0");
    }
    c++;
    if (c > 9)
    {
      c = 0;
      Serial.print(" ");
    }
  }
}

long ParseIRValueDebug(decode_results *results, unsigned int minMaxBorder, int indexes[], int indexesSize)
{
  long result = 0;
  Serial.print("#");
  for (int i = 0; i < indexesSize;  i++)
  {
    if (results->rawbuf[indexes[i]] > minMaxBorder)
    {
      Serial.print("1");
    }
    else
    {
      Serial.print("0");
    }

    //Serial.print(String(results->rawbuf[i]) + ".");
    int t = results->rawbuf[indexes[i]];
    result <<= 1;
    result += t > minMaxBorder;
  }
  Serial.print(">" + String(result) + "#");
  return result;
}

long ParseIRValue(decode_results *results, unsigned int minMaxBorder,
                  int indexes[], int indexesSize)
{
  long result = 0;

  for (int i = 0; i < indexesSize;  i++)
  {
    int t = results->rawbuf[indexes[i]];
    result <<= 1;
    result += t > minMaxBorder;
  }
  return result;
}

void loop()
{
  if (irrecv.decode(&results))
  {
    DumpIRBinaryValue(&results, 16);

    /* Picoo Z */
    /*
      Serial.print(" - ");
      int throttleIndexes[] = {15, 17, 19, 21};
      int throttle = ParseIRValue(&results, 16, throttleIndexes, 4);
      Serial.print(String(throttle));

      Serial.print(" - ");
      int dirIndexes[] = {31, 33, 35};
      int dir = ParseIRValue(&results, 16, dirIndexes, 3);
      Serial.print(String(dir));

      Serial.print(" - ");
      int chnlIndexes[] = {11, 13};
      int chnl = ParseIRValue(&results, 16, chnlIndexes, 2);
      Serial.print(String(chnl));

      Serial.print(" - ");
      int trmIndexes[] = {23, 25, 27, 29};
      int trm = ParseIRValue(&results, 16, trmIndexes, 4);
      Serial.print(String(trm));
    */

    /* Dauphin */
    /*Serial.print(" - ");
      int chnlA[] = {3};
      int chnl = ParseIRValue(&results, 16, chnlA, 1);
      Serial.print(String(chnl));

      Serial.print(" - ");
      int leftThrA[] = {9, 11, 13, 15, 17};
      int leftThr = ParseIRValue(&results, 16, leftThrA, 5);
      Serial.print(String(leftThr));

      Serial.print(" - ");
      int rightDirA[] = {19, 21, 23, 25, 27};
      int rightDir = ParseIRValue(&results, 16, rightDirA, 5);
      Serial.print(String(rightDir));

      Serial.print(" - ");
      int leftDirA[] = {29, 31, 33, 35, 37};
      int leftDir = ParseIRValue(&results, 16, leftDirA, 5);
      Serial.print(String(leftDir));

      Serial.print(" - ");
      int rightThrA[] = {39, 41, 43, 45, 47};
      int rightThr = ParseIRValue(&results, 16, rightThrA, 5);
      Serial.print(String(rightThr));

      Serial.print(" - ");
      int lightA[] = {49};
      int light = ParseIRValue(&results, 16, lightA, 1);
      Serial.print(String(light));
    */

    Serial.println("");
    irrecv.resume();  // Receive the next value
  }
  delay(100);
}

Credits

Joris Bijnens

Joris Bijnens

2 projects • 7 followers
.Net programmer by trade, but not picky to learn other stuff. Looking to build my own R2D2 as personal assistant and save the world doing it...

Comments