Built-in project · STM32 Blue Pill (F103)

Blue Pill lamp

A lamp on a Blue Pill that blinks in step with the board’s own LED, because both hang off 3.3 V and PC13 sinks them. The whole build, an STM32 Blue Pill and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $8.15. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Active-low outputs: LOW turns the LED on
  • Sinking current into a pin instead of sourcing it
  • Using LED_BUILTIN as a pin name
  • Why an LED needs a series resistor
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: STM32 Blue Pill (F103)
  • Parts: 4

How it works

The green LED and its 220 ohm resistor connect from the 3V3 pin to pin PC13. The LED lights when current can flow from 3.3 V down into PC13, so the pin has to be pulled LOW to switch it on.

The sketch does that on a loop: LOW for half a second, HIGH for half a second. The Blue Pill's own LED is wired the same way, so the two blink together.

Parts list

4 parts, plus the jumper wires. They retail for about $8.15; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
LED, green pin CSTM32 Blue Pill (F103) pin PC13
Resistor, 220 Ω pin 1STM32 Blue Pill (F103) pin 3V3
LED, green pin AResistor, 220 Ω pin 2

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · STM32 Blue Pill (F103)
// Blink: the smallest thing a board can do.
//
// The Blue Pill's green LED sits between the 3.3 V rail and PC13 through a
// 1 k resistor, so driving the pin LOW lights it and HIGH turns it off. That
// is the opposite of the Uno's, and it catches everybody once.
//
// Wire an LED and a 220 ohm resistor from PC13 to 3V3 to see the same thing on
// the breadboard.
//
// delay() parks the core on WFI between SysTick ticks rather than spinning, so
// this sketch costs the simulator almost nothing while it waits.

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

void loop() {
  digitalWrite(LED_BUILTIN, LOW);   // lit
  delay(500);
  digitalWrite(LED_BUILTIN, HIGH);  // dark
  delay(500);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Runs once. It calls pinMode to make LED_BUILTIN an output. On the Blue Pill that name stands for PC13, which is also where your breadboard LED is attached.

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

loop sketch.ino

Writes LOW to light both LEDs, waits 500 ms with delay, writes HIGH to turn both off, and waits again. Because the LEDs sit between 3.3 V and the pin, LOW and HIGH are the reverse of what you may expect.

void loop() {
  digitalWrite(LED_BUILTIN, LOW);   // lit
  delay(500);
  digitalWrite(LED_BUILTIN, HIGH);  // dark
  delay(500);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The LED is lit when it should be dark, or the other way around. This project
    That is normal here. The LEDs hang off 3V3, so LOW means on and HIGH means off.
  • The breadboard LED never lights. This project
    Check the direction. The anode (A) goes to the resistor from 3V3, and the cathode (C) goes to PC13.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • The LED does not light, or it lights once and then dies. LED
    LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.
  • A blue or white LED stays dark on 3.3 V. LED
    Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.
  • A 5 V signal is connected to an analog or other pin and the chip is at risk. STM32 Blue Pill (F103)
    The Blue Pill runs at 3.3 V. Only some pins are 5 V tolerant, and the ADC pins are not. Keep analog input between 0 and 3.3 V, and use dividers or level shifters.
  • The onboard LED seems reversed. STM32 Blue Pill (F103)
    The LED on PC13 is wired to 3.3 V, so it lights when the pin is LOW. Write LOW to turn it on and HIGH to turn it off.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.