Pulse width modulation signal. Pulse width modulator

You will need

  • Arduino;
  • Light-emitting diode;
  • resistor with a resistance of 200 ohms;
  • computer.

1 General information about pulse width modulation

Arduino digital pins can only output two values: logic 0 (LOW) and logic 1 (HIGH). That's why they are digital. But Arduino has "special" conclusions, which are indicated PWM... They are sometimes denoted with a wavy line "~" or circled or somehow distinguished from others. PWM stands for Pulse-width modulation or pulse width modulation, PWM.

A pulse width modulated signal is pulse signal constant frequency but variable duty cycle(the ratio of the pulse duration and the period of its repetition). Due to the fact that most physical processes in nature have inertia, sharp voltage drops from 1 to 0 will be smoothed out, taking some average value. By setting the duty cycle, you can change the average voltage at the PWM output.

If the duty cycle is 100%, then all the time at the digital output of the Arduino there will be a logic voltage of "1" or 5 volts. If you set the duty cycle to 50%, then half of the output time will be logic "1", and half - logic "0", and the average voltage will be 2.5 volts. And so on.



In the program, the duty cycle is set not as a percentage, but as a number from 0 to 255. For example, the command analogWrite (10, 64) will tell the microcontroller to send a signal with a duty cycle of 25% to digital PWM output # 10.

Arduino pins with function pulse width modulation operate at a frequency of about 500 Hz. This means that the pulse repetition period is about 2 milliseconds, which is measured by the green vertical strokes in the figure.

It turns out that we can imitate analog signal on the digital output! Interesting, right ?!

How can we use PWM? There are a lot of applications! For example, control the brightness of an LED, motor speed, transistor current, sound from a piezo emitter, etc. ...

2 Demonstration diagram pulse width modulation in arduino

Let's take a look at the most basic example - controlling the brightness of an LED using PWM. Let's put together a classic scheme.



3 Example sketch with PWM

Let's open the "Fade" sketch from the examples: File Samples 01.Basics Fade.



Let's change it a little and load it into the Arduino memory.

Int ledPin = 3; // declare the pin that controls the LED int brightness = 0; // variable for setting the brightness int fadeAmount = 5; // step of changing the brightness void setup () ( pinMode (ledPin, OUTPUT); } void loop () ( analogWrite (ledPin, brightness); // set the brightness brightness on the ledPin output brightness + = fadeAmount; // change the brightness value / * when reaching the limits 0 or 255 change the direction of brightness change * / if (brightness == 0 || == 255) (fadeAmount = -fadeAmount; // change the step sign) delay (30); // delay for more visibility of the effect }

4 LED brightness control using PWM and Arduino

We turn on the power. The LED gradually increases in brightness, and then gradually decreases. We have simulated an analog signal at the digital output using pulse width modulation.

LCHM FM (PM) SCM AMn FMn KAM ChMn GMSK
OFDM COFDM TCM AIM DM PCM ΣΔ PWM CHIM FIM FHSS DSSS CSS

Graph illustrating the use of a three-level PWM for motor control, which is used in variable frequency asynchronous motor drives. The voltage from the PWM modulator applied to the machine winding is shown in blue (V). The magnetic flux in the stator of the machine is shown in red (B). Here, the magnetic flux has an approximately sinusoidal shape, due to the corresponding PWM law.

Pulse width modulation(PWM, eng. pulse-width modulation (PWM)) is the process of controlling the power supplied to the load by changing the duty cycle of the pulses, at a constant frequency. Distinguish analog PWM and digital PWM, binary (two-level) PWM and ternary (three-level) PWM .

Reasons for the spread of PWM

The main reason for using PWM is the desire to increase efficiency in the construction of electronic equipment and in other nodes, for example, PWM is used to adjust the brightness of the backlight of LCD monitors and displays in phones, PDAs, etc.

Thermal power dissipated on the key during PWM

In PWM, it uses transistors as key elements (other semiconductor devices can also be used) not in a linear mode, but in a key mode, that is, the transistor is either open (off) or closed (in a state of saturation) all the time. In the first case, the transistor has almost infinite resistance, so the current in the circuit is very small, and although the entire supply voltage drops across the transistor, the power released on the transistor is practically zero. In the second case, the resistance of the transistor is extremely small, and, therefore, the voltage drop across it is close to zero - the power released is also small. In transient states (transition of a switch from a conducting state to a non-conducting state and vice versa), the power released in the switch is significant, but since the duration of the transition states is extremely short in relation to the modulation period, the average switching loss power turns out to be insignificant.

