βοΈ 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.