Hey! This I'ble is now out of date!
I made an updated version which you can read here! Hope you find it fun and useful!
Thus far, I've shown you how to create wheels out of household items, severalmotorized chassis out of cardboard, and a couple of simple and effective light sensors. Now, one of the last major pieces is about to fall into place.
Almost every robot needs to power a motor of some sort or another. Problem is that motors take quite a lot of power, compared to what most microcontrollers operate with. To solve this problem, robots use what is called a motor controller, which usually amounts to some form of electronic switch that can turn on a very high voltage, using a very low one. That's what we'll be making today!
Let's get started!
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 the following:
- At least two colors of wire.
- I use solid-core wire. It's pretty standard to use red for power and black for ground, but my black is running low so I picked white.
- (Optional) Two more colors of wire.
- It can be helpful to organize your signal wires by color. I'm using yellow and blue.
- The DP32 from Digilent.
- Most any microcontroller board will do, but the DP32 is cheap and has a built-in breadboard where you can wire everything.
- A couple of motors.
- These ones I salvaged from a couple of old, broken printers.
- A battery pack.
- I usually use four AA or AAA batteries, because that works well for most microcontroller boards.
- A screwdriver.
- This is just for screwing in your battery leads.
- Wire cutters and wire strippers.
- Two negative transistors.
- I'm using the N-FET transistors from Digilent's chipKIT starter kit. Make sure you're using N-FETs and not BJTs (NPN or PNP transistors), as BJTs are more complicated than MOSFETS.
- Mine are part number ZVN2110A, in case you were wondering.
- Two sets of two header-pins each.
You'll also need (but are not pictured):
- A soldering iron.
- (Optional) A hot glue gun.
- I only used this for my header pins, so if you don't have any of those, you don't need it at all!
Step 2: Circuit Diagram
I've received some requests for circuit diagrams for this and my line sensor tutorial, but it's always been my intent to provide one for you folks. However, I still haven't found a circuit diagramming program that I'm completely happy with. Instead of making you folks wait, I figured I would just use one of the black boards we have around the Digilent Makerspace, to draw up a quick diagram and explain what's going on.
This circuit is extremely simple, as you can see. The motor is connected directly to the 6 volt output of the batteries that power our board (no regulators or anything). Then, it's connected to ground through our N-FETs, and the gate pin of the N-FETs are connected to one of our DP32's output pins.
What this allows us to do is use the N-FET like an electronic switch, turning our motors on and off. N-FETs are also very fast, so if you wanted to you could switch the motor on and off very rapidly, effectively giving it less power to make it run slower! What this circuit can't let us do is change the direction that our motor runs. For that, you'll need a more complicated circuit.
Step 3: Installing Transistors
This step is best explained by the pictures above, however I'd like to add a couple things here.
Firstly, it's very important to know which lead is which on your transistors. In the case of my N-FET transistors, Drain (the leftmost pin) connects to the higher voltage (in this case my motor's negative lead), while Source (the rightmost pin) connects to ground. You can tell which direction these N-FETs are facing because the "front" is slightly rounded (which you can see in picture 2).
Secondly, I'd like to point out that I chose where I placed my transistors very carefully, so that this tutorial would be fully compatible with my Line Sensortutorial.
Step 4: Placing your Header Pins
Once again, this step is best described by the pictures above, but I'd like to add a couple notes here.
I used male header pins for this step, as you can see above, because I had some lying around. I could get away with this because my motors both had female connectors, however many motors won't. That's why I'd recommend using female headers instead, because it's easier to simply insert wires into female headers.
That said, female headers are trickier to use, because you can't simply break them to length the way you can with male headers. Check out the Adafruitpage I linked to in the parts list, because the product pictures give a good illustration on how to break female headers. (That's something that theSparkfun page probably needs, based on all the angry comments. ;p)
But Jay! Why not just solder my motor leads to the board?
I'm very glad you asked that Strange-Voice-In-My-Head! Motor directions aretricky things! You can't always tell which direction a motor will spin when you hook it up to power and ground. If you solder your motor in and charge ahead, you might find that they don't spin the direction that you need them to! That's why I made sure to make my motor attachments changeable!
How about using an H-bridge instead?
Once again, a very good suggestion from the Strange-Voice-In-My-Head! For those of you who don't know, an H-bridge is a configuration of transistors that allows you to change the direction of your motors in your code! This is really handy when building for stuff that needs motors to run forwards and backwards. However, having used H-bridges quite a lot, I can say that I really don't like them, and for right now I just wanted to make the simplest possible motor controller. Now, I have heard that brmarcum is already working on a tutorial for a bi-directional motor controller that doesn't use an H-bridge (gasp!), so keep an eye out for that!
Step 5: Adding Signal Pins
You're going to want to connect pins PGC and RB2 of your board (pins 12 and 13) to the gate pins of your transistors. This will allow your DP32 to open and close the transistor's gate, which turns on and off the power for your motors, just like a switch!
I chose the PGC and RB2 pins for two reasons. First, these pins fit neatly into my Line Sensor tutorial. Second, these pins also connect to LED 2 and LED 3 on your board! That means that your on-board LEDs will light up whenever your board tries to power its motors. That's handy because if your motor or N-FET is busted for some reason, you can tell that your board is applying power, and it's not just a problem in code.
Step 6: Add Power Line
The whole point of adding motor controllers to your board is because your board can't output enough power to the pins to get some motors to spin at all, let alone pull a robot around. That means that we're going to have to connect our motors to a higher voltage input. The regulated 3V input simply isn't enough, so instead we're connecting our motors directly to V IN, meaning we're essentially powering our motors directly from the batteries!
Connect a wire between the VIN pin and the rail you soldered your header pins into. It doesn't matter too much which VIN pin you connect to (there are two on the board) but I find that it makes the most sense to connect it to the one above RA4.
Step 7: Connecting Your N-FETs to Ground
The last step in the wiring process will be to connect your N-FETs to ground. Once again, I'm pretty low on black wire, so I used white for my grounding wires.
Run your wires from the third pin of your N-FETs (the right-most pin in the pictures above) to the grounding rail on the left. Solder them in and you're done!
Step 8: Connect Motors, Power, and Test It Out!
Now you're finally ready to connect your motors to power and load up some code!
The example code I've provided runs a simple test of both motors, turning them on and off alternatively! This will also tell you what direction the turn when powered.
This tutorial is just a jumping off point. It's the simplest thing I could come up with to run motors on the DP32, and I've already gotten a couple of line-following robots to run very well using it! I'd love to see what you folks can do with it!
As always, please let me know what you liked about this tutorial, and feel free to give me advice on anything you think could be done differently. I've already got a plan to add some circuit diagrams to this and my line-sensor tutorial, as well as an explanation of what's going on, so keep an eye out for that soon!