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.

StarterRuns in your browser. Free, and no account needed.

STM32 tilt lamp · STM32F411 Black Pilllive0.000 s 0.00x
Hold the button
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

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);
}

Parts list

5 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

5 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • STM32F411 Black Pill pin PC13; LED, blue pin C
  • STM32F411 Black Pill pin PB0; Tilt switch pin 1
  • Ground: STM32F411 Black Pill pin GND; Tilt switch pin 2
  • STM32F411 Black Pill pin 3V3; Resistor, 220 Ω pin 1
  • Resistor, 220 Ω pin 2; LED, blue pin A

Change it and keep it

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