In this tutorial, we will learn how to control a DC motor, using the Motor Shield Rev3, a shield compatible with the Arduino UNO. We will take a look at three different pins: brake, pwm & direction, where we will create a simple sketch that uses all three of them. In addition we will also take a look at how we can power our project, using an external power source.
The goals of this project are:
There are several ways we can control a DC motor, perhaps the easiest one is just by applying power to it. Very early inventions using the DC motor simply worked like that: add a power source and the motor will start rotating, switch the polarity and you switch the direction.
But if we want to do a bit more than just making a motor spin full speed in two directions, we need a motor control circuit. More specifically, the dual full-bridge driver L298P, which we can find on the Motor Shield Rev3.
With this IC, we can set the work duty (0-100), enable brakes (HIGH, or LOW), and set the direction (HIGH or LOW). Each of these features can be controlled using a different set of pins. As we are going to control a DC motor in this tutorial, let's take a look at the pins that are used:
Channel A:
Channel B:
Now if we were to use just the bare chip, it would involve a bit more complex circuitry. Fortunately, all we have to worry about is connecting the DC motor to one of the channels, connect an external power source, and we are good to go!
Let's begin by mounting our Arduino Motor Shield Rev3 on top of an Arduino UNO.
Now, let's connect the motor to the A channel, following the image below. The channels are marked next to the screw terminals on the shield.
Finally, we can connect the USB cable to the computer.
We will now get to the programming part of this tutorial.
First, let's take a look at some key commands in the code. We are actually not using a library, as the operation is very basic.
int directionPin = 12;
- assign direction pin.int pwmPin = 3;
- assign PWM (work duty) pin.int brakePin = 9;
- assign brake pin.digitalWrite(directionPin, state)
- sets the direction of the pin by using HIGH or LOW states. digitalWrite(brakePin, state)
- release or activate brakes, using HIGH or LOW states.analogWrite(pwmPin, 30)
- write a value between 0-100 to set the work duty.directionState = !directionState
- a boolean that switches every time the loop is run.The sketch can be found in the snippet below. Upload the sketch to the board.
1int directionPin = 12;2int pwmPin = 3;3int brakePin = 9;4
5//uncomment if using channel B, and remove above definitions6//int directionPin = 13;7//int pwmPin = 11;8//int brakePin = 8;9
10//boolean to switch direction11bool directionState;12
13void setup() {14 15//define pins16pinMode(directionPin, OUTPUT);17pinMode(pwmPin, OUTPUT);18pinMode(brakePin, OUTPUT);19
20}21
22void loop() {23
24//change direction every loop()25directionState = !directionState;26
27//write a low state to the direction pin (13)28if(directionState == false){29 digitalWrite(directionPin, LOW);30}31
32//write a high state to the direction pin (13)33else{34 digitalWrite(directionPin, HIGH);35}36
37//release breaks38digitalWrite(brakePin, LOW);39
40//set work duty for the motor41analogWrite(pwmPin, 30);42
43delay(2000);44
45//activate breaks46digitalWrite(brakePin, HIGH);47
48//set work duty for the motor to 0 (off)49analogWrite(pwmPin, 0);50
51delay(2000);52}
After we have uploaded the code, the program will start running immediately. If everything is working correctly, the motor should start spinning as soon as the program finishes uploading.
The expected outcome is that the motor spins in one direction for 2 seconds, with the work duty set to
30
(quite low), and brakes disengaged.When 2 seconds have passed, the brakes are activated and work duty is set to 0. This means the motor comes to a full stop.
If the code is not working, there are some common issues we can troubleshoot:
In this tutorial, we have learned how to control a DC motor using the Motor Shield Rev3. With the brake, pwm and direction, you now have a lot more options when it comes to motor control. Having this type of control allows you to create many different cool projects, without having to create complicated programs or advanced circuits!