Course Content
Hands-On ESP32 Robotics: Build Smart Robots Step by Step

🔌Circuit Connections

🎯 Lesson Objective

In this lesson, students will understand:

• How to connect the ultrasonic sensor to the ESP32
• How to connect the L298N motor driver to the ESP32
• How to connect DC motors to the motor driver
• The importance of correct wiring in robotics systems

This lesson explains the hardware connections required to build the Obstacle Avoiding Robot.


1️⃣ Understanding the Circuit

Before programming the robot, all hardware components must be connected correctly.

In this project, the main components connected to the ESP32 are:

🧠 ESP32 microcontroller
📏 Ultrasonic sensor (HC-SR04)
⚙ L298N motor driver
🚗 DC motors

The ESP32 reads the distance from the ultrasonic sensor and sends control signals to the motor driver.


2️⃣ Ultrasonic Sensor Connections

The HC-SR04 ultrasonic sensor measures the distance between the robot and objects.

Ultrasonic Sensor Pin Connections

Ultrasonic Sensor Pin ESP32 Connection
VCC 5V
GND GND
TRIG GPIO 5
ECHO GPIO 18

Connection Explanation

📡 VCC supplies power to the sensor.

GND connects the sensor to ground.

📤 TRIG pin sends ultrasonic signals to start distance measurement.

📥 ECHO pin receives the reflected ultrasonic signal.

The ESP32 measures the time of the echo signal to calculate the distance.


3️⃣ Motor Driver Connections

The L298N motor driver controls the two DC motors of the robot.

Motor Driver Input Pins

L298N Pin ESP32 Connection
IN1 GPIO 26
IN2 GPIO 27
IN3 GPIO 14
IN4 GPIO 12

These pins control the direction of the motors.

The ESP32 sends HIGH and LOW signals to control the motor movement.


4️⃣ Motor Connections

The DC motors are connected to the output terminals of the motor driver.

Motor Driver Output Connections

Motor Driver Output Motor
OUT1 Left Motor Terminal 1
OUT2 Left Motor Terminal 2
OUT3 Right Motor Terminal 1
OUT4 Right Motor Terminal 2

These outputs supply power to the motors and control their rotation direction.


5️⃣ Motor Driver Power Connections

The motor driver requires an external power supply.

Power Wiring

Motor Driver Pin Connection
12V Battery Positive
GND Battery Ground
5V Optional logic power

The battery powers the motors, while the ESP32 only sends control signals.


6️⃣ Importance of Common Ground

All components must share a common ground connection.

This means connecting together:

• ESP32 ground
• Ultrasonic sensor ground
• Motor driver ground
• Battery ground

A common ground ensures proper communication between devices.


7️⃣ Checking the Circuit Before Powering

Before turning on the robot, verify the following:

✔ Ultrasonic sensor pins connected correctly
✔ Motor driver pins connected to ESP32
✔ Motors connected to correct output terminals
✔ Battery polarity correct
✔ No loose wires

Incorrect wiring may prevent the robot from working properly.


8️⃣ Circuit Working Overview

The circuit operates as follows:

1️⃣ The ultrasonic sensor measures the distance in front of the robot.
2️⃣ The ESP32 reads the sensor value.
3️⃣ If the distance is safe, the robot moves forward.
4️⃣ If the obstacle is too close, the ESP32 commands the motor driver.
5️⃣ The motor driver changes the direction of the motors.
6️⃣ The robot turns to avoid the obstacle.

This allows the robot to detect obstacles and change direction automatically.


🚀 What Happens Next

Now that the hardware connections are complete, the next step is to write the program that controls the robot behavior.

Scroll to Top