This sine table calculator lets you choose how many SPWM steps (pillars) you want, and then gives you the correct sineTable[]
values (0 to 255 range) to paste directly into Arduino code.

The above image shows one half cycle example of an SPWM waveform, which is a PWM equivalent of a pure sine waveform.
In the above image we see 7 pillars or varying PWM pulses being generated per half wave SPWM cycle. 7 pulses may look good if an iron core transformer is used but the resultant sine waveform might not be pure enough.
Ideally, you must use at least 20 to 30 PWM pulses or pillars on each of these half cycle waveforms, meaning, the higher the number of these pulses, the purer the sine wave will be, and closer it will be to an actual sine waveform.
Features of the Calculator:
- Simple dropdown to select number of SPWM steps
- Calculates values using formula:
PWM = 127.5 + 127.5 × sin(angle)
- Displays the array in Arduino-friendly format
int sineTable[21] = {128, 147, 167, 185, 202, 218, 231, 241, 249, 253, 255, 253, 249, 241, 231, 218, 202, 185, 167, 147, 128};
What It Does:
When a user selects a value like 11, 21, or 31, it will instantly give you the corresponding duty cycle outputs:
int sineTable[21] = {128, 159, 185, 208, …, 128};
You can copy-paste it directly into your Arduino sketch.
Example:
Consider the following Arduino Code of an SPWM generator:
int freq = 50; // Can change to 60
int steps = 21;
int sineTable[21] = {
128, 147, 167, 185, 202, 218, 231, 241, 249, 253, 255, 253, 249, 241, 231, 218, 202, 185, 167, 147, 128};
};
int pwmPinA = 8;
int pwmPinB = 9;
void setup() {
pinMode(pwmPinA, OUTPUT);
pinMode(pwmPinB, OUTPUT);
}
void loop() {
int microDelay = (1000000 / freq) / (2 * steps); // delay per step
for (int i = 0; i < steps; i++) {
analogWrite(pwmPinA, sineTable[i]);
analogWrite(pwmPinB, 0);
delayMicroseconds(50); // Dead time
delayMicroseconds(microDelay);
}
for (int i = 0; i < steps; i++) {
analogWrite(pwmPinA, 0);
analogWrite(pwmPinB, sineTable[i]);
delayMicroseconds(50); // Dead time
delayMicroseconds(microDelay);
}
}
The code is designed to generate 21 pillars or 21 steps, or 21 pulses on each SPWM half cycles. You can easily change the following two values in code in order to change the number of these pulses or pillars, simply by tweaking the following two lines in the code:
int steps = 21;
int sineTable[21] = {
But that does not complete the procedures because then you will also need to change the following corresponding values which represent the PWM duty cycle levels that shape the sine wave.
128, 159, 185, 208, 230, 243, 250, 255, 250, 243,
230, 208, 185, 159, 128
If you don't do this, then your code will not work and not compile.
So that is when you can use the above Sine Table Calculator to calculate these corresponding PWM duty cycle levels, so that your Arduino is able to generate the required SPWM code correctly and validly.
Need Help? Please Leave a Comment! We value your input—Kindly keep it relevant to the above topic!