Skip to content

Instantly share code, notes, and snippets.

@gsauthof
Created February 6, 2022 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsauthof/144e8fc4df89cf1d8cdaad8be3accda1 to your computer and use it in GitHub Desktop.
Save gsauthof/144e8fc4df89cf1d8cdaad8be3accda1 to your computer and use it in GitHub Desktop.
Arduino sketch for transmitting a raw IR remote control code without dependencies
// 2022, Georg Sauthoff <mail@gms.tf>
void setup() {
Serial.begin(9600);
Serial.println("Ready.");
cli(); // atomic section
// set Arduino Pin 3 low
PORTD &= ~ _BV(PORTD3);
// configure Arduino Pin 3 as output
DDRD |= _BV(DDD3);
// top, i.e. 38 kHz without prescaler, clock_cpu = 8 MHz
// i.e. (8 * 10**6) / (38 * 10**3)
OCR2A = 211;
// 23 % duty sycle
// NB: Arduino Pin 3 == OC2B
OCR2B = 48;
// normal mode, but prepare for fast PWM, i.e.
// clear OC2B on compare match, set OC2B on bottom, TOP=OCRA
TCCR2A = _BV(WGM21) | _BV(WGM20);
// CS (clock select) -> prescaler /1
TCCR2B = _BV(WGM22) | _BV(CS20);
sei();
}
// NB: Arduino's delayMicroseconds() just supports waiting
// up to 16383 µs
static uint16_t raw_code[] = {
618, 3648, 576, 3648, 3754, 469,
3754, 469, 576, 3648, 576, 3626,
576, 3626, 576, 3648, 576, 3648,
576, 3647, 576, 3648, 576, 3648,
576, 3648, 576, 3648, 3775, 448,
3776, 469, 3776, 469, 3754 // , 29225
};
static void enable_pulse()
{
// clear OC2B on compare match, set OSC2B on bottom
// NB: Arduino Pin 3 == OC2B
TCCR2A |= _BV(COM2B1);
}
static void disable_pulse()
{
// normal mode, disconnect OC2B
TCCR2A &= ~ _BV(COM2B1);
// set Arduino Pin 3 low
PORTD &= ~ _BV(PORTD3);
}
static void send_raw(const uint16_t *xs, uint8_t n)
{
for (uint8_t i = 0; i < n; ++i) {
if (i % 2 == 0)
enable_pulse();
else
disable_pulse();
delayMicroseconds(xs[i]);
}
disable_pulse();
}
void loop() {
if (Serial.read() != -1) {
send_raw(raw_code, sizeof raw_code / sizeof raw_code[0]);
Serial.println("sent");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment