Home · Academy · Robotics & Coding · Arduino · First Code: Blink

First Code: Blink

Write your first sketch to blink an LED with pinMode, digitalWrite and delay.

LESSON COMPASS

What will you use this page for?

Core idea

Blink is the first program that turns the board’s built-in LED on and off at set intervals, and it teaches the pinMode , digitalWrite and delay commands.

Evidence to produce

Complete the page task with your own input, test conditions and reasoning.

Control trap

Forgetting pinMode If you do not write pinMode(..., OUTPUT) inside setup , the pin is not set as an output and the LED may not work as expected. Every output pin must be set up first. Forgetting the semicolon In Arduino, every command line ends with ; . If you write delay(1000) without a semicolon, the code will not…

Next connection

Digital Input and Output: We will control an LED with a button and learn to read information from the board with digitalRead .

Module sources: Python Tutorial · Arduino Learn

LevelBeginner
Age10–16
Duration30–45 min
PrerequisiteThe Arduino IDE
ContentStandard lesson · 1,489 words
Last updated

One-sentence summary

Blink is the first program that turns the board’s built-in LED on and off at set intervals, and it teaches the pinMode, digitalWrite and delay commands.

Why it matters

When you start a new instrument, you usually play a single note first. Coding has a similar beginning: Blink. The goal is not to build a complex robot, but to make sure your code really runs on the board.

Blink looks small, but it shows three big things at once:

In the previous lesson you installed and explored the Arduino IDE. Now we will produce a real output in that environment. When an LED starts blinking, it means “code and hardware are now talking to each other.” Every lesson after this builds on that first bridge.

Every Arduino program has two basic parts. Blink has them too.

void setup()

The setup block runs once when the board powers on. Here we make our starting settings. For example, we decide whether a pin will be an input or an output here.

It is like preparing your desk before an exam: you place your pen, eraser and water once. You do not set them up again and again during the exam.

void loop()

The loop block runs over and over forever after setup finishes. When it reaches the end, it starts again from the top. Blink’s on-and-off work happens here.

It is like a swing: you push it, it comes back, you push again. This cycle does not stop until the board is turned off.

The built-in LED and pin 13

On most Arduino Uno boards, a tiny LED is soldered to pin 13. It usually has an L printed next to it. Thanks to this LED, we can test our first code without connecting any external parts. Instead of writing the pin number, we can also use the ready-made name LED_BUILTIN.

The code below turns the built-in LED on for one second and off for one second.

// Blink program that turns the built-in LED on and off
void setup() {
  pinMode(LED_BUILTIN, OUTPUT); // make pin 13 an output
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
  delay(1000);                     // 1000 ms = 1 second wait
  digitalWrite(LED_BUILTIN, LOW);  // turn the LED off
  delay(1000);                     // wait 1 more second
}

Line by line, what happens?

Because the loop block automatically starts again when it ends, the LED keeps blinking.

Changing the timing

The number inside delay is in milliseconds. 1 second = 1000 milliseconds. You can adjust the speed by changing this number.

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(200);   // on for a very short time
  digitalWrite(LED_BUILTIN, LOW);
  delay(200);   // off for a very short time
}

If you write 200, the LED blinks several times per second, so it blinks faster. If you write 2000, it slows down.

Connecting an external LED (variation)

After the built-in LED works, you can connect your own LED on the outside. Instead of connecting an LED directly to the Arduino, we must always use a resistor. The resistor limits the current and prevents the LED from burning out.

Parts you need

Wiring

Code

The logic of the code is exactly the same as the built-in LED; only the pin number changes.

int ledPin = 8; // external LED on pin 8

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  digitalWrite(ledPin, HIGH);
  delay(500);
  digitalWrite(ledPin, LOW);
  delay(500);
}

If the LED does not light up, first check the long/short leg direction. An LED only works in one direction.

Mini practice

Write your own “heartbeat” pattern. Make the LED blink twice quickly, then pause for a long time. Try these steps:

  1. Open the Blink code.
  2. Add two quick blinks inside loop (use delay(150)).
  3. Put a long delay(800) at the end.
  4. Upload it to the board and watch the pattern.

Starter template:

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(150);
  digitalWrite(LED_BUILTIN, LOW);
  delay(150);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(150);
  digitalWrite(LED_BUILTIN, LOW);
  delay(800); // long pause
}

Change the numbers until you like the pattern. There is no single right answer; the goal is to practise building rhythm with digitalWrite and delay.

Common mistakes

Forgetting pinMode

If you do not write pinMode(..., OUTPUT) inside setup, the pin is not set as an output and the LED may not work as expected. Every output pin must be set up first.

Forgetting the semicolon

In Arduino, every command line ends with ;. If you write delay(1000) without a semicolon, the code will not compile and will give an error.

Never using delay

Without delay, the LED blinks so fast that it looks like it is always on. You must add a pause to see the blinking.

Connecting an external LED without a resistor

Without a resistor, too much current flows through the LED. The LED can be damaged or the pin can be strained. Always use a current-limiting resistor with an external LED.

Connecting the LED backwards

An LED has a direction. The long leg goes to plus and the short leg goes to ground (GND). If you connect it backwards, the LED will not light at all.

Safety note

Lesson summary

Check questions

  1. How many times does the setup block run, and how many times does the loop block run?
  2. What is the job of the line pinMode(LED_BUILTIN, OUTPUT);?
  3. What is the difference between digitalWrite(LED_BUILTIN, HIGH) and LOW?
  4. If you write delay(2000), how does the LED’s blinking speed change?
  5. Why do we use a resistor when connecting an external LED?

Answers

  1. setup runs only once when the board powers on, and loop runs over and over forever after setup finishes.
  2. It sets the given pin as an output, so we can send voltage out through that pin and drive the LED.
  3. HIGH applies voltage to the pin and turns the LED on; LOW cuts the voltage and turns the LED off.
  4. Because delay(2000) waits two seconds, the LED blinks more slowly (2 seconds in each state).
  5. The resistor limits the current; without it, too much current flows through the LED and the LED or the pin can be damaged.

Source and verification note

For “First Code: Blink”, verification focuses on whether the relationship between The structure of Blink: setup and loop and void loop() remains consistent across examples. Pin, voltage and current limits can differ between Arduino-compatible boards. Compiling code does not guarantee a safe circuit; loads such as motors and servos require a suitable driver and external power where appropriate.

Next lesson

Digital Input and Output: We will control an LED with a button and learn to read information from the board with digitalRead.

Start QuizBack to Arduino
QUESTION POOL

Reinforce this lesson with 10 questions

This lesson has a pool of 20 questions. Each attempt selects 10 and reshuffles the choices.