Wednesday, January 29, 2014

44. Robotics - A Light Follower with Two Servos

So we're still on the theme of motors, and I still haven't revealed why, only to do a bit on DC motors, and now, servos.  I thought I would re-create the Arduino Solar Tracker by Geo Bruce.  See http://www.instructables.com/id/Arduino-Solar-Tracker/ - it's a great little project, and quite ambitious, but I wanted to make something with servos.

I was so excited about getting it working, that I just had to do a blog post, in case something blew up before I had a chance to document it.  I still have a lot of tweaking and adjusting to do to get it to be just right, but it works fine - I just have to make some of the connections more secure, and play with the trimmer potentiometers which adjust the sensitivity.  I'll see how it gets on at night before doing too much.

Here is my version:
 
The white object on the left is the 'eye', mounted on the black pan-and-tilt bracket.  The trusty Arduino is in the middle, and the mini breadboard is on the right, bearing four 10kΩ  resistors, and 2 (blue) adjustable trimmer 10kΩ potentiometers.  Actually, I later changed the 10kΩ resistors to 1kΩ resistors to improve sensitivity.

 
Here's a closer look at the Arduino and mini breadboard - you can see the 2 trimmers and the 4 resistors.
Here's a closer look at the 'eye' mounted on the pan-and-tilt bracket.  The blue object just below is the 'vertical' servo - the one that makes the assembly tilt.  

You can see into the 'eye' here - there are 4 segments in the white cylindrical surround, formed by two interlocking pieces of black plastic.  You can just about see the 4 light dependent resistors (LDRs), one in each segment.  The idea is that they're somewhat isolated from one another, the incident light being seen brighter by some of them than others.  Their signals sent back to the Arduino then tell the microprocessor how to move the servos until the 'eye' is staring straight at the light, that is, each LDR is seeing the same amount of light.

You can see the blue 'horizontal' servo behind.  This makes the assembly, including the other servo, pan (rotate horizontally).  The two servos are identical.  Here's what it looks like from a short distance:




The base is a dummy CD and the upright post hot-glued to it is a medical supplies tube, into which the horizontal servo fits snugly.  The 'eye' was made from a drying capsule from the inside of a medical supplies tube lid, the interior separators made from a piece of black plastic which I cut from a highlighter pen. A 2 cm diameter piece of clear plastic forms a window which also retains the black plastic separators.

And here it is - working!!!
I have it on continuously, on a very sensitive setting, and it's constantly moving and twitching, reacting to the changing light conditions.  It's like a living thing, and seems to have a character all on it's own!  Surely this is truly a robot?

I had to get a few extra parts to make this project - 2 servos, some light dependant resistors, and a pan-and-tilt bracket.  I also had to design an 'eye' for the system.

The servos are Hextronik HXT900 9g/ 1.6kg/0.12 Micro Servos which I got delivered from eBay for £7.16.  They weigh about 9g each, the torque is 1.6 kg-cm and the speed is 0.12 seconds per 60 degrees, all at 4.8V.  

The LDRs were £3.60 for 10 from CPC, one of which I measured as 120 kΩ in the dark and 3kΩ  with bright light shining on it.  The 10kΩ resistors (currently 1kΩ) in series are needed to limit the current drawn by the LDRs.

The pan-and-tilt bracket came from RelChron for £5.60 plus £3.00 p&p.  Here it is assembled, with two micro servos very similar to mine, 'horizontal' (pan) at the bottom, and 'vertical' (tilt) at the top:
Here's the circuit:

