Look below the sketch

You can skip this page. It shows what pinMode and digitalWrite do inside the chip.

The chip keeps small memory slots called registers. Each one holds 8 bits, and a bit is 0 or 1.

In the pin registers, each bit belongs to one pin6. Pin 13 is bit 5 of the register called PORTB1. PORTB sets each pin HIGH or LOW, and DDRB sets which pins are outputs6. A 1 in DDRB bit 5 makes pin 13 an output2.

PORTB bits 7 to 0, with the port pin, Arduino pin and chip pin each one drives
PORTB bit76543210
Port pinPB7PB6PB5PB4PB3PB2PB1PB0
Arduino pinnonenone1312111098
Chip pin109191817161514
PORTB bits 7 to 0, with the port pin, Arduino pin and chip pin each one drives
PORTB bitPort pinArduino pinChip pin
7PB7none10
6PB6none9
5PB51319
4PB41218
3PB31117
2PB21016
1PB1915
0PB0814
Pin 13 is bit 5 of PORTB, also called PB51. On the 28-⁠pin chip it is pin 192. Bits 6 and 7 go to chip pins 9 and 10, named XTAL1 and XTAL22. They have no Arduino pin number1.

The same blink, two ways

l1-blink-arduino.ino
// Lesson 1, Arduino version.
// Blinks the UNO R3's LED on pin 13: 1 s on, 1 s off.
void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}
l1-blink-registers.ino
// Lesson 1, register version: the same blink, written
// directly to the port registers.
// Pin 13 is PORTB bit 5 (PB5) on the UNO R3.
void setup() {
  DDRB |= (1 << DDB5);       // pin 13 is an output
}

void loop() {
  PORTB |= (1 << PORTB5);    // pin 13 HIGH
  delay(1000);
  PORTB &= ~(1 << PORTB5);   // pin 13 LOW
  delay(1000);
}

|= with (1 << PORTB5) sets bit 5 and leaves the other bits alone. &= ~ with the same mask clears bit 5 only.

Registers in this lesson (ATmega328P data-⁠space addresses)2

RegisterAddressBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
DDRB0x24DDB7DDB6DDB5DDB4DDB3DDB2DDB1DDB0
PORTB0x25PORTB7PORTB6PORTB5PORTB4PORTB3PORTB2PORTB1PORTB0
PINB0x23PINB7PINB6PINB5PINB4PINB3PINB2PINB1PINB0

Watch the bit

Switch between the two programs. The register panel shows the same PORTB5 bit turning on and off in both.

Sources for this page

  1. ArduinoCore-⁠avr, variants/standard/pins_arduino.h: LED_BUILTIN is 13; pin 13 is PB5
  2. Microchip ATmega328P datasheet DS40002061A: Pin configurations (p12); I/O ports; PORTB and DDRB at p100
  3. Arduino: Port Manipulation (retired page, written for the ATmega8/168): DDRx, PORTx and PINx