IotIntermediate

ESP32 Four-Wheel Robot Car Using Serial Communication

Build and control a four-wheel robot car using ESP32 and serial communication.

Technologies Used

ESP32Motor DriverDC MotorsRobot Car ChassisSerial Communication

ESP32 Four-Wheel Robot Car Using Serial Communication and PWM Motor Control

1. Introduction

This project controls a four-wheel robot car using an ESP32 microcontroller and a motor driver. The ESP32 receives movement commands through serial communication and controls four DC motors independently.

The robot supports multiple movements, including forward, backward, left, right, diagonal movement, turning, and stopping.

It also supports adjustable motor speed using PWM (Pulse Width Modulation).

2. Objectives

  • Understand ESP32 microcontroller programming.
  • Learn serial communication using UART.
  • Control four DC motors independently.
  • Understand PWM-based motor speed control.
  • Learn different robot movement techniques.
  • Understand motor direction control.
  • Explore wireless robot-control systems.

3. Required Components

  1. ESP32 development board
  2. Four DC geared motors
  3. Motor driver modules suitable for four motors
  4. Four-wheel robot chassis
  5. Four wheels
  6. External battery
  7. Connecting wires
  8. Serial communication transmitter/controller
  9. USB cable
  10. Arduino IDE

4. Software and Libraries

Library/headerPurpose
vectorStores motor pin configurations
iostreamProvides C++ input/output functionality
sstreamSplits incoming command strings
Arduino.hProvides ESP32 Arduino functions

The ESP32 Arduino core provides functions such as pinMode(), digitalWrite(), Serial1, and LEDC PWM functions.

5. ESP32 Pin Configuration

MotorIN1IN2Enable/PWMPWM channel
Front rightGPIO 16GPIO 17GPIO 224
Back rightGPIO 18GPIO 19GPIO 235
Front leftGPIO 26GPIO 25GPIO 326
Back leftGPIO 14GPIO 27GPIO 337

Serial communication pins

Serial connectionESP32 pin
Serial1 RXGPIO 4
Serial1 TXGPIO 15
USB SerialUSB connection

Serial1.begin(115200, SERIAL_8N1, 4, 15) configures UART1 to receive on GPIO 4 and transmit on GPIO 15.

6. Complete Arduino Code

