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

⚙️ Controlling Motors with Code

🎯 Lesson Objective

In this lesson, students will understand:

• How microcontrollers control motors using programming
• Why motor drivers are required to control motors
• How direction control works in DC motors
• How motor control signals are sent from the microcontroller
• How robots use motor control to move

This lesson explains how programs control robot movement by controlling motors.


1️⃣ Why Motor Control is Important

Motors are responsible for movement in most robots.

In mobile robots, motors allow the robot to:

🚗 Move forward
🔙 Move backward
↩ Turn left
↪ Turn right
🛑 Stop movement

The microcontroller sends signals to the motor driver module, which then controls the motors.

Programming allows the robot to control these movements automatically.


2️⃣ Why Motors Cannot Be Connected Directly

Microcontrollers like ESP32 cannot directly drive DC motors because:

⚡ Motors require higher current
🔥 Direct connection may damage the microcontroller

To solve this problem, a motor driver module is used.

The motor driver acts as an interface between the microcontroller and the motors.

Example motor driver:

L298N Motor Driver

The microcontroller sends low-power control signals to the motor driver, and the driver supplies the required power to the motors.


3️⃣ Motor Control Using Digital Signals

Motor drivers control motors using digital signals from the microcontroller.

For example:

Input 1 Input 2 Motor Action
HIGH LOW Motor rotates forward
LOW HIGH Motor rotates backward
LOW LOW Motor stops

By changing these signals, the microcontroller can control motor direction.


4️⃣ Example Motor Control Code

Example program to rotate a motor forward:

int motor1 = 5;
int motor2 = 18;

void setup() {
pinMode(motor1, OUTPUT);
pinMode(motor2, OUTPUT);
}

void loop() {
digitalWrite(motor1, HIGH);
digitalWrite(motor2, LOW);
}

Explanation:

• Pin motor1 is set HIGH.
• Pin motor2 is set LOW.
• The motor rotates in one direction.

Changing the signals will reverse the motor direction.


5️⃣ Controlling Robot Movement

In robots with two motors, each motor controls one side of the robot.

By controlling the motors separately, the robot can perform different movements.

Examples include:

🚗 Move Forward

Both motors rotate forward.


🔙 Move Backward

Both motors rotate backward.


↩ Turn Left

Right motor rotates forward while left motor stops or reverses.


↪ Turn Right

Left motor rotates forward while right motor stops or reverses.


🛑 Stop Robot

Both motors stop rotating.


6️⃣ Using Functions for Motor Control

Motor control commands are often placed inside functions.

Example:

 
void moveForward() {
digitalWrite(motor1, HIGH);
digitalWrite(motor2, LOW);
}

This function moves the robot forward.

Example:

void stopRobot() {
digitalWrite(motor1, LOW);
digitalWrite(motor2, LOW);
}

Using functions makes motor control easier and keeps the program organized.


7️⃣ Motor Control in Robotics Projects

Motor control is used in many robotics systems.

Examples include:

🚧 Obstacle avoiding robots
🛤 Line follower robots
📱 Bluetooth control robots
🎤 Voice control robots

The microcontroller reads sensor data and sends motor commands based on program logic.

This allows robots to move intelligently and respond to environmental conditions.


8️⃣ Motor Control Flow in a Robot

A typical motor control process works as follows:

1️⃣ Sensors detect environmental conditions.
2️⃣ The microcontroller reads sensor data.
3️⃣ The program checks conditions using if statements.
4️⃣ The program sends signals to the motor driver.
5️⃣ The motors move according to the command.

This cycle repeats continuously while the robot operates.


🚀 What Happens Next

Now that you understand how motors are controlled using programming commands, the next step is to learn how to write clean and well-organized code.

Scroll to Top