Motor control is needed to make motors start, stop, and move in the right way. It helps machines do useful work like moving wheels or arms.
0
0
Why motor control is needed in Arduino
Introduction
When you want a robot to move forward or backward.
When you need to control the speed of a fan or a pump.
When you want to open or close a door automatically.
When you want to control the direction of a motor in a toy car.
When you want to stop a motor safely after use.
Syntax
Arduino
// Example to control motor speed and direction int motorPin1 = 3; // Motor control pin 1 int motorPin2 = 4; // Motor control pin 2 int speedPin = 5; // PWM pin for speed control void setup() { pinMode(motorPin1, OUTPUT); pinMode(motorPin2, OUTPUT); pinMode(speedPin, OUTPUT); } void loop() { // Move motor forward digitalWrite(motorPin1, HIGH); digitalWrite(motorPin2, LOW); analogWrite(speedPin, 200); // Speed from 0 to 255 delay(2000); // Stop motor digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, LOW); delay(1000); // Move motor backward digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, HIGH); analogWrite(speedPin, 150); delay(2000); // Stop motor digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, LOW); delay(1000); }
Use digitalWrite to set motor direction pins HIGH or LOW.
Use analogWrite on PWM pin to control motor speed from 0 (stop) to 255 (full speed).
Examples
This makes the motor spin forward at full speed.
Arduino
// Move motor forward at full speed
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
analogWrite(speedPin, 255);This stops the motor by turning off both direction pins and speed.
Arduino
// Stop the motor
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
analogWrite(speedPin, 0);This makes the motor spin backward at half speed.
Arduino
// Move motor backward at half speed
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
analogWrite(speedPin, 128);Sample Program
This program moves the motor forward, stops it, moves it backward, and stops it again. It prints messages to the Serial Monitor to show what is happening.
Arduino
int motorPin1 = 3; int motorPin2 = 4; int speedPin = 5; void setup() { pinMode(motorPin1, OUTPUT); pinMode(motorPin2, OUTPUT); pinMode(speedPin, OUTPUT); Serial.begin(9600); } void loop() { Serial.println("Motor forward at speed 200"); digitalWrite(motorPin1, HIGH); digitalWrite(motorPin2, LOW); analogWrite(speedPin, 200); delay(2000); Serial.println("Motor stopped"); digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, LOW); analogWrite(speedPin, 0); delay(1000); Serial.println("Motor backward at speed 150"); digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, HIGH); analogWrite(speedPin, 150); delay(2000); Serial.println("Motor stopped"); digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, LOW); analogWrite(speedPin, 0); delay(1000); }
OutputSuccess
Important Notes
Always connect motor driver or motor controller between Arduino and motor to protect the board.
Use delays to see motor actions clearly in sequence.
Serial prints help you understand what the program is doing step by step.
Summary
Motor control lets you start, stop, and change motor direction and speed.
You use digital pins for direction and PWM pins for speed control.
Controlling motors is important for robots, fans, doors, and many machines.
