Projects / Black Pill parking assist beeper

Black Pill parking assist beeper

Intermediate2.5 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. STM32ST

    Program the board

    Use the ST-Link, then unplug its 3.3V wire.

  2. STM32

    Wire the sensor

    VCC to 5V, GND, Trig to PB0.

  3. STM32

    Build the echo divider

    Echo through 1 kilo-ohm to PB1, and 2 kilo-ohms from PB1 to GND.

  4. STM32

    Wire the buzzer

    PB8 through 330 ohms to the plus, minus to GND.

  5. STM32

    Power from USB

    The sensor starts measuring.

  6. STM32

    Move a hand or a board towards the sensor

    The beeps speed up.

  7. STM32

    Come within 15 cm

    The tone becomes steady.

In short

How do you build a black Pill parking assist beeper?

A Black Pill measures distance with an HC-SR04 and beeps faster as an object gets closer, for a garage wall or a model car. You need STM32 Black Pill (STM32F411CEU6), ST-Link V2 programmer (clone), HC-SR04 Ultrasonic Sensor, Resistor 1 kΩ × 3, Passive buzzer, Resistor 330 Ω and Breadboard 830 Tie Points. It takes 7 steps and about 150 minutes.

Level
Intermediate
Time
About 150 min
Parts
7

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Program first with the ST-LinkSWDIO, SWCLK and GND onlyThen power the board from USB so the 5V pin is live
HC-SR04 VCCBlack Pill 5V pin (USB power)The sensor needs 5 V
HC-SR04 GNDBlack Pill GNDCommon ground
HC-SR04 TrigBlack Pill PB0Trigger pulse (3.3 V is enough)
HC-SR04 Echo1 kilo-ohm to Black Pill PB1Echo is 5 V, so divide it
PB1 to GNDTwo 1 kilo-ohm resistors in seriesDivider gives about 3.3 V
Passive buzzer plus330 ohm resistor to Black Pill PB8Tone output
Passive buzzer minusBlack Pill GNDReturn

Code

An ultrasonic sensor sends a burst of sound and times how long the echo takes to return. The sketch sends a ten microsecond trigger pulse, then measures how long the echo pin stays high. Sound travels about 29 microseconds per centimetre each way, so dividing the pulse width by 58 gives the distance in centimetres. The sensor works from 5 V and its echo pin swings to 5 V, but the board pins are 3.3 V. A divider made of one 1 kilo-ohm resistor above the pin and two in series below it brings the echo down to about 3.3 V. The distance then sets the gap between beeps, so they come faster as you approach, and below 15 cm the tone is steady. A soft or angled surface may not return an echo, and the sensor has a blind spot very close in. This is a guide for a garage wall or a model car and not a safety device, so do not rely on it in a real vehicle.

Runs on a virtual STM32 Blue Pill in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

const int TRIG = PB0;
const int ECHO = PB1;
const int BUZZER = PB8;

unsigned long lastPing = 0, lastBeep = 0;
float cm = 0;

float readCm() {
  digitalWrite(TRIG, LOW);  delayMicroseconds(3);
  digitalWrite(TRIG, HIGH); delayMicroseconds(10);        // 10 microsecond trigger pulse
  digitalWrite(TRIG, LOW);
  unsigned long us = pulseIn(ECHO, HIGH, 30000UL);        // give up after 30 ms (about 5 m)
  if (us == 0) return 0;                                  // no echo
  return us / 58.0;                                       // microseconds to centimetres
}

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(BUZZER, OUTPUT);
}

void loop() {
  if (millis() - lastPing >= 60) {
    lastPing = millis();
    cm = readCm();
  }

  if (cm > 2 && cm < 15) {
    tone(BUZZER, 2000);                                   // very close: steady tone
  } else if (cm >= 15 && cm < 150) {
    noTone(BUZZER);
    long gap = map((long)cm, 15, 150, 60, 800);          // closer means a shorter gap
    if (millis() - lastBeep >= (unsigned long)gap) {
      lastBeep = millis();
      tone(BUZZER, 2000, 40);                             // short beep
    }
  } else {
    noTone(BUZZER);                                       // nothing in range
  }
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Nothing within 1.5 mSilence
Object at 1 mSlow beeps
Object at 40 cmFaster beeps
Object at 10 cmSteady tone
Soft cloth in frontMay give weak or no echo

If it does not work

Always silent

Check 5 V on the sensor and the echo divider.

Always steady

Check the echo wire is not shorted to 3.3 V or 5 V.

Erratic beeps

Use a flat, hard target and keep wires short.

No sound

Check the buzzer is passive and wired to PB8.

Board damaged risk

Never feed the echo straight into the pin, always divide it.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the black Pill parking assist beeper?

STM32 Black Pill (STM32F411CEU6), ST-Link V2 programmer (clone), HC-SR04 Ultrasonic Sensor, Resistor 1 kΩ × 3, Passive buzzer, Resistor 330 Ω and Breadboard 830 Tie Points.

How long does the black Pill parking assist beeper take to build?

About 150 minutes for a intermediate maker, in 7 steps.

How do I know the black Pill parking assist beeper works?

Nothing within 1.5 m: Silence. Object at 1 m: Slow beeps. Object at 40 cm: Faster beeps.

Always silent: what should I check?

Check 5 V on the sensor and the echo divider.

Always steady: what should I check?

Check the echo wire is not shorted to 3.3 V or 5 V.

Erratic beeps: what should I check?

Use a flat, hard target and keep wires short.

No sound: what should I check?

Check the buzzer is passive and wired to PB8.