fast and precise timer

hello all

i’m currently in the process of hacking IR signals from a remote control. i’m reading the signal coming from the remote with a light sensitive resistor and i’m trying to store the timing of the signal. i’ve programmed a simple for loop, which increments a counter for every loop, and then stores the counter values when the light sensor goes from high to low or the other way around. that way mapping the signal coming from the remote.

but this gives unstable results - they differ a lot - even if i push the same button under controlled light surroundings.

it seems to me, that i need a more reliable way of keeping track of time (in microsenconds or smaller)

is there a timer function for the arduino board?

is there a manual/list of commands for the arduino board?

I have the same aim. I’m trying to compare the binary patterns from a IR Sensor (the ones with 3 pins: GND, +5V and digital out) gives me (i have a Yamaha remote).
I set up an external interrupt service routine, that’s listening on pin 8 (the only pin that can do that on an arduino board). My first approach was just to count the number of pulses, but it was allways 36 - completley independet form which button I pressed.
So my second attempt was to meassure the time form edge to edge of each pulse. But i still can’t distinguish which button get’s pressed. I looks like the timing/period of the counting timer is set too short in my case (change of timer1’s prescaler could be a solution).
I have not looked into that, I currently suspended this project. See my code only as a rough sketch…

int irPin = 8;
int somethingHappend = 0;

int count = 0;
int timeL;
int timeH; 

unsigned long pattern[40]; //to store the binary pattern

int i;

void irHandler(void){
// external interrupt service routine

  timeL = ICR1L;  // read time since last reset
  timeH = ICR1H;
  pattern[count]= (timeH << 8 ) | timeL;
  //pattern[count] = millis();   //another attemp, I don't which is more clever
  somethingHappend = 1;

void printLong(unsigned long n)
// that is just the new printInteger function copied from the arduino 0004 svn source
        unsigned long base = 10;
      unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars. 
      unsigned long i = 0;

      if (n == 0) {

      while (n > 0) {
            buf[i++] = n % base;
            n /= base;

      for (; i > 0; i--)
            printByte(buf[i - 1] < 10 ?
                  '0' + buf[i - 1] :
                  'A' + buf[i - 1] - 10);

void setup() {
  pinMode(irPin, INPUT);
  pinMode(ledPin, OUTPUT);
  beginSerial(9600);           // connect to the serial port to send values back

  TCNT1H = 0;                                    // reset TCNT1
  TCNT1L = 0;
  TIMSK |= _BV(TICIE1);  //enable external interrupt for Timer1
  timerAttach(TIMER1INPUTCAPTURE_INT, irHandler); //attach service routine

void loop() {

    delay(500);                //make shure we wait till the whole pattern for one button press is passed completley!
    TCNT1H = 0;            // reset TCNT1
    TCNT1L = 0;
    count = 0;
    somethingHappend = 0;

  delay(500);             //wait to not overload the serial connection if nothing happend

  // print the last pattern
  for (i=0; i < 40; i++){
    printString(" ");


note that you can’t use PWM/analogWrite() now, because timer1 would then be used for 2 things, what is not possible.
Here is a nice page that shows how to record the IR sensor binary pattern with any soundcard:
I think, one should really look into how the IR Remote protocol is constructed. Any hints?

I think, one should really look into how the IR Remote protocol is constructed. Any hints?

from my research so far it looks like there is ABSOLUTELY NO protocol/standard towards IR remote signal: sucks. big time.

First of all a photoresistor is not good
what you need is a photodiode. on top of that the IR signal is modulated at 36-40KHz

luckily enough there are pre made readers-decoders that give you a nice clean signal…

try the TSOP1738

the best protocol to decode is the RC5 used by sony and philips…

you can find an example at this page…

it can be converted to arduino



we tried to compile your code example and got a compilation error

first we declared the var: TIMER1INPUTCAPTURE_INT as int

afterwards we got following compiler.output:
In function void setup()': error: timerAttach’ undeclared (first use this function)


it seems that this c function is missing ?

any ideas?