Blink: your first firmware
About this lesson
Blink is the first program almost everyone runs on a new board: an LED that turns on for a second, then off for a second, forever. Follow the nine-line Arduino sketch on an Arduino Uno R3, from pressing Upload to the pin changing voltage. See how pinMode() and digitalWrite() set two bits inside the chip, DDRB5 and PORTB5, and how delay() waits a set time measured from the 16 MHz clock. Then change one number and make it blink faster. The lesson “setup() and loop(): the superloop” explains why loop() repeats; the C++ basics lessons cover the language.
Hello, world for chips
Blink is the first firmware most people write: an LED goes on for a second, off for a second, forever. If it blinks, your board, cable and tools all work. Ours is an Arduino Uno R3 with an LED on pin 13.
Blink the "hello, world" of microcontrollers
board Arduino Uno R3 (ATmega328P, 16 MHz)
LED on pin 13, through a 220 ohm resistor
(the board's own "L" LED is on pin 13 too)
it blinks -> board, cable and toolchain all workRead the lesson step by step
Hello, world for chips
Blink is the first firmware most people write: an LED goes on for a second, off for a second, forever. If it blinks, your board, cable and tools all work. Ours is an Arduino Uno R3 with an LED on pin 13.
Blink the "hello, world" of microcontrollers board Arduino Uno R3 (ATmega328P, 16 MHz) LED on pin 13, through a 220 ohm resistor (the board's own "L" LED is on pin 13 too) it blinks -> board, cable and toolchain all workNine lines of C++
Here is the whole program, called a sketch, in Arduino C++. setup() runs once when the board starts. Then loop() runs again and again, as the lesson on setup() and loop() showed.
sketch an Arduino program setup() once at power on loop() again and again, forever // Arduino Uno R3: LED + 220 ohm resistor on pin 13 (LED_BUILTIN) void setup() { pinMode(13, OUTPUT); } void loop() { digitalWrite(13, HIGH); delay(1000); digitalWrite(13, LOW); delay(1000); }Upload it to flash
Press Upload. The IDE compiles the sketch into machine code and sends it down the USB cable. The bootloader, a small program already on the chip, writes it into flash. The lesson "From code to chip: compile, link and flash" shows each stage.
Upload compile -> machine code -> USB -> board USB the ATmega16U2 on the board turns USB into serial loader the bootloader (optiboot) writes it into flash flash keeps the program with the power off reset the chip restarts and runs setup() more lesson: From code to chip: compile, link and flashMake pin 13 an output
Line 2: pinMode(13, OUTPUT). After a reset every pin is an input. Inside the chip, pin 13 is bit 5 of port B. Writing 1 to its direction bit, DDRB5, makes it an output the chip can drive.
line 2 pinMode(13, OUTPUT); pin 13 = port B, bit 5 (PB5) DDRB5 0 -> 1 1 = output, 0 = input at reset every pin starts as an inputHIGH: the LED lights
Line 5: digitalWrite(13, HIGH) sets the output bit PORTB5 to 1, and pin 13 goes to 5 volts. Current flows out of the pin, through the resistor and the LED, to ground. The LED lights.
line 5 digitalWrite(13, HIGH); PORTB5 0 -> 1 pin 13 5 V current pin 13 -> 220 ohm -> LED -> ground LED onWait one second, lit
Line 6: delay(1000) waits 1,000 milliseconds, one second. At 16 MHz, one second is 16 million clock ticks. The program does nothing else, and pin 13 stays at 5 volts, so the LED stays on.
line 6 delay(1000); 1,000 ms = 1 s 16 MHz x 1 s = 16,000,000 ticks pin 13 stays 5 V: LED onLOW: the LED goes dark
Line 7: digitalWrite(13, LOW) sets PORTB5 back to 0. Pin 13 drops to 0 volts, the same as ground, so no current flows and the LED goes dark.
line 7 digitalWrite(13, LOW); PORTB5 1 -> 0 pin 13 0 V, same as ground current none LED offWait one second, dark
Line 8: delay(1000) waits one more second with the LED off. Then loop() reaches its closing brace on line 9.
line 8 delay(1000); another 16,000,000 ticks pin 13 stays 0 V: LED off line 9 } end of loop()And again, forever
loop() ends and starts again at line 5. One blink takes 2 seconds: 1 on, 1 off. That is 30 blinks a minute, 0.5 per second (0.5 hertz). The plot shows pin 13 over 4 seconds.
line 9 -> line 5, again and again period 1000 ms on + 1000 ms off = 2 s rate 1 / 2 s = 0.5 Hz = 30 blinks a minute plot pin 13: 5 V, 0 V, 5 V, 0 V ...Change it: blink faster
Change both delays to 200 and upload again. Now one blink takes 0.4 seconds: 2.5 blinks a second. Edit, upload, watch the board: that is how all firmware work goes.
delay(200); on lines 6 and 8 period 200 + 200 ms = 0.4 s rate 1 / 0.4 s = 2.5 Hz edit -> upload -> watch, again and again
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01In the Blink sketch, which line runs only once?
pinMode() is inside setup(), which runs once when the board starts. The other three lines are inside loop(), which runs again and again.
02On an Arduino Uno R3, what does digitalWrite(13, HIGH) do once pin 13 is an output?
HIGH sets the output bit PORTB5 to 1, and the pin is driven to 5 V. Current flows from the pin, through the 220 Ω resistor and the LED, to ground, and the LED lights.
03Who writes your program into the chip's flash memory when you press Upload?
The IDE compiles the sketch and sends it over USB. The bootloader, already in flash, receives it and writes it into flash; then the chip restarts and runs your program.
04With delay(1000) after HIGH and delay(1000) after LOW, how many times does the LED blink in one minute?
One blink is 1000 ms on plus 1000 ms off = 2 s. A minute has 60 s, and 60 / 2 = 30.
05What does pinMode(13, OUTPUT) change inside the ATmega328P?
After a reset every pin is an input. pinMode(13, OUTPUT) writes 1 to DDRB5, the direction bit for pin 13 (port B, bit 5). Only then can digitalWrite() drive the pin: it sets PORTB5, the output bit.
Glossary
- Sketch
- An Arduino program. It has a setup() function that runs once and a loop() function that runs forever.
- Upload
- Compiling a sketch into machine code and sending it over USB into the board's flash memory.
- Bootloader
- A small program kept in the chip's flash that receives a new program over USB and writes it into flash.
- Output pin
- A pin the chip drives itself: on an Uno, 5 V when HIGH and 0 V when LOW.
- HIGH and LOW
- The two levels of a digital pin. On an Uno, HIGH is 5 V and LOW is 0 V (ground).
- Register bit
- One bit inside the chip that controls hardware. On the ATmega328P, DDRB5 sets pin 13 as an output and PORTB5 sets its level.
- delay()
- An Arduino function that pauses the program for a number of milliseconds. Nothing else in the program runs meanwhile.
- Hertz (Hz)
- Times per second. An LED that blinks once every 2 seconds blinks at 0.5 Hz.
Notes and sources
- The original Blink example writes LED_BUILTIN instead of 13. On the Uno, LED_BUILTIN is defined as 13, so the board's own "L" LED blinks along with yours. On other boards it can be a different pin.
- Each blink takes very slightly more than 2 s: digitalWrite() and the jump back to the top of loop() also take a little time, far less than a millisecond at 16 MHz.
- The resistor keeps the current safely low. An ATmega328P pin must never carry more than 40 mA (the datasheet's absolute maximum); the lesson "Pin current limits and driving loads with a transistor" shows how to work out the resistor.
- delay() keeps the chip busy waiting: nothing else in your program runs meanwhile. The lesson "Non-blocking code: replacing delay()" replaces it with millis().
- delay() does not count every one of the 16,000,000 ticks itself. The Arduino core runs Timer0 at the clock divided by 64 and keeps time from it; delay() waits until enough of that time has passed. The lesson "Hardware timers, prescalers and millis()" shows how.
- Arduino docs: Blink (built-in example, source)
- Arduino language reference: pinMode() (source)
- Arduino language reference: digitalWrite() (source)
- Arduino language reference: delay() (source)
- Arduino AVR core: pins_arduino.h (LED_BUILTIN 13 = PB5)
- Arduino AVR core: boards.txt (Uno clock, upload tool and bootloader)
- Microchip ATmega328P datasheet (DS40002061B), I/O-Ports: Configuring the Pin
- Arduino UNO R3 product datasheet (A000066)
- J. W. Valvano, Embedded Systems: Shape the World, ch. 6: Microcontroller Ports (UT Austin)
Quiz Unlocks after the last step.