and here's the code:
1:  #include <Servo.h> // include Servo library   
2:  Servo horizontal; // horizontal servo  
3:  int servoh = 90;   // stand horizontal servo  
4:  Servo vertical;  // vertical servo   
5:  int servov = 90;   // stand vertical servo  
6:  // LDR pin connections  
7:  // name = analogpin;  
8:  int ldrlt = 0; //LDR top left  
9:  int ldrrt = 1; //LDR top rigt  
10:  int ldrld = 2; //LDR down left  
11:  int ldrrd = 3; //ldr down rigt  
12:  void setup()  
13:  {  
14:   Serial.begin(9600);  
15:  // servo connections  
16:  // name.attacht(pin);  
17:   horizontal.attach(9);   
18:   vertical.attach(10);  
19:  }  
20:  void loop()   
21:  {  
22:   int lt = analogRead(ldrlt); // top left  
23:   int rt = analogRead(ldrrt); // top right  
24:   int ld = analogRead(ldrld); // down left  
25:   int rd = analogRead(ldrrd); // down rigt  
26:   int dtime = analogRead(4)/20; // read potentiometers   
27:   int tol = analogRead(5)/4;  
28:   int avt = (lt + rt) / 2; // average value top  
29:   int avd = (ld + rd) / 2; // average value down  
30:   int avl = (lt + ld) / 2; // average value left  
31:   int avr = (rt + rd) / 2; // average value right  
32:   int dvert = avt - avd; // check the diffirence of up and down  
33:   int dhoriz = avl - avr;// check the diffirence og left and rigt  
34:   if (-1*tol > dvert || dvert > tol) // check if the diffirence is in the tolerance else change vertical angle  
35:   {  
36:   if (avt > avd)  
37:   {  
38:    servov = ++servov;  
39:     if (servov > 180)   
40:     {   
41:     servov = 180;  
42:     }  
43:   }  
44:   else if (avt < avd)  
45:   {  
46:    servov= --servov;  
47:    if (servov < 0)  
48:   {  
49:    servov = 0;  
50:   }  
51:   }  
52:   vertical.write(servov);  
53:   }  
54:   if (-1*tol > dhoriz || dhoriz > tol) // check if the diffirence is in the tolerance else change horizontal angle  
55:   {  
56:   if (avl > avr)  
57:   {  
58:    servoh = --servoh;  
59:    if (servoh < 0)  
60:    {  
61:    servoh = 0;  
62:    }  
63:   }  
64:   else if (avl < avr)  
65:   {  
66:    servoh = ++servoh;  
67:     if (servoh > 180)  
68:     {  
69:     servoh = 180;  
70:     }  
71:   }  
72:   else if (avl = avr)  
73:   {  
74:    // nothing  
75:   }  
76:   horizontal.write(servoh);  
77:   }  
78:    delay(dtime);   
79:  }  

This is the code and circuitry of Geo Bruce at the link given above.  A nice project Geo!





Tuesday, January 28, 2014

43. Full Control of a DC Motor using an H-Bridge

9000 page views!!
So far we have been able to drive a DC motor under software control from the Arduino.  We saw that precautions have to be made to protect the Arduino from 

  • too much power being drawn by the motor and
  • the possibility that inductive 'kick-back' from the DC motor can send a destructive negative spike through the circuit

and that smoothing by a capacitor is desirable because of the motor's 'noisy' power demand. 

We made use of a transistor as a fairly high-frequency (50Hz) switch, turning it on and off using software, in such a way that the 9V battery potential was applied as 9V power pulses with a pre-defined 'duty cycle'.  This made the motor behave as if the applied voltage was varying from maximum to minimum.  This of course was making use of pulse-width modulation (PWM).

However, we did not have full control over the motor, as reversing its direction would have entailed physically swapping over the motor wires.

Suppose we wanted to be able to turn a potentiometer knob (or trimmer screw) and control the DC motor so that turning the screw clockwise gradually increases the clockwise rotation speed of the rotor, and then turning the screw anti-clockwise gradually slows down the clockwise rotational speed, stops the rotor, and then gradually increases the anti-clockwise rotational speed.  By turning the trimmer to a central position, the rotor could be halted, and turning it to the left would cause anti-clockwise rotation, while turning to the right would cause clockwise rotation.  Further trimming to the left or right would increase the motor's spinning speed in that direction.

To achieve this we would need a switching arrangement something like this:



so that by arranging four switches, top left (TL), top right (TR), bottom left (BL) and bottom right (BR), the current can be allowed to flow left-to-right through the motor, or right-to-left.

If TL and BR are closed, while the other two diagonal switches are open, the current flows as indicated in the top diagram, let's say clockwise (cw).  If the alternative diagonal combination of switches is used (TL & BR open, TR & BL closed), the motor turns counter-clockwise (ccw).

