Motor Controllers for Cheap Robots 2




Introduction: Motor Controllers for Cheap Robots 2

About: I build robots out of boxes! I love teaching what I've learned and seeing people add their own ideas to what they've learned. Nothing excites me more than seeing a student really take an idea and run with it!

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 to nqtronix 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
  • 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.
    • 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.
    • Screwdriver
      • 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.

    To really understand transistors, you should definitely check out brmarcum's fantastic BJT and MOSFET tutorials, but I'll go over the absolute basics that we need here.

    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.

    Pulldown Resistors

    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 rotate clockwise.

    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 my Getting 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!


    Be the First to Share


      • Colors of the Rainbow Contest

        Colors of the Rainbow Contest
      • Cardboard Speed Challenge

        Cardboard Speed Challenge
      • Stone Concrete Cement Contest

        Stone Concrete Cement Contest



      5 years ago

      I was hoping me, you and a third robot creator could work together on something that could (potentially) be basically a little robot buddy, with an amazon echo for a brain.


      Reply 5 years ago

      Unfortunately I'm going to have to decline. I'll happily answer any questions you have, but I'm too busy with school, work, and my own projects.

      I wish you the best of luck, though!


      Reply 5 years ago

      I understand. I'm only 14 myself, and can't do much on my own until I get a job. I have school and boy scouts, which is (besides pure laziness) is the main reason no progress has been made.


      6 years ago on Introduction

      At the moment I am struggling with a DC-motorcontrol for a simple robot. I use similar motors with a 12:1 gear ratio and have difficulties in moving slowly in a controlled way. When using PWM-type motorcontrol I face non-lineairities like dead-zone and hysteresis. I use a (self-made 64-slot) encoder to asses position and speed.

      The Software based PI-controller works fine above a certain speed, but at lower speeds as I said I am struggeling.

      Any ideas, tips?


      Reply 6 years ago on Introduction

      Hrrm. Good question. Honestly I may not be the best person to ask, but I'll give it a shot.

      1) Make sure your encoder is driven directly from the motor and not from the gearbox. This will make it spin faster, and give you a higher sampling rate so you can sense properly at lower speeds.

      2) Don't use a PI-controller, use a PD-controller. PI is hard to do. PD is easy, and 90% of what you want will come from PD (50% comes from just the P).

      3) If you really like the integral component use a full PID and set it up so that your integral component only retains information that is less than some fraction of a second old. This will (theoretically) prevent it from getting out of control. (I haven't gotten a chance to try this out myself yet.)

      4) Make sure you aren't trying to get your motor to spin slower than it *can* spin. If you're applying too little voltage (too little duty cycle on your PWM), it won't spin.

      I wrote a whole bunch and then deleted it after making the summary above, so if you have any questions about any of the suggestions I gave, please ask. I would love to get a chance to expound on all of them. I think this is all really cool.

      Also, this gave me an idea for an Instructable that would be super cool. Thanks!

      Oh, and let me know when you get it working! I'd love to see the finished project! Good luck!

      (Does anybody else have a suggestion that I missed?)


      Reply 6 years ago on Introduction

      Hi Jay,

      Great to read your response, I is always great to bounce off ideas. I would like to reply to your suggestions as follows:

      1) Although technically not possible with the type of motors I use, the idea of increasing the sample rate is indeed the way I will progress now. The quality of the DIY encoders I have now is the limitation and I am working on an improved version that should allow to go to 256 slots per rev. Once these work I can share them on this forum. (maybe the once I now havecould be usefull for some applications).

      Increasing the number of slots does increase the sampling rate at low speeds at the expense of processor loads at high speeds. The encoder signals are handled on interrupt basis and with two wheels turning at high speeds this could be a limitation. From my calculations I should have still some room to go. In parallel I just finished the design of a circuit that converts encoder signals to an analog signal that can be read through one of the analog ports. I could update you on this one after making sure it works.

      2,3) I believe a PI-controller is in principle what I need. The I-part will resolve the dead-zone and the P-part will take care of the hysteresis once the motor starts turning. I could share some measurements on system response.
      Because the sample time depends on the speed of the motor the dynamics of the control system change. I solved this by using two parameter settings: one for high speed and one for lower speed.
      I included a systems diagram and the Arduino code is just 2-3 lines. It works really fine..... except when then robot can halt within two samples. The processor now will have to decide that apparently the motor stopped and has to start again.

      4) You refer to what I call the dead zone or threshold. The I-part takes care of this, I tuned it such that the starting condition is more or less ok (it depends on the battery condition a.o.). The P part will be dominant in the begining and the robot will start off a little too quick. Within a second however the P makes it move back the hysteris curve and allows it to run at speeds lower than the initial threshold. To facilitate this I changed the PWM freq to approx 31kHz to stay far from the mechanical dynamics of the system. I believe this contributed to a better over-all performance (but not beyond reasonable doubt though).

      The reason that low speeds are important is not only because I want to be able to move slowly forward, I plan to add a echo (HC-S04) sensor to scan the surroundings. For this the robot needs to move slowly arround its axis. With the circumference of the wheels being 204 mm and the diameter of the robot approx. 135 mm I need to be able to move around the 25mm/s. The lowest I can get now is 75 - 90 mm/s.

      I attached a picture of the robot (without the HC-S04 sensor) as well as the PI controller. Full features of the robot include:
      - Compass shield for orientation (works)
      - 433 MHz comm with other similar robots, Interrupt driven (works)
      - IR-distance sensor, (works)
      - Battery State sensor (works)
      - Inductance recharging of batteries (still to do)
      - Video-processing (still to do and may require a Raspberry PI add on for processing power.

      The intention is that a swarm (ehh 3) of these robots inspect the surroundings and share information with each other.

      I am still a little stuck in moving the robots in a sufficiently controlled way. (Yes I know steppermotors would have avoided many issues, but for me the journey is as much fun as the goal.)

      PI-Controller.pngfoto robotje 26-04.JPG

      Reply 6 years ago on Introduction

      Oh wow for a little while there I was shocked! 20cm wheels? What were you doing? Turns out you dropped a decimal or something. 20mm wheels are much more reasonable!

      That's a really nice setup you've got there, and yeah I can understand why you're doing what you've done. Wish I could help you more!

      I will say that if you're going for a PI controller, I don't know why you wouldn't go for a full PID, but that's totally up to you. I can understand not wanting to tune that last constant.

      That sounds like a really neat project! Good luck with it!


      6 years ago on Introduction

      Great project. I always like to read about other people's motor applications.

      One observation, though. You didn't mention having problems with this in your article, but I've done a few projects with these inexpensive motors, and, without doing something to reduce it, I almost always have problems with (electronic) motor noise. The symptoms of the problem are that the board resets or freezes when the motors are at high RPMs.

      The solution is to put at least one, or as many as 3, tiny capacitors across the terminals for each motor. Search the internet for "reduce motor noise", or just look at this post that I found:

      It is possible that the DP32 has a higher tolerance for noise than the Arduino boards I usually use.

      If you've found some other way to solve this, I'd be interested in hearing about it.


      Reply 6 years ago on Introduction

      I had a similar problem of the board freezing or resetting. In the end it appeared that when the mors draw a lot of current (in my case up to 2 x 800mA) the voltage drop over the batteries resulted in the reset of the board. I used 6 LR6 batteries to reach to 9volts and each has an internal resistance of up to 150 - 200mOhm. with 1.6 Amps max the effective battery voltage dropped belo 4.8 volts and reset the board.


      Reply 6 years ago on Introduction

      Y'know that's a good point. I have run into the practice of using capacitors to smooth out motor noise before but I hadn't considered that for this project. Looks like I'm going to have to do a Motor Controller 3 in the distant future!


      6 years ago on Step 2

      True, you don't need the pulldown resistor. But what you DO need is a resistor in series with the base (i.e. from Pin 12 or 13 to the base). The base of a transistor is like a diode: once you get above about 0.7 volts it will just take all the current you give it. The data sheet for the processor on the DP32 specifies a maximum output current of 15mA. If you take (3.3V - 0.7V)/15mA you get a series resistance of 173 ohms. Based on another part of the data sheet, the output pin could have as much as 200 ohms of internal resistance (which is what has kept you from frying your chip so far) but I'd still suggest at least 150 ohms in series, just so you have some control over how much current the microcontroller pins are delivering.

      Nice instructable, by the way. You've broken everything down with lots of good details.


      Reply 6 years ago on Step 2

      Hi, you are right about using a resistor for limiting current sourced by microcontroller output, but you have to be careful on selecting the resistor value in order not to getting the transistor out of saturation zone (where it works as a switch) and make it an current amplifier (linear zone).

      You have to take into account transistor's hfe (dc current gain) and the current drawn by the motor and make calculations accordingly.



      Reply 6 years ago on Step 2

      Hrrm... That's interesting. I've never heard about this before. I'll have to play around with that a bit and talk with the folks at Digilent and see what they think. Thanks for pointing it out!


      6 years ago on Introduction

      Nice instructables ;) I really like how you explain everything very clearly!