Back to Main Page

A Simple DC Motor Controller

An Experiment For You To Build Upon
New example using BasicX BX-24 Added!

Click MOGOTUT.ZIP A zip file of this article (includes source and projects)
Click MOGOTUT.BAS The PicBasicPro source code
Click BASICXMOGO.ZIP The BASICX project and source code
Click MOGOTUT2.BAS for the BASICX .BAS file only

Motor *Controller*??? Why do we need a fancy motor controller with our robots? Can't we just apply power and let them scoot off chasing the dog or cat? Well ... sure, you can do that. But if you just apply a constant power to each motor on a 'bot, then the poor robot will never be able to maintain a steady speed. It will go slower over carpet, faster over smooth flooring, slower up hill, faster down hill, etc. In fact, it will probably be darn hard to fix it so both motors turn exactly the same speed given any terrain. The usual result is the 'bot does slow curves around the floor because one wheel is getting more power than the other. The other irritating problem with applying fixed power to robot wheels is that it is hard to apply *just a little* power to get the robot to go slow. The wheels only have a limited torque when this is done, and just about anything will stop the robot.

This article explains the basics of how to get your motor to give feedback to a microcontroller and then control the speed of the motor with a good deal of precision (well... good enough for our robots). You can select any speed you want the 'bot to go and it will try to go that speed - even if it runs into difficult terrain. It will apply more power when it senses a slow down and the power will continue to be increased until the wheels turn at the selected speed (or until you run out of battery juice). In fact, it is quite interesting to command the robot to turn at a low rpm and then watch it crawl very slowly across the carpet. If you put your hand on it to stop it, it 'hunkers' down and starts pushing harder until you let it go. It then quickly settles into its slow and straight crawl. You can test this effect with this experiment. Get everything going and then grab the wheel attached to the output shaft. The power will increase until you can't hold it anymore and then, when you let go, it will speed up and slow quickly down to the desired speed. If you are using the BasicX version, you can watch the Duty Cycle increase when you grab the wheel.

When you use the speed of the motor as feedback and add it into your speed adjustment circuit, you are implementing Proportional Feedback. A spinoff of being able to control the motors with this system is that you can keep track of just how far the motors have traveled. You can then use that information to make precision turns and do dead reckoning navigation. Click HERE for a Real Movie of a motor controller that is based on this experiment taking its robot around a relatively precise square.



These are the basic instructions on how to make a simple DC Motor Controller. They are compiled from my own experiences, and from some really great discussions about the topic on the Seattle Robotics Society's mail list. In addition, Bill Ruehl and I collaborated on a more elaborate controller that this experiment is derived from - so Bill needs to be given credit for his work on this. You can contact him at: Bill Ruehl.

The experiment described here is just that - an experiment. It is not a completed robot or motor controller, but it should introduce you to how it all works and then you can build your own. This experiment is meant to get your creative juices flowing so that you can create your personal solution for your robot. These instructions use the following configuration - if you don't have this exact equipment, then you are on your own, but if you read on, you should be able to understand what is happening and apply it to your situation:

    1. 16C73A - PIC microcontroller or BasicX chip. Chosen because they have hardware PWM. Click to find out more about hardware PWM. Go to for documents that describe the 16C73A. also makes a similar chip (AT90S4433). You could use other chips like the Atmel AT90S8515 or PIC 16F87X series. The Atmel series does not use the same code/compiler as the PIC series so the code in this project won't work on them. And, of course, you could use a Motorola MCU - most of which have hardware PWM also. You can pick up PIC chips from Digikey - but you will need a programmer and a UV light source to complete your PIC kit. You might want to get a friend who has all this to help with your first shot at it.


to make it is to use the BasicX BX-24 chip. A very nice implementation and easy to get started. I have included BASICX BX-24 source code for this project. You can get a BasicX chip at BasicX is probably the very least expensive way to do this experiment.

2.      Globe DC Motor - Chosen because it is small and has built-in shaft encoders. It seems to be popular right now on the surplus market and can be gotten from a number of sources. It simplifies the motor feedback. You can use wheel encoders or any other feedback method, but this example uses the Globe Motors for simplicity. See the bottom of this page for some examples of other feedback methods. You can get a Globe motor from:  (note: not there last time I looked - check around on the web) The Globe motor shaft encoder gives 2 pulses per revolution of the motor, which equates to about 240 pulses per revolution of the drive shaft. Note: Since this article was written, the Globe motors have been snapped up by hobbyists. Any gear head motor will work if it has shaft encoders. If the motor has a rear shaft, you can easily add your own encoding scheme (use a really small magnet and a Hall effect sensor - each time the magnet passes the sensor, you get a blip). Another way is to add your own sensor to the back of the motor. See Pittman Hack  for an example.