Luckily, it's possible to obtain an integrated circuit (IC) which has these properties - the H-bridge.  It's called this because of the H-like arrangement of the integrated transistor switches.  One example of the H-bridge is the Texas Instruments L293DNE Quadruple Half-H Driver.  This 2-cm long, 16-legged IC has more circuitry than we need.  We require a pair of Half-H circuits for our switching requirements, but in fact the chip has twice this capability - it can independently control 2 DC motors at the same time, each motor being allowed to draw up to 600 mA at up to 36V!  (Remember that my current DC motor draws up to 350 mA at 8.2V).  

So here we will use half of its capability.

Here is the Quadruple Half-H Driver:


The chip has built-in thermal shutdown to protect against a short circuit - for example, if TL and BL were closed at the same time, or if TR and BR were closed at the same time.  These conditions would allow an un-impeded current to flow from the positive to the negative terminals of the 9V battery.  This would be very undesirable, as even 9V batteries when short-circuited, can release a lot of energy, and even explode in the process.

However, it is never good practice to rely on the built-in thermal shutdown, so the switches are disabled before changing their state between on and off.  For example, the switch between motor directions needs to be disabled because a momentary short circuit is just possible.  This is why there are 3 control connections - one for controlling switches 1 and 2, one for controlling switches 3 and 4, and one to enable the circuit.


The table above summarises the functioning of each half-H driver.  So, for each of the 1, 2, 3 and 4 half-H gates, if input A is set HIGH, the output Y is HIGH, but only if EN (enable) is set HIGH.  If input A is set LOW, then output Y is LOW, but again only if EN is HIGH.  Otherwise, if EN is LOW, then, regardless of the state of input A, the output Y will be OFF.

So the connections are as follows:

We are going to use the chip's gate 1 (chip pin numbers 1, 2 & 3and gate 2 (chip pin numbers 6, 7 & 8) to represent the (TL & BR) pair and the (BL & TR) pair of switches.

On the L293DNE:

Pin 1 (1,2EN) is used to enable and disable the driver on the left of the chip and is connected to the Arduino's Pin 9

Pins 2 (1A) and 7 (2A) are connected to Arduino Pin 3 and 2 respectively, and control the state of the switches 1 and 2.

Pins 3 (1Y)  and 6 (2Y) are the outputs from the chip's left hand driver's switches 1 and 2.

Pins 4512 and 13 (Heat sink and Ground) are connected to the common GND for both the 9V and Arduino's 5V supplies.

Pin 8 (VCC2) supplies the motor current, and is connected to the 9V battery's positive terminal.

Pin 9 is the enable for the IC's right hand driver, which is not used here, so it is best to connect it to GND.

Pins 10, 11, 14 and 15 do not need to be connected because they are for the chip's right hand driver which we're not using here.

Pin 16 (VCC1) supplies the chip's power, and is connected to the Arduino's 5V supply.

Here's the mug-shot:


And here it is working:


The trimmer I'm tweaking with a screwdriver is the blue component at the top right of the picture.  As I turn one way, the motor rotates faster and faster in one direction and then as I turn the other way, the rotation slows down to a halt and then starts to accelerate in the opposite direction.  Pretty good - eh?

Note that the role of the transistor, capacitor and diode has been taken over by the H-bridge, making a relatively neat circuit on the breadboard.  The battery box now has it's cover on and the mini breadboard and DC motor have been mounted on top of it with the good old ubiquitous rubber bands!

For completeness, here's the software, again attributable to Jeremy Blum from his excellent recent book Exploring Arduino - Tools and Techniques for Engineering Wizardry:

1:  /*  
2:  Exploring Arduino - Code Listing 4-3: H-Bridge Potentiometer Speed Control  
3:  http://www.exploringarduino.com/content/ch4  
4:    
5:  Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com )  
6:  This program is free software: you can redistribute it and/or modify  
7:  it under the terms of the GNU General Public License v3 as published by  
8:  the Free Software Foundation.  
9:  */  
10:    
11:  //Hbridge Motor Control  
12:  const int EN=9;  //Half Bridge 1 Enable  
13:  const int MC1=3; //Motor Control 1  
14:  const int MC2=2; //Motor Control 2  
15:  const int POT=0; //POT on Analog Pin 0  
16:    
17:  int val = 0;   //for storing the reading from the POT  
18:  int velocity = 0; //For storing the desired velocity (from 0-255)  
19:    
20:  void setup()  
21:  {  
22:    pinMode(EN, OUTPUT);  
23:    pinMode(MC1, OUTPUT);  
24:    pinMode(MC2, OUTPUT);  
25:    brake(); //Initialize with motor stopped  
26:  }  
27:    
28:  void loop()  
29:  {  
30:    val = analogRead(POT);  
31:     
32:    //go forward  
33:    if (val > 562)  
34:    {  
35:      velocity = map(val, 563, 1023, 0, 255);  
36:      forward(velocity);  
37:    }  
38:     
39:    //go backward  
40:    else if (val < 462)  
41:    {  
42:      velocity = map(val, 461, 0, 0, 255);  
43:      reverse(velocity);  
44:    }  
45:     
46:    //brake  
47:    else  
48:    {  
49:      brake();  
50:    }  
51:  }  
52:    
53:  //Motor goes forward at given rate (from 0-255)  
54:  void forward (int rate)  
55:  {  
56:    digitalWrite(EN, LOW);  
57:    digitalWrite(MC1, HIGH);  
58:    digitalWrite(MC2, LOW);  
59:    analogWrite(EN, rate);  
60:  }  
61:    
62:  //Motor goes backward at given rate (from 0-255)  
63:  void reverse (int rate)  
64:  {  
65:    digitalWrite(EN, LOW);  
66:    digitalWrite(MC1, LOW);  
67:    digitalWrite(MC2, HIGH);  
68:    analogWrite(EN, rate);  
69:  }  
70:    
71:  //Stops motor  
72:  void brake ()  
73:  {  
74:    digitalWrite(EN, LOW);  
75:    digitalWrite(MC1, LOW);  
76:    digitalWrite(MC2, LOW);  
77:    digitalWrite(EN, HIGH);  
78:  }  
79:    

So looking at the code above, 
  • Arduino Digital pin 9 is defined as the half-bridge 1 enable pin, 
  • Digital pins 3 and 2 are connected to motor control 1 and 2 respectively, and 
  • the potentiometer's wiper is connected to Analog Pin 0

The void setup() function sets the digital pins as outputs and immediately invokes the brake() function.  We'll come back shortly to the brake() function.

There are some neat functions included:
brake(), forward() and reverse().

To see how these work, let's look at the following diagram:



We can think of diagonal TR to BL as switch 1A on the chip, and diagonal TL to BR as switch 2A.  
In diagram 1, all switches are open, ie nothing is enabled.
Look at the diagram 2:  
Forward: 1A is HIGH (switches closed) and 2A is LOW (switches open).  In diagram 3, 
Backward: 1A is LOW and 2A is HIGH
Now in diagram 4:
1A is LOW and 2A is also LOW

For each function, care is taken to disable before setting switches, followed by an enable:


53:  //Motor goes forward at given rate (from 0-255)  
54:  void forward (int rate)  
55:  {  
56:    digitalWrite(EN, LOW);  
57:    digitalWrite(MC1, HIGH);  
58:    digitalWrite(MC2, LOW);  
59:    analogWrite(EN, rate);  
60:  }  


62:  //Motor goes backward at given rate (from 0-255)  
63:  void reverse (int rate)  
64:  {  
65:    digitalWrite(EN, LOW);  
66:    digitalWrite(MC1, LOW);  
67:    digitalWrite(MC2, HIGH);  
68:    analogWrite(EN, rate);  
69:  }  


71:  //Stops motor  
72:  void brake ()  
73:  {  
74:    digitalWrite(EN, LOW);  
75:    digitalWrite(MC1, LOW);  
76:    digitalWrite(MC2, LOW);  
77:    digitalWrite(EN, HIGH);  
78:  } 

In the cases of forward and reverse, the degree of enablement, (EN, rate) and hence the speed, is dictated according to the value of rate.  The value of rate has been received from Analog pin 0 as follows:


30:    val = analogRead(POT);

This is making use of PWM as it is an analogWrite to a PWM-enabled pin on the Arduino.

The part of the program that deciphers the potentiometer's variable resistance is in the loop part:


