LESSONEmbedded C · Part 7EP.159 · 24 s

Embedded C Part 7: LED ON with 2 lines of registers

// WHAT YOU WILL LEARN

DDRB |= (1<<5); PORTB |= (1<<5); lights D13 (= PB5) without digitalWrite.

// LESSON NOTES

Turn on an LED with NO Arduino functions — just 2 lines of C! 💡

The Uno's pin 13 LED is really PB5: port B, pin 5 on the ATmega328P.

DDRB |= (1 << DDB5); // bit 5 = 1 → output
PORTB |= (1 << PORTB5); // bit 5 = 1 → HIGH, LED on ✅

Why |= and not = ?
'=' overwrites all 8 bits (wipes the others) ❌
'|=' sets only bit 5 and keeps the rest ✅

No digitalWrite — just the chip's own switches: registers.

TESTED CODE · ec07_led_on_registers.c · avr-gcc
// EC L07 - LED on D13 with 2 lines, no digitalWrite
#include <avr/io.h>

int main(void) {
    DDRB  |= (1 << DDB5);    // PB5 -> output
    PORTB |= (1 << PORTB5);  // PB5 -> HIGH, LED ON
    while (1) { }
}
Read the voiceover (transcript)
  1. No Arduino functions. Just two lines!
  2. The Uno's pin thirteen LED is really port B, pin five.
  3. Line one: DDRB bit five becomes one. Now it's an output.
  4. Line two: PORT B, bit five, becomes one. The pin goes high!
  5. And the LED lights up!
  6. The or-equals changes only bit five. The rest stay.
  7. No digital write, just the chip's own switches!
  8. Next, Part eight: the bit shift trick. Follow for Part eight!

Related lessons