Google+ Followers

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:

video

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

No comments:

Post a Comment