Pulse Width Modulation - Arduino Working ExplanationFollow article
In this post today, I’ll walk you through PWM in detail. What is PWM and How Does It Work in Embedded Systems.
To understand PWM, we need to know the difference between digital and analogue signals. The digital signal works between HIGH and LOW values or in the form of 1 or 0 which means either you’ll constantly get a HIGH value or 5V for a specific period or a LOW value or ground. But this scenario is different in the case of an analog signal. In this case, you can get infinite values between 1 and 0. Moreover, analogue signal provides the continuous values of the signal at a particular event while digital signals, on the other hand, provide values in ON and OFF pulses.
Let’s get started.
What is PWM?
PWM stands for Pulse Width Modulation which is a process to control average power supplied by the electrical signal. It is a method in which digital outputs are used to control analogue devices. This phenomenon is widely used to control inertial loads such as the speed of the motor, the brightness of the light, and the temperature of the heating elements.
Using PWM, the average power (in the perspective of the value of current or voltage) applied to the load is controlled by a turning switch that stands between the load and supply. PWM doesn’t offer true analogue output, it provides power in pulses, instead. This means you won’t be getting current continuously delivered to the load, you’ll receive power in on and off pulses.
The total power applied to the load is directly proportional to the time in which the switch remains turned ON. The percentage of the digital signal in which it is turned ON is called the duty cycle. Thus, controlling the ON phase of the digital signal over a consistent time interval controls the average voltage applied to the device. The time duration at which pulse remains in the On/Off state is called the width of the pulse wave.
Know that the electrical device controlled by PWM shows behaviour that is the result of the average of wave pulses.
Less Power Dissipation:
The PWM comes with less power loss compared to controlling power levels using an analogue potentiometer since the switching transistor used between motor load and supply remains either fully turned ON or fully turned OFF. Thus, the switching transistor comes with reduced power dissipation which leads to the linear type of transistor control for better motor speed stability.
PWM in Embedded Systems:
PWM is widely used in embedded systems including microcontrollers i.e. Arduino, PIC, Atmel etc. and in microprocessors i.e. raspberry pi pico, raspberry pi zero etc.
The PWM stands between HIGH and LOW values. The HIGH value means the maximum power delivered to the load at which it operates and the LOW value means ground where no power is delivered to the load. PWM works as a controlled switch that controls the power being delivered to the load.
PWM in Microcontroller:
For instance, we take an example of a microcontroller connected with the motor. As mentioned earlier, PWM is a process in which digital outputs are used to control analogue devices (like motors). When you connect the motor with a microcontroller, the controller will produce a modulated digital signal which is then used to control the analogue motor device. It is important to note that, during each interval, voltage is being supplied and then removed but it happens so fast that due to inertia you can never put the motor in a grinding halt. Removing the voltage will only slow down the speed of the motor and applying voltage again will turn it on. The reason you’ll never experience an instant stop of the motor when it is being controlled by PWM.
PWM in Raspberry Pi and Arduino
Both Arduino board and Raspberry Pi boards(i.e. Raspberry Pi Pico, Raspberry Pi Zero etc.) support PWM pins through which you can produce regulated pulses to control the connected motor or brightness of the LED.
The PWM function in these boards that periodically switches between HIGH and LOW values at a specified rate, can be used to control the brightness of the LED. Know that the pins that are used for PWM are fixed in Arduino. The PWM output pins come with a “~” mark next to the number (i.e. 3, 5, 6, 9, 10, and 11 in the following case of Arduino board).
The brightness of LED can be controlled by controlling the duty cycle. As you vary the amount of duty cycle, you will get different brightness on the LED. If you apply the same duty cycle for all colours on RGB LED, then you’ll get a white light of varying brightness.
You’ll experience teal colour if you mix blue equally with green colour. And to get the orange colour, you need to provide a 50% duty cycle for green and keep red fully turned on and blue fully off.
The square wave frequency does impact the brightness of the LED. Know that PWM, duty cycle, and frequency of the signal are related to each other. The result on a 20% duty cycle wave at 1 Hz will be different from the result you’ll get on a 20% duty cycle at 100 Hz. The former case will turn on the LED for a small period but you’ll hardly experience it with the naked eye while, in the latter case, when you apply duty cycle with higher frequency, the LED will get turned ON with more intensity compared to a lower frequency.
The PWM is widely used in the following applications.
- Used to control the speed of the motor
- Used to generate an audio signal
- Employed to drive buzzer with different loudness
- Telecommunication: Encode message
- Used in smart lightening system
- Control the direction of servo motor.
That’s all for today. Hope you’ve found this article helpful. If you have any questions, you can approach me in the section below. I’d assist you the best way possible. Thank you for reading this post.