28:  void loop()  
29:  {  
30:    val = analogRead(POT);  
31:     
32:    //go forward  
33:    if (val > 562)  
34:    {  
35:      velocity = map(val, 563, 1023, 0, 255);  
36:      forward(velocity);  
37:    }  
38:     
39:    //go backward  
40:    else if (val < 462)  
41:    {  
42:      velocity = map(val, 461, 0, 0, 255);  
43:      reverse(velocity);  
44:    }  
45:     
46:    //brake  
47:    else  
48:    {  
49:      brake();  
50:    }  
51:  }  

The value val of the potentiometer position is read in as an analogRead().  The analogue values read in in this way are converted into digital values between 0 and 1023.  Then the map() function is used to scale val's range to the range 0 to 255:
We have taken the pot's values between 0 and 1023, and divided this range into 3 areas - 0 to 461 is mapped to the range 0 to 255.  563 to 1023 is mapped to 0 to 255, and the region in-between is the braking ('Stopped') region.  

These mapped values are then used in the forward(), reverse() or brake() functions as required.

Isn't that marvellous?!!

Thursday, January 23, 2014

42. A DC Motor Driven by Pulse-Width Modulation

Having looked at how DC motors work in the last blog post, now let's look at a DC motor for real.  I've had this little motor for some time now, and this is the first time I've had time to get it going.

Here it is running:


Here's a closer look at the motor:

It's based on the following wiring diagram:

by Jeremy Blum, from his excellent book, Exploring Arduino - Tools and Technuques for Engineering Wizardry.

Here is the circuit:


The code is as follows:

1:  /*  
2:  Exploring Arduino - Code Listing 4-1: Automatic Speed Control  
3:  http://www.exploringarduino.com/content/ch4  
4:    
5:  Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com )  
6:  This program is free software: you can redistribute it and/or modify  
7:  it under the terms of the GNU General Public License v3 as published by  
8:  the Free Software Foundation.  
9:  */  
10:    
11:  //Simple Motor Speed Control Program  
12:    
13:  const int MOTOR=9;  //Motor on Digital Pin 9  
14:    
15:  void setup()  
16:  {  
17:    pinMode (MOTOR, OUTPUT);  
18:  }  
19:    
20:  void loop()  
21:  {  
22:    for (int i=0; i<256; i++)  
23:    {  
24:      analogWrite(MOTOR, i);  
25:      delay(10);  
26:    }  
27:    delay(2000);  
28:    for (int i=255; i>=0; i--)  
29:    {  
30:      analogWrite(MOTOR, i);  
31:      delay(10);  
32:    }  
33:    delay(2000);  
34:  }  


There are a number of points I would like to make about this project:
  • There is a separate power source (a pack of 6 x AA rechargeable batteries giving almost 9V - VCC on the circuit diagram), specially for the motor part of the circuit, in addition to the Arduino's individual DC supply. This is because of the relatively large amount of power demanded by this little motor.  I measured it at 8.2V x 350mA = 2.9W (watts), some of which is used for heating up the transistor.  This amount of power is beyond what the Arduino can supply (3.3V x 50mA = 0.165W).
  • The jerky nature of the power (noise) used by the motor needs to be smoothed by a capacitor, placed in parallel with the motor terminals.  Because it takes time to charge and discharge in parallel with the motor, it smooths out the current through the transistor, thus removing much of the noise. Be careful when using an electrolytic capacitor as the polarity is important.
  • One thing I didn't say about DC motors, is that they are also electricity generators. Moving a wire (or a coil as in the DC motor) within a magnetic field, actually induces an electric current in the wire.  This spells danger for the circuitry if the rotor should ever get rotated mechanically and cause a voltage spike to be transmitted through the circuit.  Hence the diode - in anti-parallel with the motor.  Once again watch out for the polarity of the diode.
  • The PN2222A transistor is an NPN bipolar junction transistor (BJT) used in this case as a switch - when a voltage is applied from the Arduino Pin 9 through the 1kΩ base resistor to the transistor's base, this allows current to flow from the 9V battery power supply, through the motor, into the collector and out of the emitter to ground.  The 1kΩ base resistor limits the overall current through the transistor, to protect it.
  • The transistor is capable of applying this voltage in a pulsed waveform, so that the power to the motor is also pulsed.  The pulses to the motor are all 9V pulses, but by varying the length (on the time-base) of the pulses, the motor 'sees' these as average voltages, ranging in this case, from zero to maximum - ie 9V.  As we saw before, this is known as Puse-Width Modulation (PWM).
  • As there is a relatively large amount of power passing through this little transistor, it gets HOT!  Don't test this with your finger because you'll get the characteristic 'D' shaped burn on your skin! ;-D
  • The code uses analogWrite() to send 5V pulses to the motor pin - Pin 9 - which is capable of PWM (it is labelled on the Arduino with a ~ before the label '9').  Using the index i, inside a for loop, (lines 22 to 26 and 28 to 32 on the sketch above) the width of the pulse (duty cycle) is increased every 10 ms by 1/256 th of the maximum pulse width, until the duty cycle is at 100%.  In the first for loop, the speed of the motor is 'ramped up' from rest to maximum, in 10 ms steps, held for 2 seconds, and ramped down to zero by the second for loop, held at rest for 2 seconds, and the cycle repeats continuously.
  • The direction of rotation of the motor is the same throughout, eg forward only.  If we want to be able to reverse as well as go forward, we need some further tricks.......

