💻Programming Code
🎯 Lesson Objective
In this lesson, students will learn:
• How to program the Voice Control Robot
• How the ESP32 receives commands from a smartphone through Bluetooth
• How voice commands are converted into control signals
• How the ESP32 controls the motors using the L298N motor driver
• How the robot moves according to voice instructions
By the end of this lesson, students will be able to upload the program and control the robot using voice commands from a smartphone.
1️⃣ Complete Copy-Paste Code
Students can copy and paste the following code directly into Arduino IDE.
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
int motor1A = 26;
int motor1B = 27;
int motor2A = 14;
int motor2B = 12;
String voiceCommand;
void setup()
{
Serial.begin(9600);
SerialBT.begin("ESP32_Voice_Robot");
pinMode(motor1A, OUTPUT);
pinMode(motor1B, OUTPUT);
pinMode(motor2A, OUTPUT);
pinMode(motor2B, OUTPUT);
Serial.println("Voice Control Robot Ready");
}
void loop()
{
if(SerialBT.available())
{
voiceCommand = SerialBT.readStringUntil('\n');
voiceCommand.trim();
Serial.println(voiceCommand);
if(voiceCommand == "forward")
{
moveForward();
}
else if(voiceCommand == "backward")
{
moveBackward();
}
else if(voiceCommand == "left")
{
turnLeft();
}
else if(voiceCommand == "right")
{
turnRight();
}
else if(voiceCommand == "stop")
{
stopRobot();
}
}
}
void moveForward()
{
digitalWrite(motor1A, HIGH);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, HIGH);
digitalWrite(motor2B, LOW);
}
void moveBackward()
{
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, HIGH);
digitalWrite(motor2A, LOW);
digitalWrite(motor2B, HIGH);
}
void turnLeft()
{
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, HIGH);
digitalWrite(motor2A, HIGH);
digitalWrite(motor2B, LOW);
}
void turnRight()
{
digitalWrite(motor1A, HIGH);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, LOW);
digitalWrite(motor2B, HIGH);
}
void stopRobot()
{
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, LOW);
digitalWrite(motor2B, LOW);
}
2️⃣ Program Logic
The robot follows this behavior:
1️⃣ The user speaks a command into the smartphone.
2️⃣ The smartphone converts the voice into text.
3️⃣ The app sends the text command through Bluetooth.
4️⃣ The ESP32 receives the command.
5️⃣ The program checks the command and moves the robot accordingly.
Example commands:
| Voice Command | Robot Action |
|---|---|
| forward | Move Forward |
| backward | Move Backward |
| left | Turn Left |
| right | Turn Right |
| stop | Stop Robot |
3️⃣ Bluetooth Library
The program uses the BluetoothSerial library.
This library allows the ESP32 to communicate with Bluetooth devices such as smartphones.
4️⃣ Creating a Bluetooth Object
BluetoothSerial SerialBT;This creates a Bluetooth communication object.
It will be used to send and receive Bluetooth data.
5️⃣ Starting Bluetooth Communication
Inside the setup function:
SerialBT.begin("ESP32_Voice_Robot");This line starts Bluetooth communication.
The name ESP32_Voice_Robot will appear in the smartphone Bluetooth device list.
Students will connect their phone to this device.
6️⃣ Motor Pin Declaration
int motor1A = 26;int motor1B = 27;int motor2A = 14;int motor2B = 12;These pins control the motor driver inputs.
Motor mapping:
• motor1A & motor1B → Left motor
• motor2A & motor2B → Right motor
7️⃣ Reading Voice Commands
Inside the loop function:
voiceCommand = SerialBT.readStringUntil('\n');voiceCommand.trim();This reads the text command sent from the smartphone.
The command is stored in the voiceCommand variable.
The trim() function removes extra spaces.
8️⃣ Command Processing
The program checks which command was received.
Example:
if(voiceCommand == "forward"){moveForward();}If the command is forward, the robot moves forward.
Similar conditions are used for other commands.
9️⃣ Movement Functions
Each robot movement is controlled using a function.
Forward Movement
void moveForward()Both motors rotate forward.
Backward Movement
void moveBackward()Both motors rotate backward.
Left Turn
void turnLeft()Left motor reverses and right motor moves forward.
Right Turn
void turnRight()Right motor reverses and left motor moves forward.
Stop Robot
void stopRobot()All motors stop.
1️⃣0️⃣ Final Working Process
The Voice Control Robot works as follows:
1️⃣ The user speaks a command into the smartphone.
2️⃣ The smartphone converts the voice into text.
3️⃣ The command is sent to the ESP32 through Bluetooth.
4️⃣ The ESP32 reads the command.
5️⃣ The ESP32 controls the motors based on the command.
6️⃣ The robot moves according to the voice instruction.
1️⃣1️⃣ Common Mistakes
Students may encounter the following issues:
⚠ Phone not connecting → check Bluetooth pairing
⚠ Robot not moving → verify motor driver wiring
⚠ Voice command not recognized → check the mobile app configuration
⚠ Robot moving wrong direction → swap motor wires
Always verify the wiring and Bluetooth connection first.
🚀 What Happens Next
Now that the Voice Control Robot program is uploaded, the final step is to test the robot and explore improvements.