A two-part embedded project that uses an ESP32, an ultrasonic proximity sensor (HC‑SR04), and an L293D motor driver to control a DC motor and visualize distance readings on an I2C LCD. A Python script runs on a PC to ingest and visualize the distance data in real time.
The ESP32 continuously rotates a DC motor via an L293D H‑bridge, pausing at fixed angular steps to measure distance with an HC‑SR04 ultrasonic sensor. Measurements are displayed on a 16×2 I2C LCD (LiquidCrystal_I2cTUR) and streamed over serial to a PC. A Python script reads the serial stream, plots a radar‑style visualization, and logs raw data for offline analysis.
- Angular sweep distance measurements (sonar) in 4° steps up to 360°
- Real‑time display of distance on a 16×2 I2C LCD
- Bidirectional DC motor control (forward/reverse) via L293D
- Serial handshake protocol (
RDY,FWR,Distance:<value>,CDR) - PC‑side radar visualization with live plotting of measurements
- Data logging for offline analysis
- ESP32 Development Board
- HC‑SR04 Ultrasonic Sensor
- L293D Motor Driver
- DC Motor (9 V supply)
- 16×2 I2C LCD (LiquidCrystal_I2cTUR compatible)
- Piezo Buzzer (optional)
- Breadboard, jumper wires, 9 V battery (or equivalent motor supply)
-
Arduino IDE (with ESP32 board support)
-
Python 3.8+
-
Python packages (install via
pip install -r requirements.txt):pyserialnumpymatplotlib
ESP32 HC‑SR04 L293D 16×2 I2C LCD DC Motor Buzzer
----- ------- ----- ------------- -------- ------
3V3 ────────────── VCC VCC VCC + (9 V battery +)
GND ────────────── GND GND GND - (9 V battery -)
GPIO 13 ── Trig ─ ─ ─ ─ ─
GPIO 32 ── Echo ─ ─ ─ ─ ─
GPIO 25 ── Enable ─ Enable1,2 ─ ─ ─ ─
GPIO 26 ── IN1 ─ Input1 ─ ─ ─ ─
GPIO 27 ── IN2 ─ Input2 ─ ─ ─ ─
GPIO 21 ── SDA ─ SDA ─
GPIO 22 ── SCL ─ SCL ─
GPIO 14 ── Buzzer ─ ─ ─ ─
(Replace motor leads and power lines according to your battery connector.)
-
Open
CODE.Cin the Arduino IDE. -
Ensure the following libraries are installed:
Wire.h(I2C support)LiquidCrystal_I2cTUR.h(I2C LCD)
-
Verify pin definitions at the top of the sketch:
#define enablePin 25 #define directionPin1 26 #define directionPin2 27 #define buzzer 14 #define proximityTrig 13 #define proximityEcho 32 #define SDA_PIN 21 #define SCL_PIN 22
-
Upload to your ESP32.
-
Open the Serial Monitor at 115200 bps to observe the handshake and distance readings.
-
Navigate to the
python/directory. -
Install dependencies:
pip install -r requirements.txt
-
Configure the serial port in
GROUP_03_2022510158_2018510126_2021510002_PYTHON_CODE.py:PORT = '/dev/ttyUSB0' BAUD_RATE = 115200
-
Run the script:
python GROUP_03_2022510158_2018510126_2021510002_PYTHON_CODE.py
-
A radar‑style plot window will open and update in real time.
-
Power up your ESP32 and motor supply.
-
The ESP32 prints
ESP32: Ready signal received.when it getsRDYfrom the Python script. -
The motor sweeps in 4° increments; each step:
- Stops, triggers the HC‑SR04, displays
Distance:on LCD. - Sends
FWRthenDistance:<value>over serial.
- Stops, triggers the HC‑SR04, displays
-
After 90 measurements, the ESP32 sends
CDRand reverses sweep direction. -
The Python script interprets those messages to animate the radar.
├── Radar_Code.C Arduino firmware for ESP32
├── Visualization_Code.py PC‑side visualization
└── wiring_diagram.png (Optional) high‑res diagram
- No serial output: Verify USB connection, correct COM/TTY port, and baud rate.
- LCD blank: Check I2C address (default
0x27) andLiquidCrystal_I2cTURlibrary. - No distance readings: Confirm HC‑SR04 Vcc/GND,
proximityTrig(GPIO 13) andproximityEcho(GPIO 32). - Motor not spinning: Ensure motor supply connected to L293D Vcc2,
enablePin(25) set byanalogWrite(enablePin, 255).
- Implement filtering (moving average, Kalman) on distances.
- Add web‑based dashboard via ESP32 Wi‑Fi.
- Switch to hardware interrupts for echo timing for higher precision.