#include <vector> #include <iostream> #include <sstream> #define UP 1 #define DOWN 2 #define LEFT 3 #define RIGHT 4 #define UP_LEFT 5 #define UP_RIGHT 6 #define DOWN_LEFT 7 #define DOWN_RIGHT 8 #define TURN_LEFT 9 #define TURN_RIGHT 10 #define STOP 0 #define FRONT_RIGHT_MOTOR 0 #define BACK_RIGHT_MOTOR 1 #define FRONT_LEFT_MOTOR 2 #define BACK_LEFT_MOTOR 3 #define FORWARD 1 #define BACKWARD -1 struct MOTOR_PINS { int pinIN1; int pinIN2; int pinEn; int pwmSpeedChannel; }; std::vector<MOTOR_PINS> motorPins = { {16, 17, 22, 4}, {18, 19, 23, 5}, {26, 25, 32, 6}, {14, 27, 33, 7}, }; const int PWMFreq = 1000; const int PWMResolution = 8; #define SIGNAL_TIMEOUT 1000 unsigned long lastRecvTime = 0; void processCarMovement(int inputValue) { switch(inputValue) { case UP: rotateMotor(FRONT_RIGHT_MOTOR, FORWARD); rotateMotor(BACK_RIGHT_MOTOR, FORWARD); rotateMotor(FRONT_LEFT_MOTOR, FORWARD); rotateMotor(BACK_LEFT_MOTOR, FORWARD); break; case DOWN: rotateMotor(FRONT_RIGHT_MOTOR, BACKWARD); rotateMotor(BACK_RIGHT_MOTOR, BACKWARD); rotateMotor(FRONT_LEFT_MOTOR, BACKWARD); rotateMotor(BACK_LEFT_MOTOR, BACKWARD); break; case LEFT: rotateMotor(FRONT_RIGHT_MOTOR, FORWARD); rotateMotor(BACK_RIGHT_MOTOR, BACKWARD); rotateMotor(FRONT_LEFT_MOTOR, BACKWARD); rotateMotor(BACK_LEFT_MOTOR, FORWARD); break; case RIGHT: rotateMotor(FRONT_RIGHT_MOTOR, BACKWARD); rotateMotor(BACK_RIGHT_MOTOR, FORWARD); rotateMotor(FRONT_LEFT_MOTOR, FORWARD); rotateMotor(BACK_LEFT_MOTOR, BACKWARD); break; case UP_LEFT: rotateMotor(FRONT_RIGHT_MOTOR, FORWARD); rotateMotor(BACK_RIGHT_MOTOR, STOP); rotateMotor(FRONT_LEFT_MOTOR, STOP); rotateMotor(BACK_LEFT_MOTOR, FORWARD); break; case UP_RIGHT: rotateMotor(FRONT_RIGHT_MOTOR, STOP); rotateMotor(BACK_RIGHT_MOTOR, FORWARD); rotateMotor(FRONT_LEFT_MOTOR, FORWARD); rotateMotor(BACK_LEFT_MOTOR, STOP); break; case DOWN_LEFT: rotateMotor(FRONT_RIGHT_MOTOR, STOP); rotateMotor(BACK_RIGHT_MOTOR, BACKWARD); rotateMotor(FRONT_LEFT_MOTOR, BACKWARD); rotateMotor(BACK_LEFT_MOTOR, STOP); break; case DOWN_RIGHT: rotateMotor(FRONT_RIGHT_MOTOR, BACKWARD); rotateMotor(BACK_RIGHT_MOTOR, STOP); rotateMotor(FRONT_LEFT_MOTOR, STOP); rotateMotor(BACK_LEFT_MOTOR, BACKWARD); break; case TURN_LEFT: rotateMotor(FRONT_RIGHT_MOTOR, FORWARD); rotateMotor(BACK_RIGHT_MOTOR, FORWARD); rotateMotor(FRONT_LEFT_MOTOR, BACKWARD); rotateMotor(BACK_LEFT_MOTOR, BACKWARD); break; case TURN_RIGHT: rotateMotor(FRONT_RIGHT_MOTOR, BACKWARD); rotateMotor(BACK_RIGHT_MOTOR, BACKWARD); rotateMotor(FRONT_LEFT_MOTOR, FORWARD); rotateMotor(BACK_LEFT_MOTOR, FORWARD); break; case STOP: rotateMotor(FRONT_RIGHT_MOTOR, STOP); rotateMotor(BACK_RIGHT_MOTOR, STOP); rotateMotor(FRONT_LEFT_MOTOR, STOP); rotateMotor(BACK_LEFT_MOTOR, STOP); break; default: rotateMotor(FRONT_RIGHT_MOTOR, STOP); rotateMotor(BACK_RIGHT_MOTOR, STOP); rotateMotor(FRONT_LEFT_MOTOR, STOP); rotateMotor(BACK_LEFT_MOTOR, STOP); break; } } void rotateMotor(int motorNumber, int motorDirection) { if (motorDirection == FORWARD) { digitalWrite(motorPins[motorNumber].pinIN1, HIGH); digitalWrite(motorPins[motorNumber].pinIN2, LOW); } else if (motorDirection == BACKWARD) { digitalWrite(motorPins[motorNumber].pinIN1, LOW); digitalWrite(motorPins[motorNumber].pinIN2, HIGH); } else { digitalWrite(motorPins[motorNumber].pinIN1, LOW); digitalWrite(motorPins[motorNumber].pinIN2, LOW); } } void setUpPinModes() { for (int i = 0; i < motorPins.size(); i++) { pinMode(motorPins[i].pinIN1, OUTPUT); pinMode(motorPins[i].pinIN2, OUTPUT); ledcSetup(motorPins[i].pwmSpeedChannel, PWMFreq, PWMResolution); ledcAttachPin(motorPins[i].pinEn, motorPins[i].pwmSpeedChannel); rotateMotor(i, STOP); } } void setup() { setUpPinModes(); Serial1.begin(115200, SERIAL_8N1, 4, 15); Serial.begin(115200); } void loop() { String dataString; char key[500]; int value; if (Serial1.available()) { dataString = Serial1.readStringUntil('\n'); std::string myData = dataString.c_str(); std::istringstream ss(myData); std::string key, value; std::getline(ss, key, ','); std::getline(ss, value, ','); Serial.printf("Key [%s] Value[%s]\n", key.c_str(), value.c_str()); int valueInt = atoi(value.c_str()); if (key == "MoveCar") { processCarMovement(valueInt); } else if (key == "Speed") { for (int i = 0; i < motorPins.size(); i++) { ledcWrite(motorPins[i].pwmSpeedChannel, valueInt); } } lastRecvTime = millis(); } }

