Index
Controlling motors and servos is a fundamental aspect of robotics and automation projects with Arduino. Here’s an overview of the different types of motors and how to control them using Arduino, including examples for DC motors, servos, and stepper motors.
1. Types of Motors
DC Motors
DC motors are simple motors that can rotate in either direction when a voltage is applied. They are often controlled using motor drivers that can provide the necessary current and control direction and speed.
Servo Motors
Servo motors are specialized motors that allow precise control of angular position. They are commonly used in robotics for tasks requiring accurate positioning.
Stepper Motors
Stepper motors are used for precise positioning and control over rotational angle. They move in discrete steps, making them ideal for applications requiring exact movements.
2. Motor Drivers and Libraries
Motor drivers, such as the L298N or L293D, allow you to control the direction and speed of DC motors. Libraries like Servo
make controlling servos straightforward.
Example 1: Controlling a DC Motor with an L298N Driver
Components Needed
- Arduino Uno
- DC motor
- L298N motor driver
- External power supply for the motor
- Jumper wires
Wiring Diagram
- Connect the IN1 and IN2 pins of the L298N to pins 8 and 9 on the Arduino.
- Connect the ENA pin (enable pin for motor A) to 5V.
- Connect the OUT1 and OUT2 to the DC motor terminals.
- Connect the GND of the L298N to the Arduino GND and also connect the external power supply’s GND to the L298N GND.
Code Example
const int motorPin1 = 8; // IN1 on L298N
const int motorPin2 = 9; // IN2 on L298N
void setup() {
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
}
void loop() {
// Rotate clockwise
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
delay(2000); // Run for 2 seconds
// Stop the motor
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
delay(1000); // Wait for 1 second
// Rotate counter-clockwise
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
delay(2000); // Run for 2 seconds
// Stop the motor
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
delay(1000); // Wait for 1 second
}
Explanation
- IN1 and IN2 control the direction of the motor.
- The motor rotates clockwise for 2 seconds, stops for 1 second, rotates counter-clockwise for 2 seconds, and then stops again.
Example 2: Controlling a Servo Motor
Components Needed
- Arduino Uno
- Servo motor
- Jumper wires
Wiring Diagram
- Connect the control wire of the servo to pin 9 on the Arduino.
- Connect the power wire (usually red) to 5V on the Arduino.
- Connect the ground wire (usually black or brown) to GND on the Arduino.
Code Example
#include <Servo.h>
Servo myServo; // Create a Servo object
void setup() {
myServo.attach(9); // Attach the servo to pin 9
}
void loop() {
myServo.write(0); // Move to 0 degrees
delay(1000); // Wait for 1 second
myServo.write(90); // Move to 90 degrees
delay(1000); // Wait for 1 second
myServo.write(180); // Move to 180 degrees
delay(1000); // Wait for 1 second
}
Explanation
- The
Servo
library is included to simplify servo control. - The servo moves to 0 degrees, waits for 1 second, moves to 90 degrees, waits again, and finally moves to 180 degrees.
Example 3: Controlling a Stepper Motor
Components Needed
- Arduino Uno
- Stepper motor
- A4988 or ULN2003 driver (depending on the stepper motor type)
- External power supply for the motor
- Jumper wires
Wiring Diagram (using ULN2003 driver for a 28BYJ-48 stepper motor)
- Connect IN1, IN2, IN3, IN4 of the ULN2003 driver to pins 8, 9, 10, 11 on the Arduino.
- Connect the motor to the ULN2003 driver and power it from an external supply if needed.
Code Example
#include <Stepper.h>
const int stepsPerRevolution = 2048; // Change this depending on your motor
// Create an instance of the Stepper class
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
void setup() {
myStepper.setSpeed(15); // Set the speed to 15 RPM
}
void loop() {
myStepper.step(stepsPerRevolution); // Rotate one revolution clockwise
delay(1000); // Wait for 1 second
myStepper.step(-stepsPerRevolution); // Rotate one revolution counter-clockwise
delay(1000); // Wait for 1 second
}
Explanation
- The
Stepper
library is included to handle the stepper motor. - The motor rotates one full revolution clockwise, waits for 1 second, then rotates one full revolution counter-clockwise, and waits again.
Summary
- DC motors are controlled using motor drivers, allowing direction and speed control.
- Servo motors are controlled using the
Servo
library for precise angle positioning. - Stepper motors are managed using the
Stepper
library for precise control of rotational angles.
These examples provide a solid foundation for understanding and controlling motors and servos using Arduino, enabling you to create a wide range of robotics and automation projects.