Hello and welcome! For those of you who haven't read any of my I'bles before, and for those of you unfamiliar with my For Cheap Robots series, I'm all about building robots on the cheap using simple materials that you can find around your house! If you've ever wanted to get into robotics, but don't know what you're doing and don't have the money for an expensive robotics kit, you are my kind of people!
Previously in this series, I covered how to make some super cheap wheels for your motors, but those motors can't go anywhere without some power! Problem is, most micro-controllers won't be able to power very many motors all at once, and certainly not any large motors. To solve this, we use a sort of electronic switch called a transistor. This allows the micro-controller to turn on and off the power to the motor very easily! That way the micro-controller still controls the motor, but the power comes from somewhere else!
This was the simplest circuit I could come up with for controlling motors. It won't let you change which direction the motors turn, but it will let you turn your motors on and off, which is enough to do some simple things.
Let's get started!
(If any of this sounds familiar, that's because I've already created a motor controllers tutorial before. I'm learning this stuff myself, and I've gotta give a lot of thanks to everybody who gave me feedback on that I'ble because it's helped me come out with this new and improved version! Special thanks tonqtronix and Nate Ocean who were the first to point out the major flaws with my previous tutorial. Thanks guys!)
For more Instructables on building cheap robots, please check out the For Cheap Robots collection!
For more things that I've done, you can check out my profile page!
For more info from Digilent on the Digilent Makerspace, check out the Digilent blog!
Step 1: What You'll Need
For this project, you'll need:
- A soldering iron.
- Some tape.
- Thinner tape works best.
- You'll be using this to label things, so it should be tape you can write on in pen.
- A pen.
- With which to write upon the tape.
- Not pictured.
- A DP32
- You can use pretty much any microcontroller with this project, but I prefer the DP32 because of it's built-in breadboard. So convenient!
- A micro USB cable.
- You'll use this to program your DP32 with.
- A 6V battery pack.
- This will not only power the DP32, it'll also power the motors!
- At least two colors of wire.
- I use solid core wire because it's easy to handle.
- You'll want red wire for power, and black for ground.
- I also like to have at least two colors for "signal" wires. These aren't powering things, per se. They're just sending information.
- Two diodes.
- I'm using two 1N4001 diodes. I'm not really sure if these are the best to use for this application, but they should work fine.
- Two 10kOhm resistors.
- These are optional.
- Our DP32 actually has built in pulldown resistors, and you don't really need them for BJTs anyway.
- These are optional.
- Two transistors.
- I'm using two TIP32C BJT transistors.
- Pretty much any transistor will do, but it's important that you use BJTs, as opposed to MOSFETS.
- For more information on the difference between BJT transistors and MOSFET transistors, read brmarcum's BJT and MOSFET tutorials.
- I chose BJTs over MOSFETs because I want to trigger a higher voltage power source with a lower voltage signal, and MOSFETs (or at least the ones I had) seem to scale their output voltage by the triggering voltage, whereas BJTs scale with current.
- Two motors.
- Salvaged from anything!
- Make sure they're not fancy, brushless or stepper motors...
- I'll try and cover those soon!
- Wire strippers.
- Wire cutters.
- This will be used with the screw power terminals aboard the DP32.
Step 2: Circuit Diagram
This is the circuit diagram for the new and improved motor controller! I would normally also include the board layout, but Fritzing wasn't able to place components in the same way that I placed mine, so you'll have to follow along with my tutorial.
How it Works
Our board can only supply 3.3 volts, and has a limited number of amps it can handle too. So instead of being powered by the board, we connect the motors to our battery directly. No voltage regulators or anything!
This gives the motors plenty of power, but to give the DP32 controll, we break that circuit with a transistor.
Transistors act like an electronic switch. They have three leads, and in our case these are called the Collector, Emitter, and Base. The collector connects to our load (the motor), and the emitter connects to ground. Then the base acts like the switch, closing the circuit when it's driven to a high voltage (or actually a high current) by our microcontroller.
If we're comparing the transistor to a button or a switch, then we could call the pulldown resistor something like the spring that re-sets the button after you push it. In our case, we don't actually need pulldown resistors, but I didn't realize that until after I'd put the board together. Oops!
The reason we don't need them is because the DP32 actually has pulldown resistors built in. BJTs also have a sort of built-in reset mechanism, so our pulldown resistors are a triple redundancy. Oh well...
Step 3: Flyback Diodes
Flyback diodes get their own step because they were one of the big things I got corrected on in my previous I'ble, so I want to explain what I learned about why they're necessary.
Motors, and anything with a coil really, act like a sort of electrical flywheel. Once they get spinning, they don't want to stop! This can cause havoc on your circuit when you're trying to shut them down.
In the picture above you can see how the transistor is getting activated to complete the circuit and run our motor. In the second picture, when the transistor is de-activated and breaks the circuit, the motor wants to keep spinning. This transforms it from a load (something that takes energy) into a source (something that releases energy). With nowhere to go, all that energy just fries our poor transistor!
That's where the flyback diode comes in. By placing a diode across our motor, pointing the opposite direction, we give that energy a place to go. The energy released by the motor gets channeled back into the motor. The motor spins a little longer (the difference is so small you wouldn't even notice it) and the energy gets dissipated through resistance and friction. Yay entropy!
Here's the trick though. Because we used a diode, in the fourth image you can see how that diode doesn't let any electricity flow backwards across it. This forces all the current through our motor, allowing it to run normally!
Step 4: Motor Polarity
In my previous motor controller tutorial, I used some small female header pins to allow myself to re-wire the motors however I wanted. I did this because if you solder the motor wires onto the board, it's not easy to switch the direction of the motor afterward!
It's still a good idea to do this, but I realized that 14-year-old-me wouldn't have had access to female header pins like those. Because I strive to make these I'bles as accessible as possible, I'm going to forgo that feature, and instead I'll show you how to figure out the polarity of your motors so you can solder their wires directly to your board.
However, I would strongly suggest you order some male and female header pins soon! Check out my header pin tutorial for how you can use these to make your motors easily detachable!
Step 5: Direction Flag
Start by using your tape to add a flag to your motor shaft. This will show you what direction the shaft is rotating when you hook your motor up to a battery.
You'll also want to add a label to the top of the motor. Make sure it's pointing the same direction as I have show in the picture! When your motor is properly wired (i.e. red wire to power and black wire to ground) then it should rotateclockwise.
Step 6: Wiring and Testing
Connect your black battery lead to one of your motor leads. Really it can be the black or red battery leads, but I personally prefer to ground things first as a general rule. If your lead won't stay put on its own, you can bend the wire to hook it in a little (like the second picture above).
Next, take your other lead and touch it to the remaining motor lead. Keep an eye out for which way the flag spins! It can be hard to see sometimes.
If the flag doesn't rotate in the direction that the arrow indicates, then you have the wires reversed. Swap the red and the black wire and try again. This time the flag should rotate clockwise.
After you test, it's important to remember which motor lead the black wire was connected to, and which had the red wire! This will tell you which wire gets soldered where in the next step.
Step 7: Soldering your Motor Leads
I'm assuming you probably already know how to solder, so I'm going to go over this step in much detail. If you need to learn to solder, I would definintely recommend that you check out noahw's How to Solder Instructable. JColvin91 also has a fantastic I'ble where he explains how to use different soldering tips, so you should check that out too!
My only tip for this step is that you should make certain that you give the right motor leads the right color wire. Otherwise you'll have problems after you solder your motors in and realize they're backwards.
You'll also want to make sure to give your motors plenty of wire length. The only reason my wires were so short is because I was forced to use some pre-cut lengths for this tutorial. Otherwise I would have used wires around a foot long.
Step 8: Adding Transistors
Take your two transistors and slip them into the holes of your perf-board like I have shown in the first picture. It's really important that you get the right holes here, otherwise the rest of your project will be off. It's also important to make sure the left transistor is flipped the other direction.
If you check out the second pic, you can see that I accidentally soldered these in crookedly. It still functions fine, but it aggravates my perfectionist side. To avoid this, I'd recommend that you solder them into your board one at a time. Solder the first prong, and then bend that so the other prongs are seated properly. That'll hold them in place while you solder them in. Then solder the second transistor in.
Step 9: Adding Diodes
Slip your diodes into the board through the holes shown above.
See that little silver band on the outside edge of both diodes? The diode on the left has its band on the left, while the diode on the right has its band to the right. That's important because diodes are directional. If you put them in backwards, your circuit won't work and you'll fry your transistors.
In the third pic, I show you how I bend the leads so the diodes stay in one place while I solder them.
Step 10: Resistors
I'll point out again, these aren't necessary.
Step 11: Ground Wires
These are absolutely necessary.
It can also be a little tricky getting the lengths right, so I've included some pictures that show how I got them the right length. This trick only works if your wire is small enough to go through the breadboard holes with its insulation intact.
Step 12: Signal Wires
Finally, you'll want to add your signal wires in. You'll do these using the same trick I used for the grounding wires. I like to make sure it's easy to tell my right-sidesignal wires from the left-side ones, so I colored left green and right yellow.
Step 13: Motor Leads
This step is tricky, and you're going to have to think for your self a bit here. You need to make sure that your motors are hooked up properly for what you want to do. This circuit cannot make motors change direction, it only turns them on and off going one direction, so you need to make sure that when they turn on, they're spinning the direction you want them to.
Think back to when you were testing out your motor, and the arrow you put on it. When you hook the red wire to power, and the black wire to ground (in this case, your transistor's collector pin) then the motor will rotate in the direction that arrow indicates. If you want your motor to turn the opposite direction, then you'll need to switch your wires so that black is connected to power, and red is connected to ground.
In the second picture, you can see how one of my motors is hooked up with red connected to power, and the other is swapped so black is connected to power. That's because for this example I wanted both motors to turn opposite directions. That way they would both push my robot forward when they spun.
The final thing you need to take note of is how I soldered the leads onto my board. See how the outside solder beads jump over to the power rail? That's how your motors will connect to power.
Step 14: Connecting the Power and Ground Rails
Now you're going to connect your power and ground rails to their respective sources.
It's important that you connect the first railing (the one on the bottom) to the VIN power, and not the 3V3 power. The 3V3 is the board's regulated 3.3V power line, while VIN is the battery's raw, unregulated power. VIN is equivalent to running a line directly from the battery to your motor, and that's exactly what we want!
The other jump is from the second rail (the one on the top), to the grounding rail on the left. That ground is shared with your microcontroller, your battery, and now your motor controller! It's very important that everything share the same ground.
Step 15: Upload the Test Files
Start by downloading Motor_Tester.pde. Open it up in MPIDE.
Now, connect your board to the computer using your micro-USB cable.
If you've never programmed your DP32 before, you should check out myGetting Started with the DP32 tutorial. That will show you how to get around MPIDE, and how to program the DP32.
To get the DP32 into programmable mode, hold down the RESET button (on the left). Then hold down the PGM button (also labeled BTN2). Now release the RESET button, and then the PGM button after that. You should see LD1 blinking now, and your board will be programmable.
Make sure MPIDE is configured for your board, and set to the right port. Then hit the download button!
Depending on how big your motors are, they may start spinning just from the USB power. The ones I have pictured did, but I've used bigger motors that can only run off batteries.
Step 16: Connect batteries and test!
Use your small screwdriver to attach the leads of your battery pack to the DP32. MAKE SURE YOU WIRE THIS CORRECTLY. Check out the picture above. See how the red wire connects to the terminal furthest from the USB port? If you swap these by accident, you could fry your whole board!
Once you've got your battery connected, switch the jumper near the screw terminals (outlined above) to the "battery power" mode. You should see your motors jump to life!
Once again, this was the simplest circuit I could come up with to control motors. In the future, I'd like to try controlling some stepper and brushless motors that I've salvaged from various printers, CD drives, etc. I've also got some motors with shaft encoders, and I hope to create a tutorial for those as well.
For now, I hope you'll find this simple project useful and educational. I know I learned a lot from it myself! As always, if you see any errors, or way this tutorial could be improved, please let me know! I'd also love to see how you use it in your own projects!