7. Explanation of the Code

A. Header files

#include <vector> #include <iostream> #include <sstream>
  • vector: Stores the configuration of the four motors.
  • iostream: Provides C++ input/output features.
  • sstream: Helps split incoming text into separate parts.

B. Movement definitions

#define UP 1 #define DOWN 2 #define LEFT 3 #define RIGHT 4 #define UP_LEFT 5 #define UP_RIGHT 6 #define DOWN_LEFT 7 #define DOWN_RIGHT 8 #define TURN_LEFT 9 #define TURN_RIGHT 10 #define STOP 0

These definitions assign a numeric value to each movement command.

For example:

  • UP represents forward movement.
  • DOWN represents backward movement.
  • LEFT represents left movement.
  • RIGHT represents right movement.
  • STOP represents stopping all motors.

C. Motor identification

#define FRONT_RIGHT_MOTOR 0 #define BACK_RIGHT_MOTOR 1 #define FRONT_LEFT_MOTOR 2 #define BACK_LEFT_MOTOR 3

These constants identify the four motors.

The motor index is used to access the correct motor configuration in the vector.

D. Direction definitions

#define FORWARD 1 #define BACKWARD -1

These constants represent the two directions of motor rotation.

  • FORWARD = 1
  • BACKWARD = -1

The program uses these values in the rotateMotor() function.

E. MOTOR_PINS structure

struct MOTOR_PINS { int pinIN1; int pinIN2; int pinEn; int pwmSpeedChannel; };

A structure groups related information together.

Each motor requires four configuration values:

  1. pinIN1: First motor direction pin.
  2. pinIN2: Second motor direction pin.
  3. pinEn: Motor enable/PWM pin.
  4. pwmSpeedChannel: ESP32 PWM channel.

F. Motor configuration vector

std::vector<MOTOR_PINS> motorPins = { {16, 17, 22, 4}, {18, 19, 23, 5}, {26, 25, 32, 6}, {14, 27, 33, 7}, };

This vector stores the pin configuration of all four motors.

IndexMotorIN1IN2EnablePWM channel
0Front right1617224
1Back right1819235
2Front left2625326
3Back left1427337

G. PWM configuration

const int PWMFreq = 1000; const int PWMResolution = 8;

PWM means Pulse Width Modulation.

  • Frequency: 1000 Hz.
  • Resolution: 8 bits.
  • PWM range: 0–255.

Examples:

PWM valueMeaning
00% duty cycle
64Approximately 25%
128Approximately 50%
192Approximately 75%
255Approximately 100%

The actual motor speed also depends on the motor, battery voltage, load, and driver.

H. Signal timeout variable

#define SIGNAL_TIMEOUT 1000 unsigned long lastRecvTime = 0;
  • SIGNAL_TIMEOUT: Defines a timeout duration of 1000 milliseconds.
  • lastRecvTime: Stores the time of the last received command.

