Hello everyone! ![]()
In this post, I’m sharing a simple yet effective motor control code for Arduino, ideal for controlling small robots or any project involving motorized movement. This code allows you to control your motors for basic movements: forward, backward, turn left, turn right, and stop, all while utilizing PWM (Pulse Width Modulation) for speed control.
The PWM allows for fine-tuned motor speed control, which is crucial for a variety of applications such as robot movement or driving DC motors efficiently. This code is simple enough for beginners to understand and use, yet flexible enough to adapt to various motor configurations and projects.
In this project:
- Motor 1 (right motor) and Motor 2 (left motor) are controlled independently, allowing for directional movement as well as rotation.
- You can adjust the speed using PWM values (from 0 to 255), and the delay() function controls the timing of the movements.
I hope this will be helpful for those starting their journey with motorized robots or anyone interested in learning motor control via Arduino.
Here’s the code:
/*
Arduino Motor Control Code: Forward, Backward, Turn, and Stop with PWM
This code allows you to control a robot's motors to move forward, backward, turn left, turn right, and stop.
The speed of the motors is controlled using PWM (Pulse Width Modulation).
Each motor has two pins for direction control and one pin for speed control.
*/
const int motorPinR1 = 7; // Motor Right IN1
const int motorPinR2 = 8; // Motor Right IN2
const int motorPinREna = 6; // Motor Right ENA (Speed Control)
const int motorPinL1 = 9; // Motor Left IN1
const int motorPinL2 = 10; // Motor Left IN2
const int motorPinLEna = 11; // Motor Left ENB (Speed Control)
/*
Function to control the movement of a motor (Left or Right).
direction: 1 for forward, -1 for backward, 0 to stop.
speed: The speed value (0-255) for PWM control.
*/
void moveMotor(int motorPin1, int motorPin2, int motorPinEna, int direction, int speed) {
if (direction == 1) { // Move forward
digitalWrite(motorPin1, HIGH); // Set IN1 HIGH
digitalWrite(motorPin2, LOW); // Set IN2 LOW
} else if (direction == -1) { // Move backward
digitalWrite(motorPin1, LOW); // Set IN1 LOW
digitalWrite(motorPin2, HIGH); // Set IN2 HIGH
} else { // Stop (direction == 0)
digitalWrite(motorPin1, LOW); // Set both IN1 and IN2 to LOW
digitalWrite(motorPin2, LOW);
}
// Control motor speed using PWM (analogWrite)
analogWrite(motorPinEna, speed);
}
/*
Move the robot forward.
Accepts speed (0-255) to set motor speed.
*/
void moveForward(int speed) {
moveMotor(motorPinR1, motorPinR2, motorPinREna, 1, speed); // Move right motor forward
moveMotor(motorPinL1, motorPinL2, motorPinLEna, 1, speed); // Move left motor forward
}
/*
Move the robot backward.
Accepts speed (0-255) to set motor speed.
*/
void moveBackward(int speed) {
moveMotor(motorPinR1, motorPinR2, motorPinREna, -1, speed); // Move right motor backward
moveMotor(motorPinL1, motorPinL2, motorPinLEna, -1, speed); // Move left motor backward
}
/*
Rotate the robot to the right.
Right motor moves forward, left motor moves backward.
*/
void turnRight(int speed) {
moveMotor(motorPinR1, motorPinR2, motorPinREna, 1, speed); // Right motor forward
moveMotor(motorPinL1, motorPinL2, motorPinLEna, -1, speed); // Left motor backward
}
/*
Rotate the robot to the left.
Right motor moves backward, left motor moves forward.
*/
void turnLeft(int speed) {
moveMotor(motorPinR1, motorPinR2, motorPinREna, -1, speed); // Right motor backward
moveMotor(motorPinL1, motorPinL2, motorPinLEna, 1, speed); // Left motor forward
}
/*
Stop both motors.
*/
void stopMotors() {
moveMotor(motorPinR1, motorPinR2, motorPinREna, 0, 0); // Stop right motor (set direction to stop and speed to 0)
moveMotor(motorPinL1, motorPinL2, motorPinLEna, 0, 0); // Stop left motor (set direction to stop and speed to 0)
}
/*
Setup function runs once at the beginning to initialize motor pins.
*/
void setup() {
// Initialize motor pins as OUTPUT
pinMode(motorPinR1, OUTPUT);
pinMode(motorPinR2, OUTPUT);
pinMode(motorPinREna, OUTPUT);
pinMode(motorPinL1, OUTPUT);
pinMode(motorPinL2, OUTPUT);
pinMode(motorPinLEna, OUTPUT);
}
/*
Main loop function runs repeatedly to perform the robot movements.
*/
void loop() {
// Move forward with speed 100
moveForward(100);
delay(2000); // Keep moving forward for 2 seconds
// Move backward with speed 100
moveBackward(100);
delay(2000); // Keep moving backward for 2 seconds
// Turn right with speed 100
turnRight(100);
delay(2000); // Keep turning right for 2 seconds
// Turn left with speed 100
turnLeft(100);
delay(2000); // Keep turning left for 2 seconds
// Stop both motors after completing all movements
stopMotors();
delay(1000); // Wait for 1 second before starting the next iteration
}
`List item`
Explanation:
- moveMotor() Function: This function controls the movement of one motor (left or right). It takes the direction and speed as inputs and uses
digitalWrite()to set the motor direction andanalogWrite()for speed control (PWM). - Movement Functions:
- moveForward(): Moves both motors forward.
- moveBackward(): Moves both motors backward.
- turnRight(): Rotates the robot to the right (right motor forward, left motor backward).
- turnLeft(): Rotates the robot to the left (right motor backward, left motor forward).
- stopMotors(): Stops both motors by setting the direction to stop and speed to 0.
Customizations:
- Motor Speed: Adjust the motor speed using PWM values (0-255) in the functions.
- Motor Pins: Modify the pin numbers if needed to fit your hardware.
- Movement Timing: You can change the
delay()values to control how long the robot moves or turns.
Conclusion:
This code is perfect for controlling basic motor movements with speed adjustment using PWM. It’s great for beginners who want to get started with Arduino-based robot movement projects!