Pulse-Width-Modulation is a way of controlling voltage digitally to emulate an analog signal. By turning a digital output
OFF very rapidly, an intermediate average output voltage can be achieved:
Although PWM is a modulation technique that can be used for communication, it’s most often used to:
- Limit the voltage being supplied to a device.
- Vary the output of LEDs.
- Control motors such as servos.
A PWM signal generated by a Meadow is a square wave that can be described by two key parameters known as the duty cycle and frequency.
The duty cycle describes the percentage of time the signal is
HIGH, and the average output voltage is directly tied to this parameter. Consider the following wave with a duty cycle of
In the above diagram, the signal is
HIGH half of the time, and
LOW the other half. The average voltage output is therefore
3.3V * 0.5 = 1.6V.
If the duty cycle were lowered to 25% (
0.25), then the average voltage output would also be lowered:
In this case, the average voltage output would be around
3.3V * 0.25 = 0.825V
Note that in both cases, the frequency is the same; the rising edges of the signal are aligned to the same point in time.
So the frequency is the same but the duty cycle is different.
However, for a higher resolution signal, the frequency can be increased while keeping the same duty cycle. Consider the following PWM with a 50% duty cycle but a frequency twice as high as the previous signals:
In this case, the signal is cycling
OFF twice as fast as the 50% cycle before, but still providing the same average voltage output.
Higher frequencies create smoother signals, and are required in some cases. For instance, when using a PWM signal to control the brightness of an LED, a frequency of at least 60Hz (60 cycles per second) is required because humans will notice a flicker in frequencies below that.
PWM signals can be generated via hardware (on the microcontroller) as well as via software. However, except for very slow frequencies, the hardware PWM generator should be used, as it doesn’t cause any load on the processor.
PWM Support in Meadow
Nearly every digital pin on the Meadow F7 board supports PWM.
Meadow F7v2 Micro Pinout
Meadow F7v1 Micro Pinout
Something to be aware when creating PWM ports on multiple pins is that PWM pins share timer groups, meaning they will run at the same frequency, but can have different duty cycles. With this in mind, when you want to create multiple PWM ports running on different frequencies, refer to the pinout diagram above and choose those that belong to a unique PWM timer group.
Hardware PWM signals are controlled via an
IPwmPort, which is created via an
IPwmPort pwm = Device.CreatePwmPort(Device.Pins.D07, 100, 0.5f); pwm.Start();
Software Generated via
For PWM frequencies below
1hz (one cycle per second), as used in industrial control systems such as HVACs, Meadow.Foundation contains a
Generators.SoftPwmPort that can be created on any
IDigitalOutputPort and used just like a hardware PWM port:
IDigitalOutputPort digiOut = Device.CreateDigitalOutputPort(Device.Pins.D00); IPwmPort softPwmPort = new SoftPwmPort(digiOut);