The control of various motors has always been one of the basic applications of Arduino. For example, the board can be used to control a DC motor, a servo motor or a stepper motor. Today we will analyse the first two of them, namely DC and Servo motors, which are the most popular among electronic hobbyists due to their simplicity and the available functions. Let’s have a look at the following aspects:
- What is a DC motor
- How to control a DC motor using an Arduino Motor Shield
- How to control a DC motor using an Arduino Motor Driver
- What is a Servo Motor
- How to control a Servo motor with Arduino
What is a DC motor
As promised, we first want to check what DC motors are. You may not be aware of it, but you use them every day – they are used in your vacuum cleaner or hair dryer, in elevators and in many other places. They are so popular because of their simplicity and efficiency compared to other types of motor. A DC motor consists of a stator, armature, rotor and commutator. This simple construction is enough to convert DC electrical energy into mechanical energy. And how is this possible? When current flows through the motor, the opposite polarity between the two magnetic fields inside the motor causes it to rotate as long as the current passes through the device. DC motors usually do not have their own polarity (and if they do, then they are marked with + or -), which means that you can easily change the direction of rotation by reversing the wires.
How to control a DC motor using an Arduino Motor Shield
The Motor Shield is a special driver module based on the Dual Full-Bridge Drive Chip L298. It allows you to use your Arduino to control the speed and direction of the motor, and even control two DC motors at once (or one stepper motor). To learn more about the L298 chip, you can visit the Seeedstudio blog. Now let’s get down to the basics – what do you need to control a DC motor with your Arduino Motor Shield? The list is quite short:
Step-by-step instructions
Step 1: Set SEN_A and SEN_B and connect the 2 pins that are left with a jumper.
Step 2: Since you do not need an external power supply for this project, connect MB_EN together with a jumper.
Step 3: Connect the DC motor to channel 0 (OUT1 and OUT2), connect the motor shield to your Arduino and then connect the board to your PC using a USB cable. Make sure that your connection looks like this:
Step 4: Install the Motor Shield library (it can be downloaded here, and the guide on the installation itself is available here).
Step 5: Upload the following code to Seeeduino:
// Demo function:The application method to drive the DC motor.
// Author:Loovee ([email protected])
// 2016-3-11
#include "MotorDriver.h"
MotorDriver motor;
void setup()
{
// initialize
motor.begin();
}
void loop()
{
motor.speed(0, 100); // set motor0 to speed 100
delay(1000);
motor.brake(0); // brake
delay(1000);
motor.speed(0, -100); // set motor0 to speed -100
delay(1000);
motor.stop(0); // stop
delay(1000);
}
// END FILE
Your motor should now start and move for 1 second, then stop for 1 second, move for 1 second, stop for 1 second again and then form a loop. If this does not happen, check that the code has been uploaded successfully and that the motor is connected correctly. The LED indicator should also flash properly.
That is all! You have started a DC motor with your Arduino Motor Shield in just a few steps! You can be proud of yourself.
How to control a DC motor using an Arduino Motor Driver
The Grove – I2C Motor Driver V1.3 (the latest version) is a driver that can be used to control two brushed DC motors or one 4-wire two-phase stepper motor; they can be controlled simultaneously at different speeds and directions. The drive is uses the L298N chip controlled by an Atmel ATmega8L: it can handle current of up to 2A per channel and allows you to use I2C communication (read more about this chip in the Seeedstudio blog). What do you need to get started? See the list below:
- Seeeduino V4.2
- Grove – I2C Motor Driver V1.3
- Base Shield V2 (optional, it facilitates the connections)
Step-by-step instructions
Step 1: Set the address using the dial switch that was introduced in V1.3. The default address setting in the program is 0x0f – make sure not to keep the address in the program the same as on the motor driver.
Step 2: Connect the driver to Port I2C of Grove-Base Shield and connect Grove – Base Shield to Seeeduino. If you have decided not to use the Grove basic shield, connect the motor driver directly to Seeeduino as below:
5V – red
GND – black
SDA – white
SCL – yellow
Step 3: Connect the Seeeduino to PC with a USB cable. Make sure your connection looks like this:
Step 4: Download and install the Grove I2C Motor Drive V1.3 Library from Github (here; the instructions for the installation itself are available here).
Step 5: Upload the code below to Arduino IDE:
// default I2C address is 0x0f
#define I2C_ADDRESS 0x0f
void setup()
{
Motor.begin(I2C_ADDRESS);
}
In this step you have established the connection between the DC motor, the motor driver and Arduino. The code below contains 2 functions that you may find useful:
// Set the speed of a motor, speed is equal to duty cycle here
void speed(unsigned char motor_id, int _speed);
// Stop one motor
void stop(unsigned char motor_id);
The speed() function enables you to drive a motor at the preferred speed. Note that:
- Motor_id – specifies which motor to use: MOTOR1 or MOTOR2
- _speed – specifies the speed of the motor. You can enter -100 to 100 here.
When _speed> 0, the DC motor runs clockwise; with _speed< 0, the DC motor runs counterclockwise. The higher the absolute value of _speed, the higher the speed of the DC motor DC.
The second function – stop() – allows you to stop a running DC motor.
What is a Servo Motor
The Servo motor is a rotary actuator that allows you to control the angular position precisely. A servo motor consists of: a control circuit, a small DC motor and potentiometers. It is screwed to the housing and then put on the shaft to connect it to a wheel, for example. The motor is controlled by an analog or digital electric signal to determine the movement that represents the final target position. Servos are suitable for closed-loop systems where precise position control is essential; while a servo is part of such a system, it remains a self-contained electrical device that rotate parts of a machine with high efficiency and precision. Thanks to this precision and performance, servos are suitable for many applications: they are used in robots, aircraft and various commercial applications that require precise position control.
How to control a Servo motor with Arduino
The last guide will be a bit shorter – you are only three steps away from being able to control the servo motor with your Arduino. You will need:
- Seeeduino V4.2
- Grove-Servo
- Base Shield V2 (to facilitate the connection)
Step-by-step instructions
Step 1: Connecting Servo to your Seeeduino
You can see that servo has three wires: power (usually red – must be connected to the 5V pin), ground (black or brown – should be connected to a ground pin) and signal (can be yellow, orange or white – must be connected to the D5 pin). Note that you can change the port, but then you must change the port number in the code as well.
Step 2: Connecting the module to the PC
Connect the module to the D5 port on Base Shield, then connect Grove-Base Shield to Arduino and Arduino to PC (using a USB cable).
Step 3: Software configuration
Our goal is to turn the shaft of a servo back and forth across 180 degrees using the code available at Arduino Servo Library. To obtain the code, follow the path: File -> Examples -> Servo -> Sweep.
/* Sweep
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
modified 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Sweep
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(5); // attaches the servo on pin 5 to the servo object
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
Success! Everything is ready. Once you upload the code, the servo should dance the way you wanted it to!
The use of Arduino to make motors run has always been popular. Our step-by-step guide showed how to run two different types of motors – a DC motor and a servo motor. However, there are many other applications of Arduino as far as other motors and tools are concerned. If you have any questions, please visit the comments section. Good luck!
How useful was this post?
Click on a star to rate it!
Average rating 0 / 5. Vote count: 0
No votes so far! Be the first to rate this post.