25 or less percent duty cycle on 38kHz signal

Hi guys, I have this code, working just fine for a 50% duty cycle, but I need more distance between the sensor and the emitting led, so I have to inject more current on the led and keep it more time off, to ensure not to burn it, so I need a 20/25% duty cycle to achieve this. I tried to divide the OCR2B not for 2 but 4, but the led just blinks once, anyone can help?


const byte LED = 3;  // Timer 2 "B" output: OC2B

#define PIN_IR 3
#define PIN_DETECT 2
#define PIN_STATUS 5

const long frequency = 38000L;  // Hz

void setup() 
  pinMode (LED, OUTPUT);

  TCCR2A = bit (WGM20) | bit (WGM21) | bit (COM2B1); // fast PWM, clear OC2B on compare
  TCCR2B = bit (WGM22) | bit (CS21);         // fast PWM, prescaler of 8
  OCR2A =  ((F_CPU / 8) / frequency) - 1;    // zero relative  
  OCR2B = ((OCR2A + 1) / 2) - 1;             // 50% duty cycle

}  // end of setup

void loop()

  digitalWrite(PIN_STATUS, !digitalRead(PIN_DETECT));

It is OCR2B you need to change make it 13 in the code below

void setIrModOutput(){  // sets pin 3 going at the IR modulation rate
  pinMode(3, OUTPUT);
  TCCR2A = _BV(COM2B1) | _BV(WGM21) | _BV(WGM20); // Just enable output on Pin 3 and disable it on Pin 11
  TCCR2B = _BV(WGM22) | _BV(CS22);
  OCR2A = 51; // defines the frequency 51 = 38.4 KHz, 54 = 36.2 KHz, 58 = 34 KHz, 62 = 32 KHz
  OCR2B = 26;  // deines the duty cycle - Half the OCR2A value for 50%
  TCCR2B = TCCR2B & 0b00111000 | 0x2; // select a prescale value of 8:1 of the system clock

Doesn't work, it works with 50% duty cycle as you have in your code, but when I reduce it to half, to have a 25% duty cycle and load it to the arduino it just blinks the led once and it stays off afterwards...

Are you looking at this with a scope?

Nop, I do not own a scope :(

I find that observation hard to believe. Are you sure that is what is happening? Especially without a scope to back you up.

You could try setting it to 75% and powering the LED through current sinking rather than sourcing, this inverts the signal.

Well, as I said, the led blinks once when the program is loaded and then goes off for the rest of the time, if I had a scope it would be easier but I don't, nevertheless you can test the code yourself, maybe I'm doing some mistake.

What LED blinks once?

I tested your code and it worked.

I changed the duty cycle thus:

  OCR2B = ((OCR2A + 1) / 4) - 1;             // 25% duty cycle

That works too with a 25% duty cycle.

This is on a Uno.

Well guys, you're both right, the problem is that with a 25% duty cycle, I have to get the IR leds nearer the TSOP, somehow I loose power on the leds I have. At the moment I have a dual configuration, 2 leds connected to 2 BC548, which gave me a 4 meter distance between emitter/receiver circuit, with 25% duty cycle I lost 50 cm, I was expecting to achieve more distance not less. Thanks both of you for your time and work, any suggestions to improve distance?

kalium: so I have to inject more current on the led and keep it more time off, to ensure not to burn it, so I need a 20/25% duty cycle to achieve this.

Is this wise? In any case, where is this more current coming from?

Yes just back from trying it on my UNO and it works like Nick said.

Switch back to 50% and use two LEDs driven from a transistor. Or an Led with a higher current rating and a driving transistor.

That's what I'm doing now, 2 leds, each one driven by a transistor (BC548). I needed to reduce the duty cycle to achieve more current on the leds, during less time, to assure not to burn them. I'll try the same configuration with more powerful transistor, a BC337-40, and just one led to see if it works. A regular tv remote works at 8/9 meter distance, so the receiver part is quite fine, regarding the emitter circuit I opened the remote and found what was a 1 led configuration with a BC337-40 (equivalent) and 2 resistors. Hope this works.

Thanks a lot guys, as soon as I test it I'll post results here.


A "more powerful transistor" will not make any differance. You need more light. Reducing the duty cycle and upping the peak light will not increase the range. You need an IR led that produces more lumens or can take more current. Most of the ones I have seen need a 33R resistor to limit the current from 5V. That will need a transistor driver.

I attached the configuration I’m using now, to be clear.
Nevertheless the LED can be driven with more current (and consequently more light) as long as it’s used during short pulses that’s what I expect to confirm with the new transistor.


It is the 150R that controls the current through the LED at the moment this is about 30mA. A better transistor is not going to change this. The only way is to reduce the resistor. Most transistors can switch at least 100mA so you can simply double the current by dropping the resistor.

Interesting concept - presuming that if you shine a light twice as bright for half the time, it will "get through" better.

The problem is - firstly - that the receiver is actually integrating the light it receives, so from its point of view, there is no difference.

The second problem is that the LED may not - and probably does not - generate twice the light intensity for twice the current. Various effects are in play, and it is not linear.

Sorry dude, my bad, I didn't use the 150 ohm resistor that appears on schematic, I used two 100 ohms in parallel to achieve 50 ohms. ;)

In which case all I can think of is that you have more diode / transistors. You say you have two already then you look like you will have to go to four.

Take a look at the LED specs that I’m using, it’s all there.


BIR-BM13E4G-2.pdf (213 KB)

So if you have 5V and assuming a .7V Vce saturation voltage in the transistor and 1.5V across the LED that leaves 3.4V. So to drive the LED at 100mA you need a resistor of 34R. However the peak rating on a 1/10 duty cycle is 10 times the continuous rating. You actually want to drive it at a 50% duty cycle because you are modulating it therefore you could push the current to 200mA which would give you a resistor of 17R.

I note that this diode is emitting at 940nm, does this match the receiver you have.