YKR038 COURSE · MODULE 4 · LESSON 24

Build an Ultrasonic Parking Sensor

Combine distance measurements with LED and buzzer warning patterns.

40 minComplete beginnerSensors & Detection
BUILD WITHHC-SR04
PROGRAMESP32
ACHIEVEAlerts become faster or change color…
Written for complete beginners

Follow the numbered steps in order. Do not connect USB power until the wiring step tells you to. Compare every pin label before continuing.

LESSON GOAL

What you will build and learn

In this lesson, you will learn distance zones and graduated alerts. By the end, alerts become faster or change color as an object approaches.

  • Recognize the purpose of each required part.
  • Make the connections in a safe, repeatable order.
  • Upload or run the lesson procedure and verify the expected result.
  • Use observable evidence to fix common problems.
Real YKR038 wiring photo reserved

A close, labeled photograph of this exact experiment will be added here. Until then, use the written pin-by-pin connection list below.

STEP-BY-STEP GUIDE

Build the experiment

1

Understand the target result

Combine distance measurements with LED and buzzer warning patterns. The expected result is: Alerts become faster or change color as an object approaches.

2

Prepare the board and parts

Disconnect the ESP32 from USB. Put the ESP32, breadboard, jumper wires, and the listed lesson parts in front of you. Straighten bent pins gently and never force a module into the breadboard.

3

Make each connection with power off

  1. Wire HC-SR04 as in Lesson 23: TRIG GPIO 5 and divided ECHO GPIO 18.
  2. Green LED → resistor → GPIO 23; yellow LED → resistor → GPIO 22; red LED → resistor → GPIO 21.
  3. Active buzzer positive → GPIO 19 and negative → GND.
  4. Connect every ground together.

Power check: confirm that no 5V signal goes directly into an ESP32 GPIO and that 3.3V is not connected to GND.

4

Install any library and upload the code

No additional Arduino library is required for this starter sketch. Select your ESP32 board and serial port before clicking Upload.

Arduino sketchCheck the pin constants against your wiring
const int TRIG=5,ECHO=18,GREEN=23,YELLOW=22,RED=21,BUZZER=19;
float distanceCm(){ digitalWrite(TRIG,LOW);delayMicroseconds(2);digitalWrite(TRIG,HIGH);delayMicroseconds(10);digitalWrite(TRIG,LOW);return pulseIn(ECHO,HIGH,30000)*0.0343/2.0; }
void setup(){ pinMode(TRIG,OUTPUT);pinMode(ECHO,INPUT);pinMode(GREEN,OUTPUT);pinMode(YELLOW,OUTPUT);pinMode(RED,OUTPUT);pinMode(BUZZER,OUTPUT); }
void loop(){
  float cm=distanceCm(); bool near=cm>0&&cm<15, medium=cm>=15&&cm<35;
  digitalWrite(RED,near);digitalWrite(YELLOW,medium);digitalWrite(GREEN,!near&&!medium);
  digitalWrite(BUZZER,near); delay(near?100:250); digitalWrite(BUZZER,LOW); delay(near?100:250);
}
5

Power the circuit and observe

Connect USB power only after the wiring check. Open Serial Monitor at 115200 baud when the sketch prints data. Watch the circuit for at least 20 seconds and compare it with this expected behavior: Alerts become faster or change color as an object approaches.

6

Change one value and test again

Make one small change—such as a delay, threshold, brightness, or displayed message—then upload again. This confirms that you understand which part of the program controls the result.

WHY IT WORKS

The key idea

This experiment demonstrates distance zones and graduated alerts. The ESP32 repeatedly reads or updates the connected hardware, applies the program logic, and produces the observable result. Changing one input or one code value helps you see that relationship directly.

Input or instructionESP32 logicMeasured result or output

TROUBLESHOOTING

If the experiment does not work

Nothing powers on

Disconnect USB. Check the USB cable, 3.3V/5V selection, GND rail, and breadboard row placement.

Upload fails

Confirm the board and port. Close other serial programs, reconnect USB, and hold BOOT only if your board requires it.

Result is unstable

Shorten jumper wires, confirm a shared ground, and verify that input pins are not floating.

Lesson-specific check

Use averaged distance values to prevent unstable zone changes.

VIDEO WALKTHROUGH

Hands-on lesson video reserved

The final video will show the real YKR038 parts, every wire connection, the upload process, and the expected result.