3.      12 Volt power. You can get a 12 volt Gel Cell from a battery store for less than $20 (1.4 amp hour is fine - cost me $15 retail). You could use a variety of battery packs including one of the RC model car Ni-Cads (7.2 volt). Also, you could just use a 12 - 13 volt bench supply. The Globe motors are rated at 24 volts but seem to like just about anything from a few volts on up. If you use a higher voltage ( > 12 volts), you might have to heat sink the motor control chip and your power supply chip.

4.      Pic Basic Pro or BASICX. Basic was chosen because it is like psuedo code. You can code your project in any language once you know how it all goes together. Basic is a well understood language and it is very easy to read. You can get PBPro from It is somewhat expensive, but makes prototyping very easy. Remember, you don't have to use the language used in this example, you can develop this in assembler (which is free for the PIC, Atmel, and Motorola chips). As a late addition, I also have added the BX-24 (BASICX) source code for this project (see the links at the top and bottom of the page)

5.      A 74HCT04 hex inverter for inverting one channel of the motor drive. The remaining inverters on the chip (you only use one of the six) will be useful if you expand the project to control two motors - or to use it as an inverter to serially communicate to your PC (or both). For a simple block diagram of how the inverter is used, see pulses.html. You can get the inverter chip from Radio Shack or Jameco (

6.      An SN754410 motor controller. You can get them from They are a substitute for the L293D controller. They handle more current and are a bit better (a tad more expensive). Since the Globe motors are a little current hungry, the upgraded chip was used. If you use L293D chips, then be sure to put them on a good heat sink.

7.      A solderless breadboard. A Jameco JE23 is fine (see picture below)

8.      A 7805 voltage regulator - or other 5 volt power source for the PIC and other chips. Get them at Radio Shack or cheaper through Jameco.

9.      A 20 mHz crystal for the PIC (built-in for the BasicX). You can use just about anything, but the 20 mHz gives you a great deal of flexibility if you decide to build on to the project and actually make a useful controller. Again, go to Jameco/Digikey/etc. The BasicX is pretty fast as is, so it should be fine if you want to expand it later.

10.  Various caps and hookup wire. Get them from your junk box or from the Shack.

Limitations Of This Experiment

11.  Proportional feedback only. Proportional is all you need for most small robot applications. However, the other two types of feedback are explained at PID.htm. Refer to this document if you want to implement a more elegant version. See for a more complete implementation example.

12.  Single channel only. This is a tutorial and as such it is probably not functional for anything, but it should show you how to do it all. You should be able to add a second channel (second motor) to this example very easily. You are only a few smoke releases away from a working example! 8-)

13.  Locked anti-phase for the motor controller. Anti-phase is the easiest to hook up. For a discussion of the difference between locked anti-phase and sign magnitude (the two basic types) see National Semiconductor Note AN-694 (AN-694). When using locked anti-phase, the frequency of the PWM should be relatively high so that the motor coils can act as a filter to turn the pulses into an average DC output. Try to run the PWM at 19 kHz or higher when starting and then adjust to see how it works.


The basic diagram of the experiment like this (The PIC version is first and the BasicX version is next):



The layout of the breadboard looks like the picture below (click it for larger picture and comments).

And here is a picture of the experiment all hooked up and ready to go (the motor power is coming from a bench supply that is off screen):



The layout using a BX-24 is almost the same as with a PIC and looks like the picture below (click for larger picture and comments). Read the source code for the BX-24 and study the picture and you should see how it all hooks up to the hex inverter and the SN754410. Simply take the output from one of the PWM pins (pin 26 or 27) of the BX-24 and send it to the inverter. The encoder output is sent to Pin 13 of the BX-24 (you can see the pullup resister in the picture on the back of the BX-24). Any pin to monitor the encoder output could have been used, but my code expects Pin 13. 8-)

Click on Picture To See Close Up of Mod and BX-24

The idea behind the coding for this experiment is to select a feedback rate that equates to the speed you want your motors to go. Then sample the encoder feedback to see if the motor is turning that speed. If it isn't, then increase/decrease the PWM duty cycle until the motor comes into the right speed range. The code (PicBasic Pro or BASICX) to run this experiment is at the top and bottom of this page. If you decide to use another language to do this coding (or you use an Atmel chip) then let me know. I would like to collect a few different examples so folks would be able to pick and choose.

A more complete controller and an example application using wheel encoders is here:

You will definitely want to experiment with different feedback speeds, sampling times and size, PWM frequencies, and gain of your proportional feedback (if your gain is too high, the motor will oscillate). This is actually quite a bit of fun to play with and it will get you a good deal of experience.

What's next??? Well .. with a little thought, you can see that this is not a very complete experiment. You will want to be able to start and stop the motors and tell them to turn a specific way, keep track of how far they have traveled, etc. We'll leave that code for a future article (or for your own experimentation).

Click MOGOTUT.ZIP A zip file of this article (includes source and projects)
Click MOGOTUT.BAS The PicBasicPro source code (commented)
Click BASICXMOGO.ZIP The BASICX project and source code
Click MOGOTUT2.BAS for the BASICX .BAS file only (commented)