07 - A Practical Example

So here is my experience: from purchasing to driving....

1 - Understanding what you’ve bought or are going to buy

 

First of all let me preface all of this by saying that I pretty much bought a motor at random (very cheap on eBay) so I’m not suggesting that this is a suitable motor for your project.

 

So here is what I purchased (at around $11 for two)

http://uk.rs-online.com/web/search/searchBrowseAction.html?method=searchProducts&searchTerm=351-4631#header

 

This page gives links to the following two datasheets:-

http://docs-europe.electrocomponents.com/webdocs/0030/0900766b800305e5.pdf

http://docs-europe.electrocomponents.com/webdocs/001b/0900766b8001b258.pdf

 

 

which are less than informative. My motors arrived with no wiring/plugs etc so the colour charts were less than useful – at which I point I wondered if I would ever make them work!!

 

However:- this is what I could learn from the datasheets:-

  • moves 7.5 degrees per step (ie  48 steps per revolution on the motor shaft)
  • has 6 wires
  • is a uni-polar motor
  • has a 50:1 gearbox (so 50x48 or 2400 steps per revolution on the output shaft)
  • holding torque = 100 Ncm
  • working torque = 100 Ncm
  • typical working torque = 65 Ncm
  • Step rate at typical working torque=300Hz
  • requires a 5V supply
  • Resistance = 9.1 ohms per phase
  • 550 mA per phase

 

So long as you have two of the last three parameters then you can calculate the other using the equation Volts =  Amps x Ohms

 

 

There is one potentially important statistic missing from these datasheets that can be calculated from the data given. If we can issue a maximum of 300 steps per second, (300Hz), and – due to the gearbox – we need to issue 2400 steps for a full revolution then the fastest we can turn the output shaft is (2400/300)  ie once every 8 seconds.  This may be rather slow for an output shaft if it is connected to wheels but could be fine if we are rotating the turret of a tank. So this is an important fact to ascertain before purchasing your motor.

 

So what have we learned?  The gearing, steps per revolution and the maximum frequency produce trade-offs between torque and speed. Whether the motor is uni-polar or bi-polar can also influence the required current draw, and torque, as discussed earlier.

2 - Connections to the motor

 

Assuming that the motors are suitable for the job then let’s move on. My motors have 6 pins and are uni-polar so I can use an ohmmeter to work out which 3 pins correspond to coil 1 and which 3 are for coil 2 as described earlier. I can also work out, for each coil, which lead is the centre tap but cannot decide which is one end of the coil versus the other (ie cannot tell which is 1a and which is 1b).

 

I’m guessing that if I get this wrong then all that will happen is that the motor my move in the wrong direction. Using a ‘breadboard’ should help me find out the correct connection. So I wired each coil of my motor to a plug with the centre tap in the middle and the other two wires connected to the outside. This means I can rotate the whole thing through 180 degrees if I’ve got it the wrong way around! The plug I’ve used is polarised so that once I know the correct connection then I can solder the matching header onto my circuit the correct way around. In order to connect my plug to the breadboard I made up a short length of 3 way wire with a male header at each end – which will come in handy in the future for connecting sensors, servos, etc to the breadboard.

 

 

So here’s a picture of my motor, with wires, plug and the cable to link it to the breadboard:-

 

 

3 - Wiring the motor driver

 

Since I’ve bought a uni-polar motor then I need two ‘switches’ per coil – as per my diagram earlier. However: I will forget about the center tap for now and just drive it as if it was a bi-polar motor. So I’ve chosen to use a L293D chip see http://focus.ti.com/docs/prod/folders/print/l293d.html

 

This contains four half H-Bridge ‘switches’ – so one chip can drive both coils on the same motor. One chip per motor.

 

I’m not going to use the ‘enable’ pins on the L293D so I’ve just connected them to pin 16 (the +5v logic supply). Each of the four input pins just maps to the equivalent (higher current) output pin.

 

Check the datasheet to make sure that the L293D can provide enough current for the motor you are using. NB if you need more current then the L293 supplies twice as much but you MUST then connect external diodes as shown in the datasheets. If that still isn’t enough then search this site, or Google, for other higher current H-Bridge circuits.

 

Here's how I connected the L293D

 

 

4 - Connecting the microcontroller

 

I’ve chosen to use my Baby Orangutan board because it can plug straight into the breadboard. This board uses an AVR ATMega168. If you want to use your $50 Robot Board then that is fine as well:- just make sure that your $50 Robot ground and +5v are also connected to the breadboard to drive pin 16 of the L293D. You can do this by connecting a lead from one of your unused analogue-to-digital ports and connecting the ground wire to the ground of your breadboard, and the +5v to pin 16 of your L293D. Obviously the 3rd pin of the connector (which would normally be used for the sensor input voltage) will be left un-connected.

 

Now we need to choose 4 output pins from the micro-controller that we will use to run the L293D.  I selected:-

Port C5 = 1a

Port C4 = 1b

Port C3 = 2a

Port C2 = 2b

5 - Check the first piece of code using LEDs

 

