Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 37 ยท Getting Started

Your First Sketch: Blink

Build and upload the classic Blink sketch: wire an external LED, understand pinMode, digitalWrite and delay, and learn the common reasons a first upload fails.

Blink is the "Hello, World" of hardware. It switches an LED on and off once per second. Simple as it is, it exercises the whole workflow: write a sketch, compile it, upload it, watch the board react. Once you have seen an LED blink because of code you wrote, everything else in this course is a variation.

You will need an Arduino board (an Uno is assumed), a USB cable and the free Arduino IDE (The Arduino IDE and CLI covers installing it). An external LED and resistor are optional because most boards have a built-in LED.

The built-in LED

Most boards have a small LED soldered next to pin 13 (labelled "L" on an Uno). The core defines the name LED_BUILTIN for it, so the same sketch works on boards where the LED sits on a different pin.

C++
void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

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

Line by line:

  • pinMode(LED_BUILTIN, OUTPUT); configures the pin as an output. Without this the pin would stay an input and the LED would not light properly.
  • digitalWrite(pin, HIGH) drives the pin to the supply voltage (5 V on an Uno), which lights the LED. LOW drives it to 0 V, which turns the LED off.
  • delay(1000) pauses the program for 1000 milliseconds.

In the IDE: choose your board and port, press Upload. The IDE compiles the sketch and sends it over USB. After a few seconds the "L" LED begins to blink, and it keeps blinking after you unplug the cable and power the board from a battery.

Wiring an external LED

To blink your own LED, build the circuit from Electronics Basics: pin 9 to a 220 ฮฉ resistor, the resistor to the LED's long leg (anode), and the short leg (cathode) to GND.

C++
const int ledPin = 9;

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

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

We changed three things: a named constant instead of a magic number, a different pin, and a faster half-second rhythm (two blinks per second). Named constants are an important habit. When you later move the LED to pin 10, you change one line.

C Stand-in: simulate Blink on a virtual timeline

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The output is the timeline a real board would produce, because the stand-in has the same sketch logic and a fake clock that jumps instead of waiting.

Changing the pattern

The only rules for the pattern are the lengths of the delays. Morse code for SOS is three short, three long, three short:

C Stand-in: SOS pattern as a timeline

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

On a real board you would paste the flash() function into a sketch and call digitalWrite() inside it. Breaking repeated behavior into functions keeps loop() short and readable.

When the upload fails

First uploads often fail for boring reasons:

Symptom Likely cause Fix
Port menu is grey or empty Cable carries power only, or driver missing Try another cable and port; install the board's USB driver
"avrdude: stk500_getsync(): not in sync" Wrong board or port selected Check Tools > Board and Tools > Port
"Permission denied" on Linux Your user cannot open the serial port Add yourself to the dialout group and log in again
Uploads but nothing blinks LED wired backwards, or wrong pin Flip the LED; verify the pin number
Compile error "expected ';'" Typo in the sketch Read the line number in the error and fix it

Read compile errors from the top. The first error is usually the real one; later ones are often consequences.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. What does pinMode(9, OUTPUT) do?

    pinMode only configures direction. digitalWrite then sets the level.
  2. 2. Why is LED_BUILTIN useful?

    The pin number differs between boards; the core defines the right one.
  3. 3. In the external LED circuit, what is the long LED leg connected to?

    The anode (long leg) is on the positive side, through a resistor to the pin; the cathode goes to GND.

Summary

  • Blink uses pinMode, digitalWrite and delay to toggle an LED.
  • LED_BUILTIN names the on-board LED; use named constants for pins.
  • An external LED needs a series resistor and the right polarity.
  • Most failed uploads come from a wrong board or port selection, bad cable or typo.
  • Our C stand-ins model the timeline, not the hardware.

Next: Sketch Anatomy: setup() and loop()