I programmed an ATTINY13 to drive an IR led on 36kHz with this code:

void setup(){
  DDRB |= (1<<PB0); //Set pin PB0 as output
  //DDRB |= (1<<PB1); //Set pin PB1 as output
  TCNT0 = 0;
  TCCR0A = 0;
  TCCR0B = 0;

  TCCR0A |=(1<<COM0A0); //Timer0 in toggle mode Table 11-2
  TCCR0A |=(1<<WGM01); //Start timer 1 in CTC mode Table 11.5
  TCCR0B |= (1 << CS00);// Prescaler table 11.6
  OCR0A=110; //CTC Compare value, 36khz

void loop(){ //cycle = 1/36 = 28μs

 for (int i=0; i<10; i++) { //burst of 10 cycles
  TCCR0A |=(1<<COM0A0); //on for 14μs
  TCCR0A &= ~(1<<COM0A0); //off for 14μs
  delayMicroseconds(392); //gap of 14 cycles

The loop function is based on the TSOP1236 datasheet on burst/gap rate and ratio:

• Carrier frequency should be close to center frequency of the bandpass (e.g. 36 kHz). • Burst length should be 10 cycles/burst or longer. • After each burst which is between 10 cycles and 70 cycles a gap time of at least 14 cycles is necessary. • For each burst which is longer than 1.8 ms a corresponding gap time is necessary at some time in the data stream. This gap time should be at least 4 times longer than the burst. • Up to 800 short bursts per second can be received continuously.

I'm getting readings now on the receiver's end, using Arduino and an attachInterrupt().

I posted a question here about this. It won the tumbleweed award, but I seemed to have found the answer.

However, the range went down significantly from a couple of meters, when I was overdriving the LED and wasn't using gaps, to about 30 centimeters now. It's also not very steady; sometimes it doesn't report a beam break, sometimes it does.

I only need about a meter. What am I doing wrong?


You are initializing the timer to generate 36 kHz carrier. Then in the for loop you are turning the timer 10 times on and off to the compare output pin at 28us period - or 36kHz. This obviously does not work, modulating it twice. Just connect it, let it be on for 10+ cycles like 500 us and disconnect it for say 1000 us.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.