SPWM Generation using PIC16F877A Microcontroller (original) (raw)
We will discuss how to use a microcontroller to generate sinusoidal pulse width modulation. We will explore the usage of SPWM signals as gating signals to the MOSFETs of an H-bridge in order to obtain a clean and precise sine wave output. By incorporating an LC filter, we can further enhance the quality of the sine wave. The technique of sinusoidal pulse width modulation is widely employed by inverter manufacturers and in various industrial applications to achieve a pure sine wave. It is also commonly utilized in online UPS systems for generating a clean sinusoidal output. Additionally, we will delve into the integration of these SPWM signals with an H-bridge configuration.
SPWM Introduction
Sinusoidal Pulse Width Modulation (SPWM) is a technique used to synthesize a nearly perfect sine wave from a pulse-width modulated (PWM) signal. It’s commonly used in power electronics and inverters to produce AC voltage or current waveforms that closely resemble pure sine waves.
SPWM is widely used in applications where generating a high-quality AC voltage is essential, such as in uninterruptible power supplies (UPS), variable-frequency drives (VFDs), and grid-tied solar inverters. The key benefit of SPWM is that it produces low harmonic distortion, making it suitable for driving sensitive loads and for interfacing with the grid.
Example
SPWM technique basically converts half of the sine wave into a small number of pulses, each having a different width. Sinusoidal pulse width modulation technique produces a pulsating wave in which the width of the pulses changes according to the amplitude of the sine wave voltage. For example, in a sine wave, the amplitude of the voltage is minimum at the start of the sine wave at 0 degrees, and then it starts increasing up to 90 degrees where the sine peak amplitude is reached. After that, the amplitude starts decreasing again in a reverse fashion as it increased. In the SPWM technique, we follow the same procedure to make a pulsating waveform in which the width of each pulse varies according to the same fashion as followed by the sine wave magnitude.
{ 1,2,3,4,5,6,5,4,3,2,1}
Above data shows the maximum duty cycle or width of the pulse is 6, and then it starts decreasing in reverse as it is increased. The figure below shows the pulses of SPWM.
SPWM sample
Methods to Generate SPWM
There are two ways to generate SPWM:
Analog Method
Here’s how SPWM generation works in analog method:
Reference Sine Wave: In SPWM, you start with a reference sine wave, which represents the desired output waveform. This reference sine wave has the same frequency and amplitude as the desired AC signal.
Comparison with a Carrier Signal: A high-frequency carrier waveform is generated, typically a high-frequency triangular or sawtooth wave. This carrier signal serves as a time reference.
Comparison: The reference sine wave is compared with the carrier signal. The comparison process involves continuously comparing the instantaneous amplitude of the sine wave with the instantaneous value of the carrier signal.
Pulse Width Modulation (PWM): The comparison results in a pulse-width modulated (PWM) signal. The width (or duration) of each pulse is determined by the comparison between the reference sine wave and the carrier signal. The pulse width varies to match the amplitude of the reference sine wave at each instant.
Gate Control: The PWM signal controls the gates (switches) of power electronic devices, such as transistors or MOSFETs, in an inverter circuit. These devices switch the DC voltage on and off to generate an AC voltage.
Output Synthesis: The switching of the power devices according to the PWM signal generates an AC-like voltage at the output. By carefully adjusting the pulse widths based on the reference sine wave, the output waveform closely resembles a sine wave.
By using microcontroller or digital electronics.
This method is preferable and is used in almost all pure sine wave inverters available in the market because it is cheap and easy to implement. All we need is a piece of information, i.e. code, to implement SPWM through this technique. The same microcontroller can also be used for other functions in the inverter, like protection, voltage and current reading, digital display of current and voltage, and any other functionality we want to have in our inverter. It is the reason we always prefer microcontrollers or digital electronics over analog electronics. It makes our lives easier by adding extra functionality to our project. In this article, we will also discuss how to use PIC16F877A to generate SPWM.
Steps to implement SPWM with a microcontroller
Initialize the Microcontroller: Set up the microcontroller’s clock source and configure any necessary peripherals like timers, GPIO pins, and the PWM module.
Create a Sine Wave Table: Create a table that contains discrete values representing one complete cycle of a sine wave. The number of entries in the table determines the resolution of the SPWM signal. The table should span 0 to 2π radians (or 0 to 360 degrees) and should include values that closely approximate a sine wave.
Generate PWM Signal: In your code, you will use a timer to control the PWM signal. The timer’s period (time for one cycle) will determine the frequency of the SPWM signal. The duty cycle of the PWM signal will be adjusted to generate the desired SPWM.
Calculate PWM Duty Cycle: In the interrupt service routine or periodically within your code, calculate the appropriate duty cycle for the PWM signal based on the desired amplitude of the sine wave at that point in time. You can use the sine wave table for this purpose.
Output PWM Signal: Set the PWM duty cycle to control the PWM output pin. The PWM signal should vary according to the values in the sine wave table to approximate a sine wave.
Loop and Repeat: Continuously update the PWM duty cycle to generate the entire SPWM signal.
Here’s a simplified code snippet for implementing SPWM on a PIC microcontroller:
#include <xc.h> // Include the XC compiler library for PIC
// Define the sine wave table
const unsigned int sineTable[] = { ... }; // Populate with sine wave values
void main() {
// Initialize the microcontroller and PWM module
// Configure the timer for PWM generation
while (1) {
// Calculate and set the PWM duty cycle using values from the sine table
// Adjust the delay to control the frequency and phase of the SPWM signal
// Update the PWM output
}
}
SPWM implementation using PIC16F877A microcontroller
Before implementing SPWM with a microcontroller, you should know the frequency of the sine wave you want to generate. In this project, I have used a 50 Hz frequency. A 50 Hz frequency means the time period of the sine wave is 20 ms. Therefore, the time period for half a cycle is 10 ms, as we generate only half a cycle that is used for both the positive and negative cycles. The H bridge serves the purpose of generating the negative or positive cycle. SPWM for half a wave contains multiple pulses, and the width of each pulse varies according to the amplitude of the sine wave. However, the total time of all the pulses should be equal to 10 ms, the time period of half a cycle of the sine wave. But the question is, what should be the time period of each pulse? The time period of each pulse depends on the frequency of the PWM. For example, if we have
Number of pulses = 10ms / 100us =100 pulses.
Example
We can use 100 pulses for SPWM with a 20 kHz frequency. We have already explained the functionality and working principle of SPWM. Now, we will explain how to generate 100 pulses with variable width or duty cycle according to the amplitude of the sine wave. We know the relationship of the sine wave with its phase angle and peak value. y = A * sin(angle). We also know that a half cycle of the sine wave consists of 180 degrees. For example, if we need 10 pulses, we divide 180 degrees into 10 equal parts. To achieve this, each step would be 180/10 = 18 degrees.
- Y = sin (18) = .3090
- Y= sin (36) = .5877
- Y = sin (54) = .8090
- Y = sin (72) = .9511
- Y = sin (90) =1
- Y = sin (90) =1
- Y = sin (72) = .9511
- Y = sin (54) = .8090
- Y= sin (36) = .5877
- Y = sin (18) = .3090
To convert the above values into a duty cycle, multiply them by the maximum duty cycle value that a microcontroller uses to generate the duty cycle. In the PIC16F877A, the duty cycle ranges from 0 to 255, where 0 represents a 0% duty cycle and 255 represents a 100% duty cycle. I recommend multiplying by a value less than 255, as it will help you avoid the gate turn-on or turn-off time circuitry. This will introduce a delay between the turn-off and turn-on of the MOSFETs in the H-bridge configuration.
- Y’ = sin (18) = .3090 * 250= 77
- Y’= sin (36) = .5877 *250=147
- Y’ = sin (54) = .8090 *250=202
- Y’ = sin (72) = .9511 *250=238
- Y’ = sin (90) = 1 *250=250
- Y’ = sin (90) = 1 *250=250
- Y’ = sin (72) = .9511 *250=238
- Y’ = sin (54) = .8090 * 250=202
- Y’= sin (36) = .5877 *250 =147
- Y’= sin (36) = .5877 *250= 77
Hence, the duty cycle or width of each pulse is {77, 147, 202, 238, 250, 250, 238, 202, 147, 77}. Similarly, you can calculate the duty cycle or pulse width for 20, 30, 100, or any number of pulses you want to use in your SPWM. The greater the number of pulses, the more pure sine wave will be produced. I have explained everything you need to know about sinusoidal pulse modulation: how to choose the number of pulses, how to calculate the width of each pulse, and what the relationship is between the time of total pulses and the timer of half the cycle of the sine wave. Now you can develop an algorithm using any microcontroller to generate SPWM. I have used the PIC16F877A microcontroller to generate SPWM with 100 pulses and a frequency of each pulse of 20 kHz. The frequency of the output sine wave is 50 Hz.
Steps to Write Algorithm
- Make an array containing duty cycle values of each width
- Generate a PWM of frequency 20 KHz
- call each in a function such a way that it repeat itself after a complete cycle.
This is all you need to do with a microcontroller to generate SPWM. For more information about how to write code for SPWM using the PIC16F877A microcontroller and how to use SPWM for producing gating signals for an H-bridge, as well as the complete circuit diagram of a pure sine wave inverter, please refer to the following article.
Conclusion
In conclusion, sinusoidal pulse width modulation (SPWM) is a powerful technique used in power electronics and inverters to generate high-quality AC voltage waveforms that closely resemble pure sine waves. By using SPWM, it is possible to achieve low harmonic distortion and produce clean and precise sine wave outputs. The implementation of SPWM can be done through analog methods or by utilizing microcontrollers and digital electronics. The use of a microcontroller offers advantages such as flexibility, cost-effectiveness, and the ability to incorporate additional functionalities. By following the steps outlined in this article, it is possible to generate SPWM signals using a microcontroller like the PIC16F877A and achieve a pure sine wave output. Overall, SPWM is widely employed in various industrial applications and is an essential technique for producing high-quality AC voltage waveforms.
You may also like to read:
- Soft starter for 3 phase induction motor using Arduino
- Variable Frequency Drive 3 phase Induction Motor using Pic Microcontroller
- three phase variable frequency SPWM for VFD using pic microcontroller
- three phase sine wave inverter using pic microcontroller
- Three phase space vector pulse width modulation using pic microcontroller
- single phase pure sine wave inverter using arduino
- Pure Sine Wave Inverter using PIC Microcontroller