PULSE-WIDTH MODULATION SOFTWARE AND HARDWARE IMPLEMENTATION
PULSE-WIDTH MODULATION SOFTWARE AND HARDWARE IMPLEMENTATION
Peter Safir
Bachelor of Science, The Azrieli College of Engineering in Jerusalem (JCE),
Israel, Jerusalem
ABSTRACT
In this article I will present software and hardware methods to get PWM on different embedded system platforms and different chips such as 555 and TL494. We will discuss how to increase the frequency of PWM on Arduino boards. How to generate PWM on different embedded systems using software. Using PWM to Generate an Analog Output.
Keywords: PWM, 555, TL494, embedded system, LPF, Arduino.
INTRODUCTION
Quite often we need a smooth control of some process, for example a smooth increase or decrease of speed of a stepper motor, or change of brightness of a light-emitting diode. Direct connection of our object to the power supply will not give us the desired effect, and the digital-analog converter is not available on every embedded system board, and even more so not in every processor it is integrated. But any processor can create a pulse of a given frequency and we can convert this pulse of a certain frequency into PWM [1]. Pulse width modulation is generate variable-width pulses at the same frequency and amplitude of the pulse. PWM [2] can be generated with embedded systems [7] in two ways, software using a timer and hardware by configuring registers.
PWM and Embedded system
Using the Arduino [4] as an example, let's try to figure out how we can generate pulse width modulation on certain pins with the help of the hardware and software configuration.
Picture 1.1 Pulse-width modulation 25%, 50%, 75%
PWM hardware configuration
Any microcontroller [5] has timer counters which perform the most demanding functions in any project, the main purpose of these timer counters is to count down the set time intervals, but besides this the registers of these timers can be configured to perform such functions as counting the duration of incoming pulses and also their quantity and most importantly these timers can generate a PWM[3] signal. The Arduino framework includes a plug-in library Arduino.h, where the timer registers are already set to generate PWM signals on certain pins. Not always the settings set by the Arduino developers are suitable for our projects, because these settings do not take into account the maximum frequency of PWM. If we look in the documentation of the manufacturer of the ATmega328[6], on the basis of this processor Arduino UNO[3], we see that all timers can generate PWM at 64kHz, but the Arduino developer has set the default PWM generation frequency presented in Tab 1.1.
Table 1.1.
Default frequency of the PWM. Arduino UNO
Timer |
Frequency |
Timer 0 |
976 Hz |
Timer 1 |
488 Hz |
Timer 2 |
488 Hz |
This is because Timer 0 counted exactly milliseconds, and all other timers were made to a single standard, so that the developers do not get confused.
The hardware setting on the ATmega328 processor is done by the registers described in Tab 1.2.
Table 1.2.
Timer registers. ATmega328
Timer |
registers |
bits |
Timer 0 |
TCCR0B, TCCR0A |
8 |
Timer 1 |
TCCR1A, TCCR1B |
16 |
Timer 2 |
TCCR2B, TCCR2A |
8 |
Example of PWM configuration on pin nine at 62.5kHz and 10% fill factor using processor registers:
void setup() {
TCCR1A = TCCR1A & 0xe0 | 1;
TCCR1B = TCCR1B & 0xe0 | 0x09;
analogWrite(9, 25);
}
Software PWM generation
The Arduino framework has two built-in functions such as millis() and micros() in the plug-in libraries, which use the same timers as the hardware PWM. With these two functions you can easily implement PWM within the available frequency limits of the Arduino [3] platform developers. You can generate PWM using the software on any of the available pins. The disadvantage of generating PWM using the software is that at the maximum output frequency the processor will not have enough time to perform other functions.
Analog signal generation
To get an analog signal without using a D/A converter you need a minimum of external components. In most cases the conversion can be done with a single RC-chain or a low pass filter, Pic 1.2.
Picture 1.2 RC filter (low pass filter)
The output pulses are in the form of a triangular signal, and their frequency is equal to the frequency of the incoming rectangular pulses, Pic 1.3.
Picture 1.3 Input PWM(green), output analog signal(blue). LPF
Microchip based PWM generation
There are many ways where you can generate PWM [2] without using any processor, for example on a TL494 chip or with an analog comparator when a triangular signal is applied to the comparator input. Also the NE555 analog timer is very popular. By combining resistors and capacitors we can set the frequency and the duration of the pulse.
Conclusion
As we see there are many ways to generate PWM. On the basis of any microprocessor there are two ways of PWM generation, register settings and software PWM generation. Also there are enough ways to generate PWM without using any processor only on the basis of ready-made chips.
References:
- Isaak D Mayergoyz, Siddharth Tyagi. Pulse Width Modulation in Power Electronics. World Scientific Pub Co Inc 2021. P. 67 – 89.
- D. Grahame Holmes, Thomas A. Lipo. Pulse Width Modulation for Power Converters: Principles and Practice 1st Edition. Wiley-IEEE Press 2003. P. 34 – 56.
- Xinbo Ruan. Soft-Switching PWM Full-Bridge Converters: Topologies, Control, and Design 1st Edition. Wiley 2014. P. 45 – 56.
- Massimo Banzi, Michael Shiloh. Getting Started with Arduino: The Open Source Electronics Prototyping Platform (Make) 3rd Edition. Make Community, LLC 2015. P. 34 - 80.
- J. M. Hughes. Arduino: A Technical Reference: A Handbook for Technicians, Engineers, and Makers 1st Edition. O'Reilly Media 2016. P. 34 – 78.
- Jeremy Blum. Exploring Arduino: Tools and Techniques for Engineering Wizardry 2nd Edition. Wiley 2019. P.78 – 88.
- Elecia White. Making Embedded Systems: Design Patterns for Great Software 1st Edition. O'Reilly Media 2011. P. 78 -102.