Among the tons of components that robots require, a motor shield designed to control DC or stepper motors plays a big role in robotics and for anyone who wants to have a full control over the speed and the direction of the electric motors positioned inside the robot.
Arduino and its products are very popular among hackers, makers, or engineers, and a motor shield such as R3 is able to extend the functionality of Arduino boards by adding additional hardware.
Choosing the wrong motor shield can greatly affect the design of the robot in a negative way, and that’s why in this post, I will explain a series of features, techniques, tutorials, and explore a few examples so that you can start the physical work with the Arduino Shield R3 and find inspiration for your DIY robot.
A few things to know before drive your motors with R3 shield
- the Arduino R3 should be powered with an external power supply;
- the motor shield is designed to control the speed and direction of each motor independently;
- the shield can be used to measure the current absorption of each motor separately;
- the R3 is engineered to drive two DC motors or one stepper motor with your Arduino board;
Table of Contents
ToggleGetting Started with Motor Shield R3
The Arduino as a smart card microcontroller has zero value without additional components and the power to control these. Like any of the Arduino boards, Arduino shields are available worldwide at an affordable price. On the market are available several hundred Arduino shields including here shields for wireless communication, navigation, or the shields to control electric motors.
R3 Specifications
- Motor controller: L298P;
- Operating voltage: 5V to 12V;
- External power supply: 2A/channel or 4A max. ;
- Curent sensing: 1.65V/A;
- Functions: run, stop, and break;
Buy Arduino Motor Shield R3 from here
As a motor driver board, the R3 is a motor shield based on the L298P dual full bridge motor driver and it’s engineered to drive small relays, solenoids, DC and stepping motors with features that not exceed 12V and up to 2A.
Installation
The motor shield R3 is designed to be mounted directly on the top of the Arduino microcontroller and connected to the Arduino via pin headers. The motor shield R3 is not compatible with all Arduino boards. Its pins are aligned to work only with Arduino Uno R3.
R3 Features
With 2 channels that lets you to control two DC motors or only one stepper motor, the motor shield has additional 6 headers for inputs, outputs, and communication lines.
The motor shield R3 includes additional logic features that make easier to drive DC motors.
Connected to Arduino, you can refer pins of the shield to specify the motor spin direction, to set the speed of the motor, to start or stop the motor, or to monitor the current absorption of each motor separately.
How the pins of the shield are divided by the channel:
Function
- Channel A
- Channel B
Direction
- Digital 12
- Digital 13
Speed
- Digital 3
- Digital 11
Break
- Digital 9
- Digital 8
Current Sensing
- Analog 0
- Analog 1
Programming
Thanks to a very active community of hackers and hobbyists on forums and blogs, you can find a lot of code to drive almost any electric motor that can fit in the motor shield specifications.
Before starting to write code, you have to plug the Arduino board into your computer’s USB port and install the Arduino IDE.
The whole process of controlling an electric motor could be split up into four parts.
First part: initiate the motor pins in the “setup()” function
1
2
3
4
5
6
7
8
9
10
11
|
//remember that the R3 shield has Chanel A and Channel B
void setup()
{
//the code to setup channel A
pinMode (12, OUTPUT); // sets the digital motor pin 12
pinMode (9, OUTPUT); // sets the digital break pin 9
//the code to setup channel B
pinMode (13, OUTPUT); // sets the digital motor pin 13
pinMode (8, OUTPUT); // sets the digital break pin 8
}
|
Second part: brake the motor
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//this code is part of void loop() function
void loop(){
/*with dynamic braking features,
the shield can stop the motor very quickly*/
/*the code to disengage the motor
must be before the code of spinning the motors*/
//channel A
digitalWrite(9, LOW); //disengage the motor
//channel B
digitalWrite(8, LOW); //disengage the motor
}
|
Third part: setup the spin direction of the motor
1
2
3
4
5
6
7
8
|
//this code is part of void loop() function
void loop(){
/*you can change the direction
by changing the parameter ‘HIGH’ with ‘LOW’ */
digitalWrite(12, HIGH); //setup the direction of channel A
digitalWrite(13, HIGH); //setup the direction of channel B
}
|
Fourth part: change the speed of the motor
1
2
3
4
5
6
7
8
9
10
11
|
//this code is part of void loop() function
void loop(){
/* the speed of each motor can be split up
in 256 steps of resolution. 0 value means
that the motor doesn’t rotate, while
255 value means that the electric motors
works at high speed*/
analogWrite(3, 255); //channel A motor at maximum speed
analogWrite(11, 255); //channel B motor at maximum speed
}
|
Tutorials
Below you’ll find tutorials that teach you how to control DC motors and stepper motors using the Uno R3 together with Arduino motor shield R3.
Control DC Motors
You can control two DC motors in the same time using the Arduino R3 shield. One motor is controlled via channel A, while the second motor is controlled via channel B.
Arduino Motor Shield Tutorial
Comprehensive tutorial from where you can learn how to control DC motors as well as a stepper motor using the Uno and the Arduino motor shield R3.
Arduinounomas
Simple tutorial with code able to control a DC motor in one direction and then in the opposite direction.
Control Stepper Motors
Driving a stepper motor using the Arduino motor shield is not a complicated task. Below you’ll find several tutorials from where you can start and learn how to control stepper motors with R3 shield.
Mercury Motor: Driving a stepper with the Arduino Motor Shield[link removed]
Simple tutorial from where you can get started to control a stepper motor.
Arduino and the official motor shield R3
This tutorial explains you how to use the Stepper library provided by Arduino and start working with stepper motors.
Arduino Motor Shield R3 Current Sensing
The motor shield designed in the Arduino lab is a wonderful device that lets you control the motors of the robot and in the same time let you monitor the current of the motor.
You can display the voltage of the motor with the following code:
1
2
3
4
5
|
void loop(){
sensorValue = analogRead(sensorPin);
float voltage = sensorValue * (5.0 / 255);
Serial.println(voltage);
}
|
Examples
If you want to play more with the Arduino, a motor shield and electric motors, you can find some inspiration in the below examples.
Floor vacuum cleaner robot – controlled by Arduino with motor shield, with printed motor-wheels
Arduino project: Hacking the Attacknid
There are a lot of reasons to have in your Arduino arsenal an motor shield able to control electric motors. I hope that these techniques, tutorials and examples will introduce and help you shape the motor control with Uno R3 and the motor shield R3.