As an Amazon Associate we earn from qualifying purchases.
This article explains what a timer and PWM are, and shows how a PIC microcontroller can produce a pulse-width-modulated, sine-like signal using nothing but timed delays. No PWM module is used. It is a good exercise for understanding what a PWM peripheral does for you behind the scenes.
What is PWM?
Pulse width modulation (PWM) is a way of controlling power or signal level with a digital pin. The pin switches between fully on and fully off at a fixed rate. What changes is the duty cycle, which is the share of each period that the pin stays high.
- A 10% duty cycle means the pin is high for one tenth of each period.
- A 50% duty cycle gives a symmetrical square wave.
- A 90% duty cycle means the pin is high almost all the time.
The average voltage of a PWM signal is roughly the supply voltage multiplied by the duty cycle. A simple RC low-pass filter smooths the pulses into a steady analog level. If the duty cycle changes over time, the filtered output follows it.
What is a timer and how does it relate to PWM?
A timer is a counter inside the microcontroller that counts clock pulses. It can measure time, create delays, or trigger an action when it reaches a set value. Hardware PWM modules use timers: one timer sets the period, and a compare value decides when the pin goes low.
If you do not use that module, you can still create the same effect in software. You set a pin high, wait, set it low, and wait again. The program below does exactly that, using the compiler’s delay function (which is built on instruction timing) instead of a PWM peripheral.
Common applications of PWM
- Dimming LEDs and lamps
- Controlling DC motor speed
- Driving servos
- Switching power converters and inverters
- Generating simple analog voltages from a digital pin
Sine wave using timer only (no PWM module used)
The photos below show the test setup and the output captured on an oscilloscope (CRO).
The code is written for a mid-range PIC with a 16 MHz crystal, such as the PIC16F877A, and uses the HI-TECH C style header and delay functions.
#include<pic.h>
#define _XTAL_FREQ 16000000
__CONFIG(0x3f7a);
void main()
{
TRISA=0;
PORTA=0;
while(1)
{
PORTA=0xff;
__delay_us(5);
PORTA=0;
__delay_us(95);
PORTA=0xff;
__delay_us(10);
PORTA=0;
__delay_us(90);
PORTA=0xff;
__delay_us(15);
PORTA=0;
__delay_us(85);
PORTA=0xff;
__delay_us(20);
PORTA=0;
__delay_us(80);
PORTA=0xff;
__delay_us(25);
PORTA=0;
__delay_us(75);
PORTA=0xff;
__delay_us(30);
PORTA=0;
__delay_us(70);
PORTA=0xff;
__delay_us(35);
PORTA=0;
__delay_us(65);
PORTA=0xff;
__delay_us(40);
PORTA=0;
__delay_us(60);
PORTA=0xff;
__delay_us(45);
PORTA=0;
__delay_us(55);
PORTA=0xff;
__delay_us(50);
PORTA=0;
__delay_us(50);
PORTA=0xff;
__delay_us(55);
PORTA=0;
__delay_us(45);
PORTA=0xff;
__delay_us(60);
PORTA=0;
__delay_us(40);
PORTA=0xff;
__delay_us(55);
PORTA=0;
__delay_us(45);
PORTA=0xff;
__delay_us(50);
PORTA=0;
__delay_us(50);
PORTA=0xff;
__delay_us(45);
PORTA=0;
__delay_us(55);
PORTA=0xff;
__delay_us(40);
PORTA=0;
__delay_us(60);
PORTA=0xff;
__delay_us(35);
PORTA=0;
__delay_us(65);
PORTA=0xff;
__delay_us(30);
PORTA=0;
__delay_us(70);
PORTA=0xff;
__delay_us(25);
PORTA=0;
__delay_us(75);
PORTA=0xff;
__delay_us(20);
PORTA=0;
__delay_us(80);
PORTA=0xff;
__delay_us(15);
PORTA=0;
__delay_us(85);
PORTA=0xff;
__delay_us(10);
PORTA=0;
__delay_us(90);
PORTA=0xff;
__delay_us(5);
PORTA=0;
__delay_us(95);
}
}
How the code works
Setup
_XTAL_FREQ tells the compiler the clock is 16 MHz, so __delay_us() produces correct delays. The __CONFIG line sets the configuration bits, such as the oscillator type and watchdog. Check these against the datasheet for your chip and crystal. TRISA=0 makes all PORTA pins outputs, and PORTA=0 starts them low.
The PWM pattern
Each high/low pair adds up to 100 µs, so the PWM carrier runs at about 10 kHz. Only the split between high and low changes:
- The high time starts at 5 µs (5% duty).
- It rises in 5 µs steps up to 60 µs (60% duty).
- It then falls back in 5 µs steps to 5 µs.
That is 23 steps of 100 µs, so one full cycle takes about 2.3 ms. The repeat rate of the envelope is therefore a little under 435 Hz. Loop and port-write instructions add a small extra delay, so measure the real frequency on the oscilloscope.
What you see on the output
On the oscilloscope you see a train of pulses whose width grows and shrinks. Pass the pin through an RC low-pass filter and the average level rises and falls with the duty cycle. With a 5 V supply, that would swing from roughly 0.25 V to about 3 V. The filter cut-off should sit well below 10 kHz and above the roughly 435 Hz envelope.
Limits of this approach
- The steps are linear. The duty cycle changes by equal amounts, so the filtered result is closer to a triangle than a true sine. A real sine needs a lookup table with values such as 50 + 50×sin(angle).
- The CPU is fully busy. Delay loops block everything else, so the chip cannot do other work.
- Timing is approximate. Extra instructions add jitter and drift compared with a timer-driven module.
- All PORTA pins switch together. The code writes 0xFF, so every PORTA pin carries the same signal. On many PICs, RA4 is open-drain and needs a pull-up resistor to show a proper high level.
How to improve it
To make a better sine, store 20 to 100 duty values from a sine function in an array. Then step through them from a timer interrupt, so the main program stays free. Better still, let the hardware PWM module generate the carrier, and update its duty register from the interrupt. The PIC tutorial on hardware PWM on the PIC16F877A covers that method.
The same idea scales up to power electronics. Inverters use a high-frequency PWM carrier whose duty cycle follows a sine reference, and a filter recovers the AC waveform. For a practical example, see this sine wave inverter using Arduino or the push-pull sine PWM inverter. If you need two outputs that never overlap, such as for a half-bridge, look at complementary PWM. Faster carrier frequencies are also possible, as shown in the 62.5 kHz PWM on an Arduino example.
FAQ
Can I make PWM without a PWM module?
Yes. You can toggle a pin and wait with delays or timer interrupts, as in this example. It works for simple tests, but a hardware PWM module is more accurate and leaves the CPU free.
Is the output of this program a true sine wave?
Not exactly. The duty cycle changes in equal steps, so after filtering you get a triangle-like wave. To approximate a sine, use duty values taken from a sine table.
How do I turn PWM into a smooth analog signal?
Use a low-pass filter, such as a resistor and capacitor. Set its cut-off well below the PWM carrier frequency but above the frequency of the wave you want to keep.
What is the PWM frequency in this code?
Each pulse and gap together last 100 µs, so the carrier is about 10 kHz. The duty cycle sweeps up and down about 435 times per second.