Built-in project · STM32F411 Black Pill

STM32 tilt lamp

A tilt switch on PB0 of a Black Pill lights a lamp on PC13 while it stands upright. Tip it and the lamp goes dark. The whole build, an STM32 Black Pill and 3 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 $9.95. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Reading a switch with INPUT_PULLUP
  • Active-low LEDs
  • Tilt switches as simple sensors
  • Using the board's LED_BUILTIN pin
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: STM32F411 Black Pill
  • Parts: 5

How it works

The tilt switch is wired between PB0 and ground. The sketch turns on the chip's internal pull-up, so PB0 sits at 3.3 V until the switch closes and pulls it to ground. A closed switch reads LOW.

The lamp is a blue LED with its anode on 3V3 through a 220 Ω resistor and its cathode on PC13. That makes it active low: it lights when PC13 is LOW. The sketch sets PC13 LOW while the switch is closed, so the lamp is on, and HIGH when it is open, so the lamp is off.

Parts list

5 parts, plus the jumper wires. They retail for about $9.95; 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
Resistor, 220 Ω pin 1STM32F411 Black Pill pin 3V3
Tilt switch pin 2STM32F411 Black Pill pin GND
Tilt switch pin 1STM32F411 Black Pill pin PB0
LED, blue pin CSTM32F411 Black Pill pin PC13
LED, blue 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 · STM32F411 Black Pill
// Button: a pushbutton on PB0 lights the on-board LED.
//
// INPUT_PULLUP switches on the STM32's 40 k internal pull-up, so the pin sits
// at 3.3 V until the button pulls it to GND. That is why the button reads LOW
// when it is pressed: wire one side to PB0 and the other to GND, no resistor.
//
// The LED is active low (see the blink example), so a press drives PC13 LOW.

const int BUTTON_PIN = PB0;

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
}

void loop() {
  bool pressed = digitalRead(BUTTON_PIN) == LOW;
  digitalWrite(LED_BUILTIN, pressed ? LOW : HIGH);
  delay(5);
}

Code walkthrough

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

setup sketch.ino

Sets BUTTON_PIN (PB0) as an input with the internal pull-up and makes LED_BUILTIN (PC13) an output. It starts with the pin HIGH, which turns the lamp off.

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
}

loop sketch.ino

Reads PB0: LOW means the switch is closed, so pressed is true. Then it writes LOW to the lamp pin if pressed, otherwise HIGH. A 5 ms delay ends each pass.

void loop() {
  bool pressed = digitalRead(BUTTON_PIN) == LOW;
  digitalWrite(LED_BUILTIN, pressed ? LOW : HIGH);
  delay(5);
}

Troubleshooting

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

  • The lamp is on when you expect it off This project
    The lamp is active low. LOW lights it, so check which position of the switch closes the circuit.
  • The lamp never lights This project
    Check the LED: cathode (C) to PC13, anode (A) through the 220 Ω resistor to 3V3.
  • 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.
  • The reading flickers when the switch is moved. Tilt switch
    The ball bounces on the contacts, giving many fast changes. Debounce in software by waiting 20 to 50 ms before accepting a new state, or average a few readings.
  • The input floats and never gives a clean reading. Tilt switch
    A tilt switch is just a switch, so the pin floats when it is open. Use INPUT_PULLUP with the switch between the pin and GND, or add a 10 k pull-down resistor.

Change it and keep it

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