1. R t r → ∞ ↔ P = U 2 R → 0 (\ displaystyle R_ (tr) \ rightarrow \ infty \ leftrightarrow P = ((\ frac ((U) ^ (2)) (R)) \ rightarrow 0))

2. R t r → 0 ↔ P = I 2 R → 0 (\ displaystyle R_ (tr) \ rightarrow 0 \ leftrightarrow P = (I) ^ (2) R \ rightarrow 0)

How PWM works

Analog PWM

Analog PWM is implemented using a comparator, one input of which is fed with a triangular or sawtooth periodic signal from an auxiliary generator, and the other with a modulating signal. At the output of the comparator, periodic rectangular pulses with variable width, the duty cycle of which changes according to the law of the modulating signal, and the frequency is equal to the frequency of the triangular or sawtooth signal and is usually constant.

Analog PWM is used in low frequency amplifiers of class " D».

Digital PWM

In binary digital technology, the outputs of which can only take one of two values, the PWM approximation of the desired average output level is completely natural. The circuit is just as simple: a sawtooth signal is generated N-bit counter. Digital devices(CSIP) operate at a fixed frequency, usually much higher than the response of controlled installations ( oversampling). In the periods between clock pulse edges, the output of the CSIP remains stable, it is affected by either a low level or a high level, depending on the output of the digital comparator, which compares the value of the counter with the level of the approximated one. digital signal V(n). Exit for many clock cycles can be interpreted as a sequence of pulses with two possible values ​​0 and 1, replacing each other every clock T... The frequency of occurrence of single pulses is obtained proportional to the level of the approaching signal ~ V(n). The units following one after the other form the outline of one, wider impulse. The duration of the received pulses of variable width ~ V(n) are multiples of the clock period T, and the frequency is 1 / ( T*2 N). Low frequency means long, relatively T, periods of constancy of the signal of the same level, which gives a low uniformity of pulse distribution.

The described digital generation circuit falls under the definition of one-bit (two-level) pulse-code modulation ( ICM). 1-bit PCM can be thought of in PWM terms as a train of 1 / T and width 0 or T... The available oversampling allows to achieve averaging in a shorter period of time. High quality is possessed by such a kind of one-bit PCM as pulse-density modulation ( pulse density modulation), which is also called pulse-frequency modulation.

A continuous analog signal is recovered by arithmetic averaging of pulses over many periods using a simple filter low frequencies... Although this is usually not even required, since the electromechanical components of the drive have inductance, and the control object (OA) - inertia, the pulses from the PWM output are smoothed out and the OA, with a sufficient frequency of the PWM signal, behaves as when controlling a conventional analog signal.

In digital PWM, the period is divided into parts that are filled with rectangular subpulses. The average value over the period depends on the number of rectangular pulses. Digital PWM - binary signal approximation (with two levels - incl/off) to a multilevel or continuous signal so that their average values ​​over a period of time t 2 -t 1 would be approximately equal.

Formally, it can be written like this:

∫ t 1 t 2 x (t) dtt 2 - t 1 = ∑ i = 1 n A ∗ 4 T it 2 - t 1, (\ displaystyle (\ int _ (t1) ^ (t2) (x (t) \ , dt) \ over (t2-t1)) = (\ sum _ (i = 1) ^ (n) (A * (\ mathcal (4)) T_ (i)) \ over (t2-t1)),)

where x(t) - input signal ranging from t 1 before t 2 and ∆ T i = t 2 - t 1 n (\ displaystyle (\ frac (t2-t1) (n)))- duration i -th PWM podpulse, each with an amplitude A. n is chosen in such a way that for the period the difference between the total areas (energies) of both quantities is less than the permissible one:

∫ t 1 t 2 x (t) d t - ∑ i = 1 n A ∗ 4 T i< E {\displaystyle \int _{t1}^{t2}{x(t)\,dt}-\sum _{i=1}^{n}{A*{\mathcal {4}}T_{i}} .

The controlled "levels" are usually the power supply parameters of the power plant, for example, the voltage of the pulse converters / DC voltage regulators / or the speed of the electric motor. For impulse sources x Links

Dear microproger, we will talk with you to the utmost simple language:

Imagine an electrical pulse A with a voltage amplitude from 0V to 5V, a length of 1 ms and a repetition period of 10 ms (i.e., within 1 ms, the voltage on the line through which the pulse A passes is 5V, then within 9ms it is 0V, and so repeats every 10 ms). Now let's imagine that we increase the duration pulse A up to 2ms (let it now be pulse B), and it repeats exactly the same every 10ms. A task pulse width changes And from 1 ms to pulse B 2 ms is PWM task.

Generally speaking, the word "Modulation" means change oscillation parameters (frequency, amplitude, phase). Pulse width modulationchange in duty cycle pulses at a constant frequency. The duty cycle is the same as the length, i.e. in our example, this is a change in the pulse width from 1ms to 2ms.

PWM regulator. Example

PWM regulator

PWM operation the regulator is clearly displayed in this picture-graph.

We see three signals on the chart. Signals modulated by PWM ohm that generates and regulates the duty cycle impulses.

The duty cycle on the chart above is 15%. That is, for one period equal to 100%, 15% of the time, a logical unit is issued (TTL voltage level + 3V or + 5V). 75% of the time, a logical zero is output (no voltage in the line - 0V).

On the average graph, the duty cycle is 50% - 50% of the time, a logical 1 is issued, 50% is a logical 0.

On the graph below, the duty cycle is 90%. 90% -1. 100.

If you connect the LED to ours, then in the case of the first graph, the LED will glow dimly. With graph 2, the LED will glow brighter than with 1, but the LED itself will glow at 50% of its power. In the case of the 3rd graph, the brightness of the LED will be adjusted to 90%, close to the maximum.

As you can see, using PWM it is very convenient to adjust the brightness of the LED, as well as the operation of the stepper motor.

Practical value of PWM

Once again I remembered our impulses A and B. They run along the wire to the consumer of electric current and represent an electric current with a certain voltage (Volts) and a certain strength (Amperes), which depends on the consumer. Consumers generally eat a fixed current (for example 300mA). That is, if pulse A or B lasted all 10ms and was not interrupted, then the current consumption for the consumer would be exactly 300mA. If we interrupt the current with a pulse duration, then the current consumption with an active pulse A will be 300mA * (1/10) = 30mA, with a pulse B 300mA * (2/10) = 60mA.

PWM generators are used in the tasks of controlling LED lamps. Everything is extremely simple: the more current we supply to the LED, the brighter it glows. The same with RGB LEDs - we apply to the red (R) pulse A (30mA), to the blue pulse B (60mA), to green 0 - we get dim purple light, which is obtained from less bright red and brighter blue colors.

PWM is applied in the tasks of controlling rotating motors - the more current we supply to the contacts of the engine, the faster it rotates. And if we have three motors, and in addition we have a bunch of ideas and a whole program for sequentially applying impulses of type A and B to their windings? Here you can put together a whole 3D printer!

PWM controller

For microproger understanding of the essence of the phrase “ PWM controller“, It is enough to understand the general purpose of the reference frequency and the methods of issuing a sequence of logical zeros and ones on one leg of the microcircuit.

Let's say we have a microcontroller or FPGA and all the same rotating engine, which at a constant current of 5V consumes 300mA and with this consumption rotates its axis 10 times per second. Now we have been asked to make it so that I press the button, and the engine makes 5 revolutions at a frequency of 1 revolution per second, then makes 2 more revolutions in 1 second and turns off.

O p

Pulse width modulation(PWM, eng. Pulse-width modulation (PWM)) - approximation of the desired signal (multilevel or continuous) to valid binary signals (with two levels - incl/off), so that, on average, over a certain period of time, their values ​​are equal. Formally, it can be written like this:

,

where x(t) is the desired input signal in the range from t1 before t2 and ∆ T i- duration i th PWM pulse, each with an amplitude A. ∆T i is selected in such a way that the total areas (energies) of both quantities are approximately equal over a sufficiently long period of time, and the average values ​​of the quantities over the period are also equal:

.

The controlled "levels" are usually the power supply parameters of the power plant, for example, the voltage of the pulse converters / DC voltage regulators / or the speed of the electric motor. For impulse sources x(t) = U const stabilization.

THORN- a pulse-width converter that generates a PWM signal at a given value of the control voltage. The main advantage of PWM is the high efficiency of its power amplifiers, which is achieved by using them exclusively in the key mode. This significantly reduces the power dissipation at the power converter (SP).

Application

With pulse-width modulation, a periodic sequence of rectangular pulses is used as a carrier wave, and the information parameter associated with a discrete modulating signal is the duration of these pulses. A periodic sequence of rectangular pulses of the same duration has a constant component inversely proportional to the duty cycle of the pulses, that is, directly proportional to their duration. Having passed the pulses through the low-pass filter with a cutoff frequency much lower than the pulse repetition rate, this constant component can be easily isolated by obtaining a constant voltage. If the duration of the pulses is different, the low-pass filter will emit a slowly varying voltage that follows the law of the change in the duration of the pulses. Thus, using PWM, you can create a simple DAC: the sample values ​​of the signal are encoded by the pulse duration, and the LPF converts the pulse sequence into a smoothly varying signal.