
Most infrared remote controls emit modulated light pulses to activate an infrared receiver. Using modulated IR light helps prevent stray light from messing with reception. Pulses at around 38kHz and 56kHz are common, but many values are used. The USB IR Toy’s transmitting LED is driven by a pulse-width modulator (PWM) that can be adjusted to match many modulations. This post shows how to calculate custom modulation values.
You can get a USB IR Toy for $20, including worldwide shipping. Seeed Studio currently has them available on backorder, additional units should arrive soon.
The best tool we’ve found is this online PIC PWM calculator. Enter the desired frequency and it calculates the best values to configure the PWM. The IR Toy clock is 48Mhz and the duty cycle is always 50% (by bit shift right). Currently, the prescaler is fixed at x4, but we could always make it configurable in the future. Here’s where the magic happens in the code.
Frequency Resolution Prescaler PR2 37500.00 10 ÷4 0b01001111 37974.68 10 ÷4 0b01001110 38461.54 10 ÷4 0b01001101 38961.04 10 ÷4 0b01001100 39473.68 10 ÷16 0b00010010
By request, we calculated the settings to modulate the IR LED at 38.4kHz. The value to send to set the IR PWM is PR2. The closest setting to our requested value is 38.4615kHz with a PR2 value of 0b01001101, or 77 (0x4D). Note that the prescaler is fixed at 4x, so only setting based on a prescaler of /4 will work (39.473 for example is based on a 16x prescaler and is not valid).
The complete command to set the IR modulation frequency is:
0x02 0x4d 0x00
0x02 is the setup TX command
0x4d is the value from the table
0x00 is a don’t care byte (doesn’t matter)
