Skip to content

Embedded Systems

The Arduino Blink sketch, explained line by line

Blink is the first Arduino sketch: it switches the board's built-in LED, L on pin 13, on and off once a second. pinMode() makes the pin an output, digitalWrite() sets it HIGH (5 V, on) or LOW (0 V, off), and delay() waits a number of milliseconds. loop() repeats the four lines forever.

void setup() {
  pinMode(13, OUTPUT);     // pin 13 drives L
}

void loop() {
  digitalWrite(13, HIGH);  // 5 V: on
  delay(500);              // wait half a second
  digitalWrite(13, LOW);   // 0 V: off
  delay(500);
}

Line by line

  • pinMode(13, OUTPUT); Every pin starts as an input, only listening. This makes pin 13 an output that can drive the LED. It runs once, in setup().
  • digitalWrite(13, HIGH); Pin 13 goes to 5 V and L lights.
  • delay(500); The board does nothing for 500 ms, half a second.
  • digitalWrite(13, LOW); Pin 13 goes to 0 V and L goes dark.
  • delay(500); Another half second, then loop() starts again from the top.

How fast does it blink?

One blink is the on time plus the off time, and the blink frequency is how many fit in a second:

f = 1 over on time + off time
WaitsOne blinkFrequency
500 and 500 ms1 s1 Hz
250 and 250 ms0.5 s2 Hz
100 and 100 ms0.2 s5 Hz
10 and 10 ms0.02 s50 Hz

Live experiment

Faster and slower

The Blink sketch's loop, running: Pip plays L on pin 13. Change both waits at once and watch the line the board is on, and a strip of the last two seconds of HIGH and LOW. Shrink the waits to 10 ms and see what your eye makes of 50 blinks a second.

Try it yourself: free lesson

“Blink” is lesson 2 of 6 in Blink Bay, Embedded Systems World 1. Free with an account. Lessons unlock in order, so you’ll do the one before it first.

Uneven blinks

The two waits don't have to match. On for 100 ms and off for 400 ms is still 500 ms a blink, 2 Hz, but L is on for only a fifth of each one: a short heartbeat flash. Give each wait its own name so it's easy to tune:

int on = 100;
int off = 400;

void loop() {
  digitalWrite(13, HIGH);
  delay(on);
  digitalWrite(13, LOW);
  delay(off);
}

Too fast to see

At 10 ms on and 10 ms off, L blinks 50 times a second. Your eye can't follow that, so it sees the average: a steady glow about half as bright. That's the idea behind PWM, which dims LEDs by switching them far faster still.

Free lesson

Now build it yourself

Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.

Try it yourself: free lesson

“Blink” is lesson 2 of 6 in Blink Bay, Embedded Systems World 1. Free with an account. Lessons unlock in order, so you’ll do the one before it first.

Or start from the very first lesson, no account needed

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3delay(2000) makes the board…
    Show the answer

    Answer: Wait two seconds. delay() counts in milliseconds, thousandths of a second. 2000 of them make 2 seconds.

    • Why not “Blink twice”? delay() only waits. The blinking comes from the digitalWrite lines before and after it.
    • Why not “Wait 2000 seconds”? delay() counts in milliseconds, so 2000 is 2 seconds. 2000 seconds would be over half an hour.
    • Why not “Stop for good”? delay() pauses for the time it's given, then the program carries on with the next line.
  2. Question 2 of 3int wait = 250; then later delay(wait); The board waits…
    Show the answer

    Answer: 250 ms. wait is a name for the number 250. delay(wait) is delay(250), a quarter of a second.

    • Why not “nothing, wait is a word”? wait is a variable holding 250, so delay(wait) does exactly what delay(250) does.
    • Why not “until wait changes”? delay() reads wait's value when it's called and waits that long. Changing wait later only affects later delays.
    • Why not “250 s”? delay() counts milliseconds, so delay(250) is a quarter of a second.
  3. Question 3 of 3digitalWrite(13, LOW) makes pin 13…
    Show the answer

    Answer: 0 V, so L is off. LOW is the off position of the switch, 0 V. HIGH is 5 V, on.

    • Why not “5 V, so L is on”? 5 V is HIGH. LOW puts the pin at 0 V, which switches L off.
    • Why not “blink”? One digitalWrite sets the pin once. A blink needs HIGH and LOW in turn, with waits between them.
    • Why not “an input”? pinMode sets whether a pin is an input or an output. digitalWrite only sets an output's level.
Keep practising: free lesson

“Blink” is lesson 2 of 6 in Blink Bay, Embedded Systems World 1. Free with an account. Lessons unlock in order, so you’ll do the one before it first.

Questions people ask

How do I make the Arduino LED blink faster?

Make the delays shorter. The time for one blink is the on time plus the off time, so delay(250) twice gives 500 ms a blink, twice a second. delay(100) twice gives five blinks a second.

What pin is LED_BUILTIN?

On an Arduino Uno it's pin 13, which drives the small LED marked L on the board. Other boards put their built-in LED on different pins, which is why LED_BUILTIN is better than writing 13.

Why does a very fast blink look like a steady dim light?

Your eye can't follow flicker much faster than about 50 to 60 times a second, so it sees the average. An LED on half the time at 50 Hz looks about half bright. That's how PWM dimming works.

Every topic