• Skip to main content
  • Skip to primary sidebar

Homemade Circuit Projects

Need circuit help? Post them in the comments! I've answered over 50,000!

Blog | Categories | About | Contact | Calculators-online
You are here: Home / Sine Table Calculator for SPWM Arduino Code

Sine Table Calculator for SPWM Arduino Code

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.

SPWM pillars

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
Calculator by homemade-circuits.com
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);
  }
}
SPWM-code for ArduinoDownload

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.

Reader Interactions

Need Help? Please Leave a Comment! We value your input—Kindly keep it relevant to the above topic! Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar




Subscribe to New Posts

Categories

  • Arduino Projects (89)
  • Audio and Amplifier Projects (132)
  • Automation Projects (17)
  • Automobile Electronics (101)
  • Battery Charger Circuits (83)
  • Datasheets and Components (104)
  • Electronics Theory (143)
  • Free Energy (37)
  • Games and Sports Projects (11)
  • Grid and 3-Phase (19)
  • Health related Projects (25)
  • Home Electrical Circuits (12)
  • Indicator Circuits (14)
  • Inverter Circuits (88)
  • Lamps and Lights (142)
  • Meters and Testers (69)
  • Mini Projects (46)
  • Motor Controller (64)
  • Oscillator Circuits (27)
  • Pets and Pests (15)
  • Power Supply Circuits (108)
  • Remote Control Circuits (50)
  • Security and Alarm (64)
  • Sensors and Detectors (101)
  • Solar Controller Circuits (59)
  • Temperature Controllers (42)
  • Timer and Delay Relay (49)
  • Transmitter Circuits (29)
  • Voltage Control and Protection (39)
  • Water Controller (36)




Other Links

  • Privacy Policy
  • Cookie Policy
  • Disclaimer
  • Copyright
  • Videos
  • Sitemap




People also Search

555 Circuits | 741 Circuits | LM324 Circuits | LM338 Circuits | 4017 Circuits | Ultrasonic Projects | SMPS Projects | Christmas Projects | MOSFETs | Radio Circuits | Laser Circuits | PIR Projects |

Social Profiles

  • Twitter
  • YouTube
  • Instagram
  • Pinterest
  • My Facebook-Page
  • Quora
  • Stack Exchange
  • Linkedin



  • Recent Comments

    • Swagatam on How to Make HHO Fuel Cell Circuit in Automobiles for better Fuel Efficiency
    • Swagatam on How to Make HHO Fuel Cell Circuit in Automobiles for better Fuel Efficiency
    • eq on How to Make HHO Fuel Cell Circuit in Automobiles for better Fuel Efficiency
    • Swagatam on How to Make HHO Fuel Cell Circuit in Automobiles for better Fuel Efficiency
    • eq on How to Make HHO Fuel Cell Circuit in Automobiles for better Fuel Efficiency

    © 2025 · Swagatam Innovations