[ Log In ]
Image of the Atmega324p


USB AVR programmer

USB AVR Programmer

Breadboard adapter for the USBasp AVR Programmer

USBasp Breadboard Breakout Adapter

ST Link v2 for STM32 and STM8 microcontrollers top view of all components

ST-Link v2 STM32 and STM8 Programmer

STM32F0 microcontroller and interface board top view

STM32 M0 MCU and Interface to Breadboard

Serial to USB converter with Micro USB cable

USB to Serial Converter

Thumbnail: Crystal Oscillator 18.432 MHz for UART

18.432 MHz Crystal Oscillator 18pf 30ppm

Thumbnail: 22 pF Capacitor

22 pF Multilayer Ceramic Capacitor

Thumbnail: Quartz crystal oscillator - 16 MHz

16 MHz Crystal Oscillator 20 pF Through Hole

Thumbnail: 4x4 keypad top view.

4x4 Keypad with Adhesive Backing

Skip Navigation Links

Introduction to PWM for the AVR (Atmel) Microcontrollers

PWM stands for Pulse Width Modulation and is the method to produce variable voltages using digital means. Typically, variable voltages come from analog circuits, and digital circuits produce only two voltages, the high (5v, 3.3v, 1.8v, etc.) or low (0v). So how it is possible that digital circuits can produce avoltage that is between the high and the low voltages? If you bring a digital signal up and down, in a consistent manner, you will get a proportion of the voltage between the high and low voltage. Imagine if a digital signal was pulsed high (5v) and low (0v) evenly, say the signal was in the high state for 1 microsecond and in the low state for 1 microsecond, add a capacitor to smooth the signal, the voltage would measure 2.5 volts. Now, change the high voltage in the high state for 9 microseconds and in the low state for 1 microseconds, the voltage would measure 90% of 5 volts, or 5v x .9 = 4.5 volts. The 90% is significant because the duty cycle is represented as a percentage (%). The applications associated with PWM could be: thecontrol of motors, sound output, dimming LEDs, and producing approximated analog waveforms.

Ok, let's get more technical, because PWM has many requirements and specifications that are very important to insure that you are outputting a PWM signal that will be accepted by the device that is receiving it. The device receiving the PWM that is being outputted by your microcontroller will require the PWM to be at a particular frequency. The period of the PWM is what creates the frequency and it is represented as a length of time. The period is where the digital signal is held high and then goes low and the proportion within this period is the duty cycle. The period is selected initially and doesn't change. The longer the period, the slower the frequency and the sorter the period, the faster the frequency. The frequency of the PWM is how many of these periods can fit within one second. If the period is 1 milisecond long, then the frequency would be 1 kHz, or 1000 Hz, or 1000 times per second.

There are a couple types of PWM, including phase correct where the pulse happens right in the middle of the period, and standard PWM where the pulse happens at the end of the period. The duty cycle, as stated above, is the percentage that the pulse is high within the period. For instance, a 50% duty cycle would be half of the high voltage level.

If you have a quick enough PWM, you can also make analog waveforms of almost any kind. By varying the duty cycle at each period, you could essentially draw the waveform and have it output that way, but the waveform will appear a bit stairlike, rather than a perfect waveform. The smaller the periods, or higher the frequency, the better and more smooth the waveforms can be.

I provide an example on the video on sending a PWM signal from the AVR microcontroller to a hobby servo. Hobby servos, the standard type rather than the digital type will receive a PWM signal, generally with a 20ms period and the pulse within this period generally has contraints of .9 and 2.1 ms. The .9 ms pulse will set the servo's horn at 0 degree rotation and the 2.1 ms will set the servo's horn to 180 degrees. These numbers may vary depending on the servo that you use, and even if the datasheet gives you these numbers, your servo may be slightly different as you will see in this and my other videos.

So, how does the microcontroller create a PWM? The microcontroller uses its clock source and a built-in timer mechanism. You can control the internal timer to count up and then set back to 0 at a particular count, so the timer will count up and then set back to 0 over and over again. This sets your period. You now have the option of controlling a pulse, turning a pulse on at a sepcific count in the timer while it goes up. When the counter goes back to 0, then you turn off the pulse. There is a lot of flexibility with this because you can always access the count of the timer and provide different pulses with a single timer. This is great when you want to control multiple servos at once.