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.
Why motor control is needed in 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).
// Move motor forward at full speed
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
analogWrite(speedPin, 255);// Stop the motor
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
analogWrite(speedPin, 0);// Move motor backward at half speed
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
analogWrite(speedPin, 128);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.
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); }
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.
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.
Practice
Solution
Step 1: Understand motor control purpose
Motor control lets you decide how fast and which way a motor spins.Step 2: Compare options with motor control function
Only It allows you to control the speed and direction of the motor. correctly states speed and direction control, which is the main reason for motor control.Final Answer:
It allows you to control the speed and direction of the motor. -> Option DQuick Check:
Motor control = speed and direction control [OK]
- Thinking motor control only runs motor at full speed
- Assuming motor control fixes hardware automatically
- Believing motor control prevents motor from stopping
Solution
Step 1: Identify pin types for motor control
Speed control uses PWM (Pulse Width Modulation) pins to vary power to the motor.Step 2: Match pin type to speed control
Only PWM pins can change motor speed by adjusting power pulses.Final Answer:
PWM pin -> Option BQuick Check:
Speed control = PWM pin [OK]
- Choosing digital pins for speed control
- Confusing analog pins with PWM pins
- Selecting ground pin by mistake
analogWrite(5, 128);
digitalWrite(4, HIGH);
Assuming pin 5 controls speed and pin 4 controls direction of a motor.
Solution
Step 1: Analyze analogWrite on pin 5
analogWrite(5, 128) sets PWM to about half power (128 out of 255), so motor speed is half.Step 2: Analyze digitalWrite on pin 4
digitalWrite(4, HIGH) sets direction pin HIGH, which we assume means forward direction.Final Answer:
Motor runs at half speed forward. -> Option AQuick Check:
Speed half + direction forward = half speed forward [OK]
- Assuming analogWrite 128 is full speed
- Confusing HIGH with backward direction
- Thinking motor stops with these commands
pinMode(3, OUTPUT);
digitalWrite(3, 255);
Solution
Step 1: Check digitalWrite parameters
digitalWrite only accepts HIGH or LOW, not numeric values like 255.Step 2: Validate pinMode and call order
pinMode(3, OUTPUT) is correct and must be called before digitalWrite, which is done here.Final Answer:
digitalWrite cannot take 255 as a value. -> Option AQuick Check:
digitalWrite = HIGH or LOW only [OK]
- Using numbers instead of HIGH/LOW in digitalWrite
- Switching pinMode to INPUT incorrectly
- Calling digitalWrite before pinMode
Solution
Step 1: Understand pin roles for motor control
Speed is controlled by PWM pins to vary power; direction is controlled by digital pins to set HIGH or LOW.Step 2: Evaluate each option
One PWM pin for speed and one digital pin for direction correctly pairs one PWM pin for speed and one digital pin for direction. Others misuse pin types.Final Answer:
One PWM pin for speed and one digital pin for direction -> Option CQuick Check:
Speed = PWM, Direction = digital [OK]
- Using digital pins for speed control
- Using analog pins for direction
- Trying to control both speed and direction with PWM only
Ready to prove it?
Pass the test and this pattern moves unknown