Before connecting the motor – I wanted to make sure that my code was, at the very least, doing what I thought it was. Just connecting the motor only to find out that nothing happens means you have to work out if there is a problem in your program, your hardware connections or, dare I say, if you’ve managed to fry some of the hardware. What better to give a clue as to what’s going on then our trusted friend the LED!!

 

So with the motor disconnected: so place a 1.6k resistor in series with an LED - which is then connected to ground -  on each of the output pins from the L293D - that is pins 3, 6, 11 and 14 - which we will call outputs 1a, 1b, 2a and 2b respectively.

 

 

The easiest sample code to write is for the ‘Wave Drive’, as discussed previously, which requires us to send pulses as follows:

 

 

T

T + 1

T +2

T + 3

1a

High

Low

Low

Low

1b

Low

Low

High

Low

2a

Low

High

Low

Low

2b

Low

Low

Low

High

 

So here’s some pseudo code:-

 

Set your pins as outputs

 

While(1){

     Pin1a = high, Pin1b = low, Pin2a = low, Pin2b=low

     Wait 1 second

     Pin1a = low, Pin1b = low, Pin2a = high, Pin2b=low

     Wait 1 second

     Pin1a = low, Pin1b = high, Pin2a = low, Pin2b=low

     Wait 1 second

     Pin1a = low, Pin1b = low, Pin2a = low, Pin2b=high

     Wait 1 second

}

 

This should make the LEDs flash in sequence: 1a, 2a, 1b, 2b.

 

Looking at my timing tables for the other two potential driving methods you should easily be able to adapt your test code to the other methods.

 

6 - Connecting the motor

 

Now that we have the LEDs flashing correctly we can connect the motor instead.

 

For each coil of the motor the centre tap is unused (as we are simulating a bipolar motor) and the ‘a’ and ‘b’ terminals are connected to the outputs of the L293D driver (instead of the LEDs).

We can now change our program so that instead of waiting 1 second between pulses we can send faster pulses. My motor has a frequency of 300Hz – so one pulse every 3.3ms. However: my timer only lets me wait for a given number of milliseconds so I shall round that up to waiting for 4ms.

Now turn on – and your motor should rotate. If it doesn’t then maybe you’ve got the frequency wrong. For example: if I try to ‘over-clock’ my motor by changing my delays to 3ms then it just hums and doesn’t rotate.

Increasing your delay time will obviously make your motor turn slower.

 

 

But what about reverse? Well if you’ve followed my wiring convention then you can rotate one of the motor connectors through 180 degrees to swap the a and b connections. That should work. Interestingly: I started by swapping the leads round for both coils – and it made no difference – so only reverse one of them. You could, of course, add additional circuitry to do this for you, but this would need an additional controller pin to select forward or reverse and, in my view, this is better done in software:-

Set your pins as outputs

While(1){

Pin1a = low, Pin1b = high, Pin2a = low, Pin2b=low

Wait 1 second

Pin1a = low, Pin1b = low, Pin2a = high, Pin2b=low

Wait 1 second

Pin1a = high, Pin1b = low, Pin2a = low, Pin2b=low

Wait 1 second

Pin1a = low, Pin1b = low, Pin2a = low, Pin2b=high

Wait 1 second

}

7 - Adapting for your motor

 

I encourage you to try out the other two driving methods: Full Step and Half Step. If you a meter then check out current consumption, see if the L293D gets hot (in which case you might need a heat sink), and look at other ‘Calculators’ on this site to estimate how much torque you might need.

 

My set up requires four output pins on the controller for each motor – which is a bit excessive. This can be halved by using ‘Full Step’ drive/ Looking at the table for this mode

 

T

T + 1

T +2

T + 3

1a

High

High

Low

Low

1b

Low

Low

High

High

2a

Low

High

High

Low

2b

High

Low

Low

High

you will see that the ‘a’ pins are always the opposite of the ‘b’ pins. So if the additional current usage of this configuration is acceptable, or is necessary in order to reduce the controller pins, then you can get away with only having controller pins for 1a and 2a. Then use a TTL inverter chip (they cost next to nothing and come in a package with 6 inverters on one chip) to connect 1a to 1b, and 2a to 2b - you will see an example diagram for this in the datasheet for the L293D I listed earlier.

 

 

8 - But what if you want to use the center tap of a uni-polar motor

If you want to use your uni-polar as a 'true' uni-polar motor then you need to use another hardware driver. The L293D contains 'half H Bridges' but what you actually need is effectively' a quarter'  - known as a 'Driver Array'. The ULN2003AN is a good choice. With the center tap connect to +ve supply you can make the current flow in the coil by connect the 'a' terminal of the coil to ground. To make it flow the other way then connect 'b' to ground. If both 'a' and 'b' then no current flows through the coil, and if both are low then the current flows both ways - which effectively cancels each other out. So we can do this by connecting one driver to the 'a' terminal and another to the 'b' terminal. So two drivers per coil makes four per motor. The ULN2003AN contains 7 drivers - so not quite enough to drive two motors from the one chip!!