Important: The original code declares these variables but does not use them to stop the robot. A timeout check must be added to make this feature functional.

I. processCarMovement() function

void processCarMovement(int inputValue) { switch(inputValue) { // Movement commands } }

This function receives a movement command and selects the appropriate motor directions.

It uses a switch statement to check the command.

For example:

case UP:

When the UP command arrives, all four motors rotate forward.

J. Forward movement

case UP: rotateMotor(FRONT_RIGHT_MOTOR, FORWARD); rotateMotor(BACK_RIGHT_MOTOR, FORWARD); rotateMotor(FRONT_LEFT_MOTOR, FORWARD); rotateMotor(BACK_LEFT_MOTOR, FORWARD); break;

All four motors rotate forward, so the robot moves forward.

K. Backward movement

case DOWN: rotateMotor(FRONT_RIGHT_MOTOR, BACKWARD); rotateMotor(BACK_RIGHT_MOTOR, BACKWARD); rotateMotor(FRONT_LEFT_MOTOR, BACKWARD); rotateMotor(BACK_LEFT_MOTOR, BACKWARD); break;

All four motors rotate backward.

L. Left and right movement

The LEFT and RIGHT commands rotate the motors on opposite sides in opposite directions.

This creates a turning movement.

M. Diagonal movement

The program also supports four diagonal commands:

  • UP_LEFT
  • UP_RIGHT
  • DOWN_LEFT
  • DOWN_RIGHT

These commands stop two motors and run the other two motors.

The result depends on the robot chassis and wheel arrangement. With ordinary fixed wheels, this may produce a curved or pivoting movement rather than true diagonal motion.

N. TURN_LEFT and TURN_RIGHT

case TURN_LEFT:

The right-side motors move forward, while the left-side motors move backward.

case TURN_RIGHT:

The right-side motors move backward, while the left-side motors move forward.

This allows the robot to rotate around its center.

O. rotateMotor() function

void rotateMotor(int motorNumber, int motorDirection)

This function controls the direction of an individual motor.

For forward:

digitalWrite(motorPins[motorNumber].pinIN1, HIGH); digitalWrite(motorPins[motorNumber].pinIN2, LOW);

For backward:

digitalWrite(motorPins[motorNumber].pinIN1, LOW); digitalWrite(motorPins[motorNumber].pinIN2, HIGH);

For stop:

digitalWrite(motorPins[motorNumber].pinIN1, LOW); digitalWrite(motorPins[motorNumber].pinIN2, LOW);

The function uses the motor index to select the correct pins from the vector.

P. setUpPinModes() function

void setUpPinModes() { for (int i = 0; i < motorPins.size(); i++) { pinMode(motorPins[i].pinIN1, OUTPUT); pinMode(motorPins[i].pinIN2, OUTPUT); ledcSetup(motorPins[i].pwmSpeedChannel, PWMFreq, PWMResolution); ledcAttachPin(motorPins[i].pinEn, motorPins[i].pwmSpeedChannel); rotateMotor(i, STOP); } }

This function configures all four motors.

  • for loop: Repeats the configuration for each motor.
  • pinMode(): Sets direction pins as outputs.
  • ledcSetup(): Configures PWM frequency and resolution.
  • ledcAttachPin(): Connects a PWM channel to an output pin.
  • rotateMotor(i, STOP): Stops the motor initially.

ESP32 version note: These ledcSetup() and ledcAttachPin() functions are used with the Arduino-ESP32 2.x core. Arduino-ESP32 3.x changed the LEDC API to functions such as ledcAttach() and ledcWrite() using pins rather than the old channel setup pattern. The original code may need modification for core 3.x.

Q. setup() function

void setup() { setUpPinModes(); Serial1.begin(115200, SERIAL_8N1, 4, 15); Serial.begin(115200); }

The setup function runs once.

  • setUpPinModes(): Configures all motor pins and PWM.
  • Serial1.begin(): Starts UART1 communication.
  • Serial.begin(): Starts USB serial communication for debugging.

SERIAL_8N1 means:

  • 8 data bits.
  • No parity bit.
  • 1 stop bit.

R. loop() function

void loop() { String dataString; if (Serial1.available()) { dataString = Serial1.readStringUntil('\n'); } }

The loop continuously checks for incoming serial data.

Serial1.available() checks whether data has arrived.

Serial1.readStringUntil('\n') reads the incoming message until a newline character is found.

S. Split incoming data

std::string myData = dataString.c_str(); std::istringstream ss(myData); std::string key, value; std::getline(ss, key, ','); std::getline(ss, value, ',');

This code separates the received message into two parts using a comma.

Example incoming message:

MoveCar,1

After splitting:

  • key = "MoveCar"
  • value = "1"

Another example:

Speed,180

After splitting:

  • key = "Speed"
  • value = "180"

T. Convert string to integer

int valueInt = atoi(value.c_str());

atoi() converts a numeric string into an integer.

Examples:

  • "1" becomes 1.
  • "180" becomes 180.
  • "255" becomes 255.

U. Process movement command

if (key == "MoveCar") { processCarMovement(valueInt); }

If the received key is MoveCar, Arduino calls the movement function.

V. Control motor speed

else if (key == "Speed") { for (int i = 0; i < motorPins.size(); i++) { ledcWrite(motorPins[i].pwmSpeedChannel, valueInt); } }

If the received key is Speed, the program updates the PWM duty cycle of all four motors.

For example, a speed command of 180 sets the PWM duty cycle to 180 out of 255.

The original program does not validate the speed value. A safer implementation should restrict it to 0–255.

8. Movement Control Table

CommandNumeric valueRobot movement
UP1Forward
DOWN2Backward
LEFT3Left
RIGHT4Right
UP_LEFT5Diagonal/curved upper-left movement
UP_RIGHT6Diagonal/curved upper-right movement
DOWN_LEFT7Diagonal/curved lower-left movement
DOWN_RIGHT8Diagonal/curved lower-right movement
TURN_LEFT9Rotate left
TURN_RIGHT10Rotate right
STOP0Stop all motors

9. Example Serial Commands

The controller must send messages in the following format:

MoveCar,1 MoveCar,2 MoveCar,3 MoveCar,4 MoveCar,0 Speed,130 Speed,180 Speed,255

Each message should end with a newline character (\n).

10. Block Diagram

+------------------------+ | Remote Controller | | Sends Movement Commands| +-----------+------------+ | | UART Serial | +-----------v------------+ | ESP32 Microcontroller | | Receives Commands | | Processes Movement | | Controls PWM Speed | +-----------+------------+ | | Direction + PWM | +-----------v------------+ | Motor Driver Modules | +-----------+------------+ | +----+----+ | | v v +-------------+ +-------------+ | Right Motors| | Left Motors | | Front/Back | | Front/Back | +-------------+ +-------------+

11. Flowchart

START | v Initialize ESP32 | v Configure Motor Pins | v Configure PWM | v Start Serial1 | v Check Serial Data | v Data Available? / \ No Yes | | | v | Read Message | | | v | Split Key/Value | | | v | Convert Value | | | v | Check Command | | | +----+----+ | | | | MoveCar Speed | | | | v v | Move Motors Set PWM | | | +------+---------+ | v Repeat Loop

12. Important Functions

FunctionPurpose
pinMode()Configures pins
digitalWrite()Controls motor direction
ledcSetup()Configures PWM on ESP32 core 2.x
ledcAttachPin()Attaches a PWM channel to a pin on core 2.x
ledcWrite()Sets PWM duty cycle
Serial1.begin()Starts UART1
Serial1.available()Checks incoming serial data
Serial1.readStringUntil()Reads a message until a delimiter
std::getline()Splits a string
atoi()Converts a numeric string to an integer
processCarMovement()Selects robot movement
rotateMotor()Controls one motor's direction
setUpPinModes()Configures all motor pins

13. Applications

  1. Four-wheel robotic vehicles.
  2. Remote-controlled robot cars.
  3. Educational robotics projects.
  4. ESP32-based automation.
  5. Wireless vehicle control.
  6. Robotics research and demonstrations.

14. Advantages

  • Controls four motors independently.
  • Supports multiple movement directions.
  • Allows adjustable motor speed.
  • Uses ESP32 UART communication.
  • Uses structured motor configuration.
  • Can be extended with Wi-Fi or Bluetooth control.

15. Limitations and Improvements

Problems in the original code

  1. Timeout is not implemented: SIGNAL_TIMEOUT is declared but never checked.
  2. PWM initial speed is not set: The program configures PWM channels but does not write an initial duty cycle.
  3. No speed validation: Invalid values outside 0–255 may be received.
  4. Blocking serial reading: readStringUntil() may wait for a newline or timeout, delaying other operations.
  5. Unused variables: key[500] and value are declared but not used in the outer loop.
  6. ESP32 compatibility: The LEDC functions depend on the ESP32 Arduino core version.
  7. No message validation: An incomplete or malformed command may be interpreted incorrectly.
  8. Motor driver requirements: Each motor requires a suitable driver channel with enough current capacity.

Recommended improvements

  • Add a communication timeout that stops all motors.
  • Set an initial PWM speed for all four motors.
  • Validate speed values between 0 and 255.
  • Validate the command before controlling the motors.
  • Use a non-blocking serial parser.
  • Add obstacle detection using ultrasonic sensors.
  • Add battery-level monitoring.
  • Add an emergency stop command.

16. Troubleshooting

ProblemPossible solution
ESP32 does not receive commandsCheck UART RX/TX connections
Robot does not moveCheck motor-driver power and wiring
One motor does not workCheck its direction and enable pins
Speed does not changeCheck PWM configuration and enable connections
Robot moves in the wrong directionReverse the motor direction wiring or logic
Commands are incompleteCheck newline termination and baud rate
Code does not compileCheck the ESP32 Arduino core version
ESP32 restarts during movementCheck motor supply and electrical noise

17. Safety Precautions

  • Do not connect motors directly to ESP32 GPIO pins.
  • Use motor drivers with suitable voltage and current ratings.
  • Use an external motor power supply.
  • Connect ESP32 and motor-driver grounds together.
  • Check the battery voltage before connecting it.
  • Keep the wheels raised during initial testing.
  • Stop the robot before changing wiring.
  • Ensure the driver supports the current required by all four motors.

18. Viva Questions and Answers

Q1. What is ESP32?

ESP32 is a microcontroller development platform that supports wireless communication and GPIO control.

Q2. What is UART?

UART stands for Universal Asynchronous Receiver/Transmitter. It is used for serial communication between devices.

Q3. What is PWM?

PWM stands for Pulse Width Modulation. It controls the average power delivered to a motor.

Q4. What is the purpose of the L298N motor driver?

It controls the direction and speed of DC motors.

Q5. Why are four motors used?

Four motors provide drive power to the four wheels of the robot.

Q6. What is a vector in C++?

A vector is a dynamic array that can store multiple elements.

Q7. What is a structure in C++?

A structure groups related variables under one name.

Q8. What is the purpose of switch?

It selects one operation from multiple possible cases.

Q9. What does atoi() do?

It converts a numeric string into an integer.

Q10. What is the purpose of ledcWrite()?

It sets the PWM duty cycle on an ESP32 LEDC output.

Q11. What is the purpose of readStringUntil('\n')?

It reads serial data until a newline character is received.

Q12. What is a signal timeout?

It is a time limit after which the robot should stop if no new control signal arrives.

19. Conclusion

This project demonstrates how an ESP32 can control a four-wheel robot using serial commands. The program processes movement and speed commands, selects the required motor directions, and controls PWM outputs.

It supports forward, backward, left, right, diagonal, turning, and stopping movements. This project provides practical knowledge of ESP32 programming, C++ structures and vectors, UART communication, PWM, and robotics.