💻 Lesson 8.3 — Programming Code
🎯 Lesson Objective
In this lesson, students will learn:
• How to program the Bluetooth Control Car
• How the ESP32 receives commands from a smartphone using Bluetooth
• How the robot interprets received commands
• How the ESP32 controls motors using the L298N motor driver
• How the robot moves based on user input
By the end of this lesson, students will be able to upload the program and control the robot using a mobile phone.
1️⃣ Complete Copy-Paste Code
Students can copy and paste the following program directly into Arduino IDE.
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
int motor1A = 26;
int motor1B = 27;
int motor2A = 14;
int motor2B = 12;
char command;
void setup()
{
Serial.begin(9600);
SerialBT.begin("ESP32_Robot");
pinMode(motor1A, OUTPUT);
pinMode(motor1B, OUTPUT);
pinMode(motor2A, OUTPUT);
pinMode(motor2B, OUTPUT);
Serial.println("Bluetooth Robot Ready");
}
void loop()
{
if(SerialBT.available())
{
command = SerialBT.read();
Serial.println(command);
if(command == 'F')
{
moveForward();
}
else if(command == 'B')
{
moveBackward();
}
else if(command == 'L')
{
turnLeft();
}
else if(command == 'R')
{
turnRight();
}
else if(command == 'S')
{
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 smartphone sends a command using Bluetooth.
2️⃣ The ESP32 receives the command.
3️⃣ The program checks which command was received.
4️⃣ The robot performs the corresponding movement.
Example commands:
| Command | Robot Action |
|---|---|
| F | Move Forward |
| B | Move Backward |
| L | Turn Left |
| R | Turn Right |
| S | Stop |
3️⃣ Bluetooth Library
The program uses the BluetoothSerial library.
This library allows the ESP32 to communicate with Bluetooth devices.
4️⃣ Creating a Bluetooth Object
This creates a Bluetooth communication object.
The object will be used to send and receive Bluetooth data.
5️⃣ Starting Bluetooth
Inside the setup function:
This line starts Bluetooth communication.
The name ESP32_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 Bluetooth Data
Inside the loop:
{
command = SerialBT.read();
}
This checks if Bluetooth data has been received.
The received character is stored in the command variable.
8️⃣ Command Processing
The program checks which command was received.
Example:
{
moveForward();
}
If the phone sends F, the robot moves forward.
Similar conditions control other movements.
9️⃣ Movement Functions
Each movement is controlled using a function.
Example:
This function rotates both motors forward.
Forward Movement
digitalWrite(motor1A, HIGH);digitalWrite(motor1B, LOW);
digitalWrite(motor2A, HIGH);digitalWrite(motor2B, LOW);
Both motors move forward.
Backward Movement
digitalWrite(motor1A, LOW);digitalWrite(motor1B, HIGH);
digitalWrite(motor2A, LOW);digitalWrite(motor2B, HIGH);
Both motors rotate backward.
Left Turn
digitalWrite(motor1A, LOW);digitalWrite(motor1B, HIGH);
digitalWrite(motor2A, HIGH);digitalWrite(motor2B, LOW);
Left motor reverses while right motor moves forward.
Right Turn
digitalWrite(motor1A, HIGH);digitalWrite(motor1B, LOW);
digitalWrite(motor2A, LOW);digitalWrite(motor2B, HIGH);
Right motor reverses while left motor moves forward.
Stop Robot
digitalWrite(motor1A, LOW);digitalWrite(motor1B, LOW);
digitalWrite(motor2A, LOW);digitalWrite(motor2B, LOW);
All motors stop.
1️⃣0️⃣ Final Working Process
The Bluetooth Control Car works as follows:
1️⃣ The smartphone sends a command using Bluetooth.
2️⃣ The ESP32 receives the command.
3️⃣ The program checks the command type.
4️⃣ The ESP32 sends signals to the motor driver.
5️⃣ The motor driver controls the motors.
6️⃣ The robot moves according to the smartphone command.
1️⃣1️⃣ Common Mistakes
Students may encounter the following problems:
⚠ Phone not connecting → check Bluetooth pairing
⚠ Robot not moving → verify motor driver wiring
⚠ Commands not working → check app configuration
⚠ Robot moving wrong direction → swap motor wires
Always verify the wiring and Bluetooth connection first.
🚀 What Happens Next
Now that the Bluetooth Control Car program is uploaded, the final step is to test the robot and explore improvements.