Sunday, January 19, 2014

41. Let’s Get into Motion – with Motors

Firstly we’ll have a little physics lesson.  Moving electrical charge and magnetic fields interact with each other.  All conductors are full of charged particles (free electrons) which, when moving in an electric current, generate an electro-magnetic field.  

This field presents itself in two components, described as electrostatic and magnetic fields.  The electrostatic field surrounds each charged particle (electron) and the magnetic field wraps around the wire as shown below:
Thanks to http://physicsed.buffalostate.edu/SeatExpts/resource/rhr/rhr.htm for the images.

Magnetic fields have direction, for example from the N pole of one magnet to the S pole of another magnet.  The direction of the magnetic field around the wire with a current I flowing, with velocity v, is as shown above where the field is represented as a series of concentric lines. In fact there are no lines, just a steadily decreasing strength, B, as you move further from the wire (ie as the radius r increases).  

In a straight wire with a current flowing (don’t forget that the conventional current is said to flow in the direction opposite to the flow of electrons), the direction of the magnetic field generated can be determined as above from the right-hand rule, where the right hand’s thumb represents the direction of conventional current, and the fingers are pointing towards the magnetic field's direction.  

If that current-carrying wire is now placed in a strong magnetic field, between the N and S poles of a permanent magnet, the wire’s magnetic field interacts with the permanent magnet’s magnetic field (like poles repel and opposite poles attract) so that there is a resulting force which will be big enough to move the wire: 

Thanks to http://hyperphysics.phy-astr.gsu.edu/hbase/magnetic/motdc.html#c1 for the image.

If the current-carrying wire and the strong magnetic field are arranged as in the above diagram, the movement can be harnessed as rotary motion, and we have a simple motor.  Notice that the commutator is split into two halves and the direct current is applied by brushes in contact on either side.  By the time the part of the rotating wire which is experiencing an upward force, has moved through 180 degrees, the current in the wire loop will have reversed, and the other magnetic pole will exert a downward force, enabling continued rotation.  The direction of the force on the wire can be determined using the left hand rule:
Thanks to http://www.magnet.fsu.edu/education/tutorials/java/handrules/ for the image.
DC Motors
Direct Current motors as described above have two wires, power and GND, and they rotate continuously (quite fast) as long as power is supplied.  Their speed in revolutions per minute (rpm) is high, as in computer cooling fans, or the wheels of model cars.  The speed of rotation can be controlled in an analogue fashion by varying the supply voltage, and also more usually in a digital fashion by pulsating the 5V supply on and off, using Pulse Width Modulation (PWM).  We came across PWM before for controlling the brightness of LEDs. See Post 15. Programming the ATTiny85 with the BreadboArduino 

The PWM of LEDs made use of the eye’s Persistence of Vision (POV) where the jerky flashing isn’t noticed, while the apparent brightness depends on the duty cycle of the PWM.  In the case of DC motors, the jerky motion produced by PWM is not noticed because the motion is smoothed by the angular momentum of the spinning motor/wheel system. 

For example, if the duty cycle is 50 per cent, the speed of rotation will be at half of the maximum available at 100 per cent duty cycle.  Model DC motors used for robotics projects are normally the brushed type and the one shown below has wire coils wound around an iron rotor giving rise to three electromagnets, which exert forces on the permanent magnet’s N and S poles. 

