Turning DC motors into servos

This project is about positioning DC motors, check out the main project index for the rest.

With a wheeled robot the motors can simply be switched off and on to drive it around. With a legged robot it is slightly more difficult as each joint needs to be positioned accurately.

This article is a brief description of how I am building a control system for Android 5, it will allow accurate positioning of each linear actuator from commands sent on a serial bus.

The basic principle is that each actuator produces a reference voltage relative to it's position. This comes from a sliding potentiometer attached to each actuator used as a potential divider, I made these boards for that purpose in the 'making your own PCBs' article:

This voltage is compared with another reference voltage produced by the control system - this is the 'target' voltage and represents the desired position for the actuator to move to. The two voltages are compared and the motor is moved accordingly until both voltages match. When the difference is zero the motor stops.

The basic principle is quite simple and the circuit looks like this:

R1,2,4,5 - 100K
R3, 7 - 1K
R6 - 100K present (potentiometer)
NOTE: wires only join on the green dots.

This is the first stage of the circuit, it uses two 741 op-amps. The first compares the two voltages (ref1, ref2) and produces an output which is the difference between the two. This circuit needs + and - 12v as well as 0v as the voltage needs to swing +ve in one direction and -ve in the other.

The next 741 is to add some gain - the 100K preset provides a sensitivity control to make the voltage swing larger or smaller.

The next stage of the circuit looks like this:

R1 - 1K

This stage of the circuit switches two relays to control the actual motor. They are driven by two transistors (one each) so if the voltage swing is +ve one turns on, if the swing is -ve the other turns on. The diodes are to protect the transistors from nasty 'back EMF' - this is current that comes out of coils when you remove power from them (make sure the diodes are the right way around or the relays won't switch on at all).

NOTE: any NPN and PNP pair of transistor will pretty much do, low power standard 'small' type in a D shaped case. BUT, make sure the top one is NPN and the bottom is PNP or it won't work.

The motor is wired between the two common connections on the relays, the normally closed connections of the relays are wired to the -ve motor supply, the normally open connections are wired to the +ve motor supply. This means when both relays are off - the motor is off, when one relay comes on it goes one way, when the other is on it goes the other way.

It is important to get the motor wired the correct way round so that the potentiometer on the actuator goes towards the target position and not away from it - otherwise it never gets there.

I have to control eight motors so I needed to make this circuit 8 times. I made 4 assemblies, each containing 2 complete circuits, so 4 op-amps, 4 transistors and 4 relays in each. Again, I etched my own PCBs:

Relay board with transistors to the left, board with op-amps to the right:

And here are the four boards - the two halves of each circuit sit on top of each other:

I have made it quite modular so each of the four assemblies has connectors for the power, reference voltages and actuator potentiometers to plug into. I will use cables with connecters on both ends so that the project the whole system can be moved to another machine in the future:


The control system is now finished as far as the electronics go.

The method for producing the control reference voltages is as follows. The control data will come from as RS232 (serial) signal - this can be from a PC or from a microcontroller. It will then be converted to 8 parallel bits by a 'picaxe' microcontroller. These 8 bits wil then feed a Digital to Analog converter (DAC) which will produce an analog reference voltage:

I will be writing a separate article about the pixace microcontroller, for now go to: www.picaxe.co.uk , there are many datasheets and very helpful forums there. The process of converting RS232 to digital bits is about 3 lines of code...

The circuit for the DAC is:

IC1 : DAC0800
IC2 : LM741 opamp
R1,2,3 : 4K7
C1 : 0.1uF
C2 : 0.01uF
Note: wires only join on the green dots

I have used the DAC0800 which is a National Semiconductor part as they are cheap and easy to get hold of.

So, the output of this circuit goes to one input of the first circuit, the actuator reference voltage goes to the other. This voltage can be programmed by a computer and tells the actuator where to go, when both reference voltages are the same the actuator stops. This means that you can send a number to the controller and it will move the actuator to that position with 8 bit resolution, i.e. 256 steps (from 0 to 255).

I had to control 8 actuators, so I had to build the circuit 8 times. Here's my finished circuit that produces 8 reference voltages from a common serial bus. I built it on stripboard this time due to the large amount of ICs that I had to mount (was easier than drawing PCB tracks by hand):

Parts are as follows:

1 : Power distribution board, takes +/-12V and gives out +5V from the regulator - as the picaxe's only run on +5 V.
2 : eight picaxe18's.
3 & 4 : DACs
5 : eight 741s
6 : this board just has the pin stip on to allow the cables to plug into this section from the above sections of the controller. It provides the eight reference voltages and +/-12V to the other circuits.

Even though it was hectic to build - mainly because I had to get the 8 bits from each picaxe to each DAC - 64 wires in total, it actually works.

Each picaxe listens on the serial bus for a unique identifier 'A', 'B' etc. When it hears the correct letter (A through H) it gets the number that is sent after it and outputs this as 8 binary bits to the 8 output pins. - these feed the DAC to make the reference voltage.

In order to control the actuators you need to write a program on a PC that sends serial data, I will also be writing an article about how to do this under Windows shortly, although any operating system / programming language can be used that supports RS232 outputs.