Sie sind auf Seite 1von 6

Micro crash course/pwm - Wiki

http://wiki.mech.uwa.edu.au/index.php/Micro_crash_course/pwm

Micro crash course/pwm


From Wiki

Contents
1 Drive a motor using a Pulse Width Modulated signal 1.1 PWM 1.2 A small electric motor 1.3 FET transistors 1.3.1 N-channel FET 1.3.2 P-channel FET 1.4 What kind of transistors do I need? 1.5 Driving the small motor with a single FET transistor 1.6 More power please! 1.7 Bidirectional operation (H-bridge circuit) 1.8 A final word about MOSFETs: the problem of low-voltage gate drive

Drive a motor using a Pulse Width Modulated signal


back to main tutorial page The AVR can produce a timed sequence of pulses called PWM and this is a common approach used to control the torque of a DC motor. In this tutorial we will program the AVR to produce a PWM output, and also breadboard some transistor driver circuits. These drivers will deliver enough current to control a small DC motor.

PWM
Pulse Width Modulation is an impressive-sounding phrase but is actually quite simple. The idea is illustrated in Figure 1.

Figure 1 Pulse Width Modulation principle A microcontroller such as the AVR can easily produce such a series of pulses by turning on an output pin for a certain time, then turning it off for some other time. A fragment of code to do this is shown in Figure 2.
void main() { char on_percent = 20; PORTC = 0; DDRD |= 0x01; // set pin 0 to be an output while (1) { PORTC |= 0x01; // turn on output DelayUs(on_percent); // delay for approx. on_percent microseconds PORTC &= ~0x01; // turn off output DelayUs(100 - on_percent); // delay for approx. (100 - on_percent) microseco nds } }

Figure 2 A fragment of code to produce PWM output using the Delay functions The problem with the code in Figure 2 is that the processor spends most of its time in the "DelayUs" function. If you look at the code for the DelayUs function (in the delay.h header file) you will see that it is actually just a loop which runs for a certain number of cycles, this wastes the processor's time and creates a delay. The structure of the program is dedicated to creating the delays for the PWM signal and this makes it hard to add other features which may be desired. Also the DelayUs function is only approximate, it will create an unexpectedly long delay if an interrupt procedure is called (refer back to the TMR0 tutorial if you want to know why). A long delay means a long PWM pulse (a glitch) which in turn could mean improper motor control. A better approach would be to use the TMR0 interrupt to create a regular "heartbeat". Then in the TMR0 interrupt the PWM output pin (or even pins!) could be turned on or off. Exercise: modify the example program tmr0.c to produce PWM output on PORTC, PIN0, with a frequency of about 1kHz. Verify the output using an oscilloscope. Advanced exercise: now modify the program to produce 8 simultaneous channels of PWM on ALL the pins of PORTC. Each channel must be independently controllable. Another approach - not necessarily better, just different - is to make use of one of the AVR's many good features. It has several "built in" PWM pulse generators. In this exercise we will set up the PWM generator associated with timer 1. As always, to learn all the details, consult the Atmel documentation for the AVR. Compile and download the project "pwm.prj". Change your LED connection so that it matches the code... can you see which pin to connect it to from the code below?

1 of 6

3/30/2013 6:29 PM

Micro crash course/pwm - Wiki

http://wiki.mech.uwa.edu.au/index.php/Micro_crash_course/pwm

2 of 6

3/30/2013 6:29 PM

Micro crash course/pwm - Wiki

http://wiki.mech.uwa.edu.au/index.php/Micro_crash_course/pwm

Figure 3 Example program that implements a variable PWM on PORTB, PIN1 Check the output of PORTB, PIN1, using an oscilloscope. There should be bursts of activity every six seconds or so. What is the frequency of the PWM signal?

A small electric motor


A small DC electric motor with gearbox and encoder is shown in Figures 4 and 5. The only reason I chose this motor was because it was in the junk box and was about the right size! This motor happens to be precision made and was worth about $USD200 in 2002. It was designed for use in a precise application such as for laboratory position control.

Figure 4: A small gear-motor with encoder, made by Maxon Co

Figure 5: Close-up of the shaft and mounting bezel.

The gearbox reduces the speed of the motor from about 12000 RPM to a more useful value, about 600RPM, depending of course on drive voltage and torque. The gearbox also increases the torque available at the output shaft. The encoder is of the quadrature type, the subject of a later tutorial. Note that the encoder is fixed directly to the motor shaft, this greatly improves the resolution as measured from the output of the gearbox (provided the gears are not loose or worn). Some small motors, available from electronic hobby stores, cost less than $USD5. Cheap motors generally do not have gearboxes or encoders, however they will work well for the purposes of this tutorial - an expensive Swiss gearmotor is not required!

FET transistors
The AVR can drive a load of about 40mA directly. Unfortunately 40mA is not enough current to turn a small electric motor like the one from my junk box. As usual there is a standard solution to this problem: use a transistor as a driver. There are many kinds of transistor available. However FET transistors have certain useful properties and I would like to show you what they can do. Before we go on, I must introduce the notation for FET transistors, which is not the same as for the more common bipolar transistors. First of all, FET transistors come in two main types, called "N-channel" and "P-channel". They are very similar, the only difference really is the signal required to "turn them on":

Figure 6: Circuit diagram and pinouts for BS170 N-Channel FET

Figure 7: Circuit diagram and pinouts for VP2110 P-Channel FET I want to warn you about transistors and transistor manufacturing companies. The data sheets are confusing and sometimes fail to give the most basic information, such as the pin assignments. The numbers and letters on the transistors are sometimes confusing (or even missing) and may not exactly match the data sheet. The package types (the shape of the transistors) are variable, there are several kinds, and the data sheets may or may not include the ones you have. All this is to say, be careful, do your own tests to check that the transistors you have 1. Are the kind you think they are; and 2. Are working properly. I always do tests like the ones shown in Figure 8 on all transistors before I use them, I have found that this reduces my level of frustration. These tests will also serve as a good way of explaining what the transistors can do for us.

3 of 6

3/30/2013 6:29 PM

Micro crash course/pwm - Wiki

http://wiki.mech.uwa.edu.au/index.php/Micro_crash_course/pwm

Figure 8: Test circuits for FET transistors

N-channel FET
First consider the N-channel FET. When the switch is connected to GND, the gate G (like a control lever) is driven LOW. This turns the transistor OFF which means that the resistance from D to S is very high. No current can flow from D to S and we can imagine that the FET is not even in the circuit! If you imagine that, you will see that test point A must have voltage VCC. When the switch is connected to VCC, the Gate G is driven HIGH. This turns the transistor ON which means that the resistance from D to S becomes quite low (only 5 ohms according to the data sheet!). We can imagine that D is connected to S (this is not actually true and current can only flow from D to S, not the other way). If D were connected to S, clearly test point A would have to be at GND voltage. We say "when the switch pulls G LOW, D floats HIGH" and "when the switch pulls G HIGH, D is driven LOW"

P-channel FET
The P-channel FET is very similar to the N-channel FET. The main difference for our purposes is that the signal required to turn the device ON is the opposite. When switch B is connected to VCC, the resistance from S to D is HIGH. When B is connected to GND, the resistance becomes LOW. We say "when the switch pulls G LOW, D is pulled HIGH" and "when the switch pulls G HIGH, D floats LOW" Note that in each case, whether the transistor is a P-channel or N-channel type, it is always the DRAIN that does the pulling. The SOURCE is connected to a supply line such as GND or VCC, the DRAIN then changes to pull some electrical load either up or down. If all of this HIGH and LOW business is too much, just remember the following: a FET, in fact any transistor, is like a controllable RESISTANCE. Some signal controls the amount of resistance. The detail is all in what kind of signal will cause the change. One final point about these transistors: when they turn on, they are not quite just a resistance. Current can only flow in one direction. The direction of flow depends on the transistor type. For an N-channel transistor, current can only flow from the Drain to the Source; for a P-channel transistor, current can only flow from the Source to the Drain. The transistor is a bit like a diode which can be either inserted into a circuit (when it's on) or removed altogether (when it's off).

What kind of transistors do I need?


The examples on this page use some cheap but low-current transistors, the BS170 and VP2110 types. If you look up the data sheets for these you will see that they can only drive about 500mA loads. This is probably enough to drive a very small motor about the size of your thumb, provided it can run freely at all times. If you want to drive a big motor you will need to use high current transistors, and substitute them in all the diagrams below. For example I recommend the IRF540 or NDP4060 for driving Warman Device motors. These can drive a load of about 15A if you mount it on a proper heatsink, that's enough current to drive a DC motor the size of your fist. The IRF540 and NDP4060 are N-channel MOSFETs so you can change "BS170" to "NDP4060" or "IRF540" everywhere on this page and the circuits will work. For every N-channel transistor there is usually a P-channel "complement" meaning a P-channel transistor that has similar current rating and other electrical characteristics. If you would like to buy pairs of power MOSFETs for construction of the bridges shown below, I recommend N-channel IRF540 Jaycar ZT2466 Pchannel IRF9540 Jaycar ZT2467 These are rated at about 30A (which you will only get if you use good heat-sinks) and cost about $5 each. To make a bridge you need 4 transistors so $20.

Driving the small motor with a single FET transistor


Enough theory, let's build something. Figure 9 is based on Figure 8 (N-channel part only) but now the resistor has been replaced with our motor. When the AVR drives G high, the FET becomes a low resistance, effectively a short-circuit. The motor has one terminal at VCC and the other at GND - it must run! However when the AVR drives G low, the FET cuts the connection to the motor and it cannot run. A motor of the size shown in Figure 4 cannot start and stop hundreds of times per second, so a series of pulses effectively makes it run at some speed which is less than full speed (more correctly, the torque is determined by the PWM fraction).

Figure 9: FET driver for unidirectional PWM Breadboard the circuit shown in Figure 9. Does your motor run? Can you control the speed of your motor from software? If so, isn't that amazing? Can you explain the high pitched sound that the motor makes sometimes e.g when starting?

More power please!


The circuit of Figure 9 is OK but with a small change we can drive the motor at higher voltages. This is a useful technique as it allows us to achieve higher torque for the same motor mass. In other words, your robot, or whatever you are

4 of 6

3/30/2013 6:29 PM

Micro crash course/pwm - Wiki

http://wiki.mech.uwa.edu.au/index.php/Micro_crash_course/pwm

building, can then go faster. Important warning, make sure you understand the following: In the circuit of Figure 10 there are two voltage levels: 5 volts for the AVR, and a higher voltage for the motor only. The FET and the motor can work at the higher voltage safely. However the high voltage will damage the AVR if it ever touches any of the AVR's pins. Therefore when you work on the circuit of Figure 10, always turn the power off first. Double check all your wiring so that you know exactly which wires have the high voltage on them.

Figure 10: Driving a motor at high voltage Breadboard the circuit shown in Figure 10. Have an instructor check your wiring before you turn it on. You should find that the motor runs more strongly or faster than before. You should still be able to control the speed from software. Before beginning work on the next exercise, turn the power supply off and return the breadboard to the state shown in Figure 9. Remove the voltage regulator and make sure the power supply is set to 5V DC. DO THIS NOW BEFORE YOU FORGET. How high can we go? The FET is rated to 60V so the limit is likely to be the motor. Small DC motors are actually quite robust and we do not have to strictly follow the voltage ratings which may be printed on them. However we must NOT ignore the possibility that the motor could overheat in service. When I start to use a new motor, I try to find the highest voltage I can use under realistic load conditions, without causing the motor to get hot. Short bursts of high voltage operation are usually OK. Continuous operation under load is another matter. Be willing to experiment with your motor but learn to recognise the warning signs of a motor that is about to overheat. There is usually a strong smell. If you see smoke, it may be too late. Note that the highest current is usually drawn when the motor is stalled (shaft stopped because the torque is too high for the motor to turn).

Bidirectional operation (H-bridge circuit)


We have achieved speed control and have made a powerful drive circuit. However in robotic work we also usually want to be able to drive a motor either clockwise or counterclockwise. Before we discuss the use of transistors to solve this problem, consider the switch circuit of Figure 11.

Figure 11: Mechanical reversing switch Switches A and B are joined together mechanically so that they have to switch at the same time. When A switches off, B switches on and vice-versa. Switches A and B are joined mechanically to switches C and D so that all must change at the same time. If the switches are as shown in the figure, the motor will run one way, perhaps clockwise. However if all the switches are made to flip to the other position, the motor will run the other way, counterclockwise. Before we go on, consider what would happen if the mechanical connection between switches A and B were to break, and switch A was connected to VCC and switch B was connected to GND. There would be a short-circuit from VCC to GND - not good at all! Now consider a transistorised version of the same circuit. Each switch has been replaced with an FET transistor:

Figure 12: H-bridge circuit based on 4 FET transistors The control inputs A and B determine the flow of current in the circuit. If Control input A is HIGH and Control input B is LOW, the path of the current will be the blue dotted line marked "Counterclockwise". You should think about the resistances of each of the FETs and confirm that this works.

5 of 6

3/30/2013 6:29 PM

Micro crash course/pwm - Wiki

http://wiki.mech.uwa.edu.au/index.php/Micro_crash_course/pwm

To make the final circuit we need some way to guarantee that Control input B is always the opposite of Control input A (think about what would happen if both were the same - would there be a short circuit?). You may have already used a 74LS14 inverter chip in the int0 (hardware debounce) tutorial. We will now use the chip again:

Figure 13: H-bridge circuit based on 4 FET transistors The 74LS14 ensures that when point A is HIGH, B is LOW and vice-versa, which is what we want. The 1k resistors in the circuit are there to prevent high current flow from the digital logic parts of the circuit, in the case of slight voltage differences near the H-bridge. Breadboard the circuit of Figure 13. To test the H-bridge, try holding the input A LOW. The motor should run in one direction. Now try holding it HIGH. The motor should run in the opposite direction. If it does not run, or if it runs very slowly, there is a problem. Check that all the transistors are in the right way around and that you are using the right ones in the right places. Think about what this system will do when the PWM fraction is 50%. Will the motor run? Try it by changing the software so that you have a fixed 50% fraction. Advanced exercise: Try to design an improved PWM system that uses two pins on the AVR: one to produce a "magnitude" PWM signal, and another which is a boolean "direction" i.e. HIGH means clockwise and LOW means counterclockwise. You may need an additional logic chip such as a QUAD NOR gate (or you may not... think about optocouplers and what they can do...) Advanced exercise: Try to design a high voltage version of the H-bridge circuit. The control signals must still be 5VDC. However the supply voltage for the H-bridge should be 15V or more. Hint: the problem lies mainly in turning off the P-ch FETS near the high voltage line, which means driving their G terminals to the high voltage level. Some additional control transistors may be needed... have an experienced person check your circuit before turning it on to prevent damage to your AVR and FETs. Cheat sheet - a design for a single bidirectional PWM motor driver This design is well tested and can be built pretty much as shown. It has a neat feature which is that no logic voltage is needed on the "grunt" or high-voltage side.

A final word about MOSFETs: the problem of low-voltage gate drive


When I first used MOSFETs with high current ratings I had a misunderstanding about them. To use an N-channel MOSFET as an example, I thought that if the GATE voltage was above, say, 4V, the transistor would fully "switch on". However later I learned that this is not quite true. Most MOSFETs you can buy BEGIN to turn on when the Gate is about 3 or 4 volts above the Source. Note that word "begin". The internal resistance of the transistor Drain-Source path gradually decreases as the Gate is driven to higher voltages. To fully switch on a big, powerful MOSFET (i.e. to get the most powerful motor drive out of it), you have to drive the gate to 12V or more. Naturally this depends what kind of MOSFET you have. I present this as an example of a general principle that "there is always more to learn". The "cheat" design above exploits this property of MOSFETs to do a good, strong drive where the Gates are pulled all the way to the "other" voltage. back to main tutorial page Retrieved from "http://wiki.mech.uwa.edu.au/index.php/Micro_crash_course/pwm"

This page was last modified 10:55, 22 February 2010. This page has been accessed 31,712 times. Privacy policy About Wiki Disclaimers

6 of 6

3/30/2013 6:29 PM

Das könnte Ihnen auch gefallen