Electric motors can be found inside just about everything you use. They are in everything from your phone (vibration motor), to your coffee machine (water pump, and even your windshield wipers have them! It should be no surprise that learning how to control an electric motor is a great skill to learn. With motors you can give your robot wheels to move, and hands to pick things up. The simplest way to control a DC motor is to use an H-Bridge. An H-Bridge is a circuit that allows you to control the speed and the direction of a DC motor. Building a full H-Bridge using transistors and mosfets can be a pain.
What You Will Need:
Arduino Compatible Dual Motor Shield (MC33932)
Arduino Board (Uno, Leonardo, Mega)
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Hardware and Assembly
This is where the MC33932 Motor shield comes in. It allows you to control two DC motors, or one bipolar stepper motor. In this tutorial we will show you how to assemble the shield and learn to use it to drive both types of motors. The shield comes with all the headers and connectors you will need to put the shield together, but you will need an Arduino Board (Uno, Leonardo, Mega,) to control the shield.
Step 2: Insert Headers
Start by inserting all the headers in their respective spots on the board. No need to solder anything just yet, we will do all four at the same time.
Step 3: Taping the Headers
Next we will use a piece of masking tape to hold all four pieces on. This makes it a breeze flipping before soldering.
Step 4: Soldering the Pins
To make it easier, solder one pin on each of the headers so that you can remove the tape. Once the tape is off, you can check the alignment of the headers before soldering all the pins in.
Step 5: Inserting & Soldering Screw Terminals
Next step is to solder in the screw terminals. Go ahead and place your screw terminals in place and use a piece of tape to hold them down.
Step 6: Soldering Screw Terminals (cont.)
Flip over the shield and solder the remaining 6 joints. Once you are done, you can remove the tape and mount the completed shield on your Arduino Board. We chose to work with an UNO, since we have a gazillion of these laying around our shop.
Step 7: DC Motor Connection
Next step is wiring up a motor and writing some code! We will first control a standard DC motor using this shield, and then show you how the board can also control a stepper. The MC33932 has two full H-Bridges built in so it can control two DC motors. The two channels are labeled A and B on the board. Hook up your DC motor to either of these channels, we chose to use channel A.
The second image is the completed motor shield mounted on an Arduino Uno and connected to a DC motor. The rest of the wires from the motor are for a built in encoder, which we will not be using for this tutorial. Now for the code!
Step 8: DC Motor Code
We have already written some code for this shield which you can download here. It includes the code to control both channels, as well as one bipolar stepper motor. Go ahead and open up the Dual_DC_Motor.ino sketch.
Below is what you should see when you open up the motor sketch. The first part of the code defines the motor control pins. PWM for channel A and B are connected to pins 3 and 5 respectively. The Direction control pins for channel A and B are connected to pins 8 and 7 respectively (Note: These don’t match the silk screen on our board because we accidently swapped the labels on the silkscreen).
The setup part of the code configures both Direction pins as outputs. There is no need to set the PWM pins as outputs, as this is already handled by the analogWrite functions. To control the speed, you call the function analogWrite(“Channel”, “Speed”) and pass it to whichever motor's speed you are trying to control (PWMA or PWMB) and the speed at which you want it to go (Between 0-255). To control direction, you use the digitalWrite function. A digitalWrite(“Channel” , LOW) will cause the motor to turn one way and a digitalWrite("Channel”, HIGH) will cause the motor to turn the other way. The complete code is simply turning one motor at a time in one direction, waiting a second, and then turning the other way for one second.
Step 9: DC Motor Code (cont.)
Upload the sketch, and connect your UNO to an external power supply to test the code. The motors will not turn if the Arduino is powered by the USB Port. Above, you can see we are using the UNO's DC barrel jack, to supply power to the board.
Note: Although we are powering the shield through the DC Barrel Jack, this is not recommended since you risk burning the thin traces on your Arduino. The shield should instead be powered using the VIn screw terminals (Max 28V).
In the second picture below, you can see the proper way to power the shield. Use an external power source connected directly to the shield. The shield will automatically route power to the UNO, so you do not need to power it separately.
Step 10: BONUS: Stepper Motor Example
As an added bonus, this shield can also control a single stepper motor all the way down to 5 volts. A stepper motor gives you the ability to control precise position without the complexity of position sensors and expensive closed loop controller. The zip file above also contains a sample code to control a stepper.
The sample code below uses a built in Arduino Stepper library to control the motor’s steps (first line in the code). We start by defining the number of steps our motor has per revolution, in our case this is 1.8 degrees per step, so 200 steps per revolution.
Next, we initialize the stepper class with the step per revolution, as well as the two pins used to control the direction of the outputs. The last two lines before the setup are to define the PWM pins, just like our last program.
The first line in our setup routine sets the speed at which we will want to move the stepper. The maximum speed is limited by the type of stepper you use (generally higher voltages mean higher RPM’s). For our stepper, we were able to get about 60 RPM, by driving the motor at its rated 5 volts.
Next, we need to set our PWM pins HIGH. We can do this using the analogWrite function, or the digitalWrite function. By setting the PWM pins to 255, both channels are always on and enabled. From this line on, the motor will just be controlled using the two direction pins. The Serial port is just used to display messages to the computer and is optional.
Finally, we get to the loop. The loop is turning the stepper one full revolution, pausing for half a second, and then one full revolution the in the other direction. You can use the myStepper.step() function, to step the motor in any directions.
Step 11: BONUS: Stepper Motor Example (cont)
Now, connect your stepper; channel A goes to one coil, and channel B goes to the second coil. Connect each pair to the shield; if you got it right, the motor will turn one full revolution, and then turn return back home. If the motor fails to turn, flipping one pair will fix the problem.
That concludes our motor shield tutorial. Now you are ready to put these motors to some good use. If you have any questions about this tutorial, don't hesitate to post a comment.
Check out our other Instructables, as well!
Participated in the
123D Circuits Contest