Basics of PWM (Pulse Width Modulation)

Post Reply
User avatar
Gregski
Posts: 2274
Joined: Tue Sep 14, 2021 10:28 am
Location: Sacramento, California
Has thanked: 330 times
Been thanked: 451 times
Contact:

Basics of PWM (Pulse Width Modulation)

Post by Gregski »

As you venture into the EV Conversion world you will come to realize that different components speak different languages if you will. More specifically different EV components are controlled using different technologies. Some rely on CAN (Controller Area Network) or as I like to call it Car Area Network. I torture you with that write up here: I think I CAN, I think I CAN... others rely on something called PWM, Pulse Width Modulation.

I did not write this article, that credit goes to Timothy Hirzel, I just stumbled upon it and thought I would share it, as it is an easy read. In case it ever goes away I will cut and paste it in here as well:


Basics of PWM (Pulse Width Modulation)
Learn how PWM works and how to use it in a sketch..


The Fading example demonstrates the use of analog output (PWM) to fade an LED. It is available in the File->Sketchbook->Examples->Analog menu of the Arduino software.

Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between the full Vcc of the board (e.g., 5 V on UNO, 3.3 V on a MKR board) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of "on time" is called the pulse width. To get varying analog values, you change, or modulate, that pulse width. If you repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is a steady voltage between 0 and Vcc controlling the brightness of the LED.

In the graphic below, the green lines represent a regular time period. This duration or period is the inverse of the PWM frequency. In other words, with Arduino's PWM frequency at about 500Hz, the green lines would measure 2 milliseconds each. A call to analogWrite() is on a scale of 0 - 255, such that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time) for example.

pwm.jpg

On some microcontrollers PWM is only available on selected pins. Please consider the pinout diagram of your board to find out which ones you can use for PWM. They are denoted with a tilde sign (~).

Once you get this example running, grab your Arduino and shake it back and forth. What you are doing here is essentially mapping time across the space. To our eyes, the movement blurs each LED blink into a line. As the LED fades in and out, those little lines will grow and shrink in length. Now you are seeing the pulse width.

Originally Written by Timothy Hirzel
"I don't need to understand how it works, I just need to understand how to make it work!" ~ EV Greg
Post Reply