LESSONEmbedded C · Part 6EP.105 · 24 s

Embedded C Part 6: What is a register?

// WHAT YOU WILL LEARN

DDRB / PORTB / PINB: 8 switches each — direction, output, input — that control port B's pins.

// LESSON NOTES

What is a register, really? 🤔 It's the secret behind every pin of your Arduino 👇

1️⃣ 8 TINY SWITCHES: a register is a row of 8 bits — 8 tiny switches — inside the ATmega328P chip. On port B, each bit controls one pin: PB0 = D8 … PB5 = D13 (PB6 and PB7 run the crystal on the Uno).

2️⃣ DDRB + PORTB: DDRB sets the direction (1 = output). PORTB sets the level (1 = HIGH, 5 V).
DDRB = 0b00100000;
PORTB = 0b00100000;
→ pin D13's LED turns on.

3️⃣ PINB: reads what each pin really sees. Bit 5 = PB5 = pin D13 = the on-board LED.

Direction, output, input — 3 registers per port!

Try it: paste the code into the Arduino IDE (a sketch with its own int main) or a free online Uno simulator.

TESTED CODE · ec06_registers.c · avr-gcc
// EC L06 - three registers control every pin of port B
#include <avr/io.h>

int main(void) {
    DDRB  = 0b00100000;  // Direction: 1 = output (PB5 = D13)
    PORTB = 0b00100000;  // Output   : 1 = HIGH (5 V)
    uint8_t now = PINB;  // Input    : read real pin levels
    (void)now;
    while (1) { }
}
Read the voiceover (transcript)
  1. What is a register, really?
  2. It's a row of eight tiny switches inside the chip. Each bit controls one pin!
  3. D D R B sets direction: one is output. Port B sets the level: one is five volts.
  4. And pin B reads what each pin really sees. Bit five is pin thirteen, the LED!
  5. Direction, output, input!
  6. Next: LED on with two lines! Follow for part seven!

Related lessons