You can see the brushes which make contact with the rotating commutator.  The rotating part is called the rotor and the non-rotating part is called the stator.

Thanks to http://www.southernsoaringclub.org.za/a-BM-motors-1.html for the image.




Servo Motors
Servos are more complicated, but basically consist of a DC motor, a set of gears, a control circuit and a position sensor (ie a potentiometer with rotating wiper).  Here’s one which has been stripped down to show the works:

Thanks to http://induino.blogspot.co.uk/2012/07/induinox-user-guide-interfacing-with.html for the image.

Servos are not usually used for continuous rotation as with the simple DC motor described previously.  The position of the rotor can be precisely controlled and they are used for tasks where the angular position of the rotor needs to be known reliably, for example, the segments of a robot’s arm.  Servos usually have three wires, power, GND and control.  The power is supplied continuously, and the servo’s circuit regulates the current drawn by the load on the motor. 

Servos’ angle of rotation is usually within a restricted range of less than 180 degrees, and is used for forward and backward motion within that range.  The control signal they receive determines what angle the rotor is to turn through.  When the position sensor determines the position reached, the motor stops. 

This control signal is a Pulse Width Modulation signal.  While the rotation speed of a DC motor is controlled by PWM, it is the position in degrees that is dictated by PWM in the servo.  Here are some control pulse sequences used for servos:

Thanks to http://www.electroons.com/electroons/servo_control.html for the image.

A minimum pulse width of 1 millisecond, repeated every 20 ms, keeps the rotor at the zero degrees position.  If the pulse width is increased to 1.5 ms, the rotor rotates to the 90 degree position, and if the pulse width is increased to 2 ms, the rotation would be through 180 degrees.  These pulse widths, repeated every 20 ms, represent duty cycle values of 5 per cent, 7.5 per cent and 10 per cent respectively.  The repeating signal every 20 ms tells the servo to stay in that position until the duty cycle changes.  Servos are quite ‘torquey’  - while the gear train reduces the rotation speed, at the same time it increases the torque.  Torque is a way of describing the ‘strength’ of the servo – how much work it can do, and servos come with a torque rating.

Stepper Motors
Stepper motors are also employed for precision work, but in such things as printers, where many rotations are required, while still maintaining accuracy of position.
A 2-phase unipolar, permanent magnet type stepper motor, with a rotor step angle of 90 degrees.
The direction of rotation can be reversed by reversing the sequences of activating the wires shown.
Thanks to 
http://www.piclist.com/images/www/hobby_elec/e_step1.htm for the animation.
The construction of stepper motors is different from servos.  As can be seen above, they employ multiple ‘toothed’ electromagnets and they are brushless.  An external microcontroller calculates the combination of electromagnets to be energised.  

In the diagram above, the bar magnet rotor gets ‘stuck’ between two of the electromagnet’s teeth – S pole stuck between (attracted to) two N poles, and at the other end, its N pole stuck between two S poles.  When the electromagnets change, the bar magnet's N pole will be repelled or 'kicked' by an N pole (and simultaneously its S pole will be kicked by a S pole) and will be shifted into the next step, and so on in a clockwise rotation.  If the rows of the table above are scanned from bottom to top, the rotation will be anti-clockwise.

The current within the electromagnet coils is alternated in such a way as to perform a rotation of precisely pre-defined steps.  The N poles and S poles of the electromagnets alternate in a sequence which allows continuous rotation until the final position is reached.  The ‘steps’ are represented by small rotations from one electromagnet to the next, and rotations of more than 360 degrees are possible.  Once the required position has been reached, the stepper motor will stay there with a good holding torque.

Stepper motors can be unipolar (6 or 8 wires) or bipolar (4, 6 or 8 wires) in design.  The electronic control of unipolar steppers is not as complicated as that for bipolar stepper motors.  Bipolar steppers usually have more torque.  Unipolar steppers have a centre tap to the coil connections.  Below are some uni-polar and bi-polar stepper motor wiring schemes:


Thanks to http://probotix.com/stepper_motors/unipolar_bipolar/ for the image.

Here endeth the physics lecture!  But why motors?  Just be patient and we'll soon see!