Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 37 ยท Getting Started

Your First Program: Blinky

Write the "hello world" of embedded systems: configure a pin as output, toggle an LED, add delays, then see why busy-wait delays are bad and how non-blocking timing works.

Every programming language starts with "hello, world". In embedded systems the equivalent is blinky: make an LED blink. It sounds trivial, but it proves that your toolchain works, that the board gets power, that you can write to a pin, and that the clock is running at the speed you expect. When blinky works, you have crossed the hardest first hurdle.

In this lesson you will see blinky on three real platforms (as read-only code) and run a simulated version that you can modify. Then you will learn why the simple delay version is the beginning of a path to better ways of handling time.

An LED is wired from a GPIO pin (general-purpose input/output) through a resistor to ground (or to the supply). The pin is a tiny switch inside the chip that can be:

  • input: the chip reads whether the voltage on the pin is high or low;
  • output: the chip drives the pin high (close to supply voltage) or low (close to 0 V).

To blink you need three steps: configure the pin as an output, set it high, wait, clear it low, wait, and repeat.

flowchart TD
    A["Reset"] --> B["Configure clock"]
    B --> C["Configure pin as output"]
    C --> D["LED on"]
    D --> E["Wait 500 ms"]
    E --> F["LED off"]
    F --> G["Wait 500 ms"]
    G --> D

Blinky on three platforms (read-only)

These listings are for real boards; they cannot run in the browser. Compare the shapes: the steps are identical, only the names change.

Arduino (framework level):

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

void loop() {
    digitalWrite(LED_BUILTIN, HIGH);   // on
    delay(500);
    digitalWrite(LED_BUILTIN, LOW);    // off
    delay(500);
}

STM32 (HAL level, LED on pin PA5):

C
int main(void) {
    HAL_Init();
    SystemClock_Config();
    __HAL_RCC_GPIOA_CLK_ENABLE();      // power up the port

    GPIO_InitTypeDef cfg = {0};
    cfg.Pin = GPIO_PIN_5;
    cfg.Mode = GPIO_MODE_OUTPUT_PP;
    HAL_GPIO_Init(GPIOA, &cfg);

    for (;;) {
        HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
        HAL_Delay(500);
    }
}

Raspberry Pi Pico (Pico SDK):

C
#include "pico/stdlib.h"

int main(void) {
    const uint LED = 25;
    gpio_init(LED);
    gpio_set_dir(LED, GPIO_OUT);
    for (;;) {
        gpio_put(LED, 1);
        sleep_ms(500);
        gpio_put(LED, 0);
        sleep_ms(500);
    }
}

Three things to notice in all of them: there is a one-time setup, then an infinite loop; the loop never returns; and "time" is a function call (delay). On STM32 there is one extra step you must not forget: peripherals are clock-gated, which means a port does nothing until you enable its clock (__HAL_RCC_GPIOA_CLK_ENABLE). A blinky that "does nothing" is often missing exactly this line.

A simulated blinky you can run

The next program fakes a GPIO register and a millisecond clock, then runs the classic blink loop for three periods. "Delay" here advances a simulated clock instead of burning real time, so the output appears instantly.

C Blinky with a simulated pin and delay

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.

Try changing the delays to 100 and 900 ms. That asymmetric pattern gives a "heartbeat" style flash. Many boards use a short flash on, long off pattern as a "alive" indicator.

The problem with delay

delay_ms(500) on a real chip is usually a busy-wait: the CPU runs a counting loop, doing nothing useful, for half a second. While it waits, it cannot read a button, answer a message or run a second blinking LED. The program is blocked.

Imagine two LEDs: one blinking every 300 ms and another every 700 ms. With delay you would have to interleave the delays by hand, and the timing would drift whenever any other work took time. The professional answer is a non-blocking design: keep a free-running millisecond counter (a hardware timer interrupt increments it, as you will see in Interrupts and Timers), and instead of waiting, check "has enough time passed?".

C Two independent blinkers without blocking

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 expression now_ms - b->last_toggle is deliberate: unsigned subtraction gives the right elapsed time even when the 32-bit counter wraps around (after about 49.7 days for a millisecond counter). Comparing now_ms >= deadline would break at the wrap. Remember this idiom; it appears in almost every firmware code base.

Common blinky failures and what they teach

Symptom Likely cause
Nothing happens clock for the GPIO port not enabled; pin not set as output; wrong pin number
LED stays on dimly pin left as input, or wired through a resistor to the wrong rail
LED blinks at the wrong speed CPU clock is different from what the delay assumes (internal oscillator vs crystal)
Works with debugger, not alone code relies on the debugger to hold the chip out of reset or sets up a clock for you
LED polarity inverted the LED is wired to the supply, so low turns it on (active-low)

Blinky is therefore also a clock test: if you ask for 1 second and the LED takes 8 seconds, the chip is running on a slow default oscillator, which tells you where to look next (Clocks, Reset and Boot).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What are the three basic steps in blinky?

    You configure the GPIO as an output, then repeatedly drive it high and low with delays in between.
  2. 2. Why does a busy-wait delay cause problems in a larger program?

    While busy-waiting, the CPU cannot service buttons, communication or other timing tasks.
  3. 3. Why use (now_ms - last) >= period instead of now_ms >= last + period?

    With modular unsigned arithmetic the difference is right even across overflow of the 32-bit counter.
  4. 4. On many STM32 chips, a GPIO port does nothing until you do what?

    Peripherals are clock-gated to save power. Forgetting the clock enable is the classic reason a first program does nothing.

Summary

  • Blinky proves toolchain, power, pin control and clock in one small program.
  • The shape is always: one-time setup, then an endless loop.
  • delay calls busy-wait and block everything else; a free-running tick with elapsed-time checks lets many activities share the CPU.
  • Use unsigned subtraction for wrap-safe time comparisons.
  • Common failures (clock gating, wrong pin, polarity, oscillator speed) are worth recognizing early.

Next: Microcontroller vs Microprocessor