How to Build an ESP32 Ultrasonic Parking Sensor

Distance sensing, traffic-light LEDs, OLED readout, and beeps that speed up as you get close

ESP32VehiclesBeginner40 minutes6 components

Updated

How to Build an ESP32 Ultrasonic Parking Sensor
For illustrative purposes only
On this page

What you'll build

This guide walks you through a tabletop parking sensor that measures distance with an HC-SR04P ultrasonic module, shows the live reading on a small SSD1306 OLED, and gives an obvious warning using green, yellow, and red LEDs. When an object is far away, the green LED stays on. As it moves closer, the yellow LED takes over. Inside the stop zone, the red LED lights and the buzzer beeps faster so the warning is useful without staring at the screen.

The build is deliberately bench-safe. It does not connect to a real vehicle, control brakes, or make safety decisions for you. The HC-SR04P version works at 3.3V logic, so it is a better fit for an ESP32 than the older 5V-only HC-SR04 modules. The firmware sends a short trigger pulse, measures the echo time, converts it to centimetres, smooths the reading, and updates the LEDs, display, and buzzer from the same distance thresholds.

By the end you will have a compact distance-warning box you can test with a cardboard wall, robot chassis, or desk edge. It is a good first project for learning timing, digital outputs, simple state thresholds, and sensor noise handling before moving on to a rover or any build where distance sensing affects motion.

Wiring diagram

Wiring diagram

Interactive wiring diagram

Components needed

ComponentTypeQtyBuy
HC-SR04P Ultrasonic Distance Sensorsensor1€1.85
0.96 inch SSD1306 OLED Displaydisplay1€4.30
Green LEDactuator1€1.60
Yellow LEDactuator1
Red LEDactuator1€1.60
Passive Piezo Buzzeractuator1€4.75

Prices and availability are indicative and may have been updated by the supplier. Schematik may earn a commission from purchases made through affiliate links.

Assembly

1

Wire the distance sensor

Connect the HC-SR04P VCC and GND to the ESP32 3V3 and GND rails, then connect TRIG to GPIO27 and ECHO to GPIO26.

2

Add the display

Connect the SSD1306 OLED to the same 3V3 and GND rails, with SDA on GPIO21 and SCL on GPIO22.

3

Add the warning outputs

Connect the green LED to GPIO14, yellow LED to GPIO12, red LED to GPIO13, and the passive buzzer signal pin to GPIO25. Each LED should use a current-limiting resistor.

4

Upload and test against a wall

Upload the sketch, aim the sensor at a flat surface, and move it from about one metre away to the stop zone. The OLED, LEDs, and buzzer should change together.

Pin assignments

PinConnectionType
3V3hc-sr04p-ultrasonic-sensor_0 VCCPOWER
GNDhc-sr04p-ultrasonic-sensor_0 GNDGROUND
GPIO 27hc-sr04p-ultrasonic-sensor_0 TRIGDIGITAL
GPIO 26hc-sr04p-ultrasonic-sensor_0 ECHODIGITAL
3V3ssd1306-oled_0 VCCPOWER
GNDssd1306-oled_0 GNDGROUND
GPIO 21ssd1306-oled_0 SDAI2C
GPIO 22ssd1306-oled_0 SCLI2C
GPIO 14green-led_0 ANODEDIGITAL
GNDgreen-led_0 CATHODEGROUND
GPIO 12yellow-led_0 ANODEDIGITAL
GNDyellow-led_0 CATHODEGROUND
GPIO 13red-led_0 ANODEDIGITAL
GNDred-led_0 CATHODEGROUND
GPIO 25passive-buzzer_0 SIGDIGITAL
GNDpassive-buzzer_0 GNDGROUND

Code

Arduino C++
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define TRIG_PIN 27
#define ECHO_PIN 26
#define GREEN_LED_PIN 14
#define YELLOW_LED_PIN 12
#define RED_LED_PIN 13
#define BUZZER_PIN 25
#define SDA_PIN 21
#define SCL_PIN 22
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

const int SAFE_DISTANCE_CM = 70;
const int CAUTION_DISTANCE_CM = 35;
const int STOP_DISTANCE_CM = 18;

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
float smoothedDistance = 100;
unsigned long lastBeepMs = 0;
bool buzzerOn = false;

float readDistanceCm() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  unsigned long duration = pulseIn(ECHO_PIN, HIGH, 30000);
  if (duration == 0) return smoothedDistance;
  return duration / 58.0;
}

void setOutputs(float distanceCm) {
  bool safe = distanceCm > SAFE_DISTANCE_CM;
  bool caution = distanceCm <= SAFE_DISTANCE_CM && distanceCm > CAUTION_DISTANCE_CM;
  bool warning = distanceCm <= CAUTION_DISTANCE_CM;

  digitalWrite(GREEN_LED_PIN, safe ? HIGH : LOW);
  digitalWrite(YELLOW_LED_PIN, caution ? HIGH : LOW);
  digitalWrite(RED_LED_PIN, warning ? HIGH : LOW);

  if (!warning) {
    noTone(BUZZER_PIN);
    buzzerOn = false;
    return;
  }

  int interval = distanceCm <= STOP_DISTANCE_CM ? 120 : 320;
  if (millis() - lastBeepMs > interval) {
    lastBeepMs = millis();
    buzzerOn = !buzzerOn;
    if (buzzerOn) tone(BUZZER_PIN, distanceCm <= STOP_DISTANCE_CM ? 1800 : 1200);
    else noTone(BUZZER_PIN);
  }
}

void drawDisplay(float distanceCm) {
  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.println("Parking sensor");
  display.setTextSize(2);
  display.setCursor(0, 18);
  display.print(distanceCm, 0);
  display.println(" cm");
  display.setTextSize(1);
  display.setCursor(0, 48);
  if (distanceCm <= STOP_DISTANCE_CM) display.println("STOP");
  else if (distanceCm <= CAUTION_DISTANCE_CM) display.println("Slow down");
  else if (distanceCm <= SAFE_DISTANCE_CM) display.println("Getting close");
  else display.println("Clear");
  display.display();
}

void setup() {
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(GREEN_LED_PIN, OUTPUT);
  pinMode(YELLOW_LED_PIN, OUTPUT);
  pinMode(RED_LED_PIN, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);

  Wire.begin(SDA_PIN, SCL_PIN);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.display();
}

void loop() {
  float reading = readDistanceCm();
  smoothedDistance = (smoothedDistance * 0.75) + (reading * 0.25);
  setOutputs(smoothedDistance);
  drawDisplay(smoothedDistance);
  delay(80);
}

// Run this and build other cool things at schematik.io
Libraries: Adafruit SSD1306, Adafruit GFX Library

Ready to build this?

Open this project in Schematik to get the full wiring diagram, pin assignments, and deployable code for the ESP32 Ultrasonic Parking Sensor.

Open in Schematik →

Related guides