makeshift SPWM

I am building this inverter, and I am a bit stuck with code, the sine wave effective voltage MUST be able to change from a pid program, and I would like the frequency of the simulated wave to also be able to change, but this is just for experimenting purpose. If there is a better way could someone please link me. I am doing this for a college project, all references will be linked in it, and this is the second time I used arduino, the project has a much longer code that works, but the drive part is still missing. I was fiddling with the fade example code and came up with this, can you please have a look before i hook it up to 320V(or a scope XD)…
Could this work, I need a basic functionality nothing fancy yet, interval may be still tweaked to get 100Hz (doing this in halfperiods).

int positive = 5;           // the PWM pins the inverter is attached to
int negative = 6;
int amplitude = 0;                    // momentary amplitude
int fadeAmount = 5;                 // how many points to fade amplitude by
int maxamplitude = 255;            // maximum
int halfperiod =0;                   //indicates what to fire and when to update
unsigned long previousMillis = 0;
unsigned long interval = 10;

void setup() {
  pinMode(positive, OUTPUT);
  pinMode(negative, OUTPUT);
}

void loop() {
  unsigned long currentMillis = millis();                // grab current time 
  if (halfperiod==0){
  digitalWrite(negative, LOW);                          // turn off second H pair:
  analogWrite(positive, amplitude);                    // set the amplitude of first H pair:
  }
  if (halfperiod==1){
   digitalWrite(positive, LOW);                           // turn off first H pair:
  analogWrite(negative, amplitude);                   // set the amplitude of second H pair:
  }
  if (currentMillis - previousMillis >= interval){
    amplitude = amplitude + fadeAmount;     // change the brightness for next time through the loop:
    previousMillis = millis();
  }
 if (amplitude <= 0 ) {                      // reverse the direction of the fading at the ends of the fade:
    amplitude = 0;
    fadeAmount = -fadeAmount;
  } 

   if (amplitude >=maxamplitude )
  {                                            // reverse the direction of the fading at the ends of the fade:
    amplitude = maxamplitude;
    fadeAmount = -fadeAmount;
  }
if (amplitude==0) halfperiod=halfperiod+1;   //detects halfperiods by zero cross

if (halfperiod=2) {
maxamplitude=pidout;                 //not included but here it sets itself from the other part of code
halfperiod=0;                               when full period has passed, then resets counter
} 
}

When i say amplitude i mean the finished filtered wave.
I realize it is triangle shaped, could it be modified by making amplitude = amplitude + fadeAmount; function exponential somehow to make it parabolic, and how. Not experienced with math in this environment. Or is this whole approach for some reason flawed, keep in mind that I don’t require super high resolution or eficiency?

have a look at this webpage.

is this what you are trying to achieve?

sherzaad:
have a look at this webpage.

is this what you are trying to achieve?

Arduino SPWM inverter with full sine output 220V AC tutorial

yes, but i don't see how he can change the duty cycle real time, since he pulls it from a table, and messing with TIMER1 would screw my pid time? no? and I have a current probe, and display with buttons to worry about too.
I need the arduino to act kind of like a VFD but where voltage is regulated and frequency is independent (Variable Voltage Drive?) this should be possible

Semtex9:
yes, but i don't see how he can change the duty cycle real time, since he pulls it from a table, and messing with TIMER1 would screw my pid time? no? and I have a current probe, and display with buttons to worry about too.
I need the arduino to act kind of like a VFD but where voltage is regulated and frequency is independent (Variable Voltage Drive?)

I kinda understand what you are saying (been a while since I dealt with power electronics!) but I still think that code that person did still might be useful for you.

with regard to your code I do not think it would work well as you are using analog write which does not support variable duty cycle so to speak.... it just outputs a PWM which you probably want be have more control on.

so coming back to the example code I found and posted. My approach to integrating it into your application would be to use your PID input to the arduino as a scaling factor to the lookup table... that way you should be able to vary the 'Amplitude' of your sine wave in real time.

if my proposed solution suits your application then next step would be to control the frequency which I do not think would be too complicated to implement (I HOPE!) since that example deals with the arduino timer registers directly.

Hope that helps...

sherzaad:
it just outputs a PWM which you probably want be have more control on.

True

sherzaad:
My approach to integrating it into your application would be to use your PID input to the arduino as a scaling factor to the lookup table... that way you should be able to vary the 'Amplitude' of your sine wave in real time.

if my proposed solution suits your application then next step would be to control the frequency which I do not think would be too complicated to implement (I HOPE!) since that example deals with the arduino timer registers directly.

Hope that helps...

Very interesting solution, one more question pls, would a single UNO be able to handle this, and the interface + PID library? If not, would MEGA cut it.
frequency control is an added bonus, if its too complicated, I dont realy need it anyway.
thank you sherzaad I will look into it more

but if I vary "brightness" of analogwrite in a timed function the duty cycle is bound to change I tought it would be enough. Like dimming to and from max 100Hz fast on 2 channels intermitently xD.

Semtex9:
.... one more question pls, would a single UNO be able to handle this, and the interface + PID library? If not, would MEGA cut it.

depends how big a code you have and how much processing power you require... for the later even a MEGA might stuggle

so answer is "you will just have to compile your code onto the boards to see if either is good enough for your purpose!"

good luck! :slight_smile:

sherzaad:
depends how big a code you have and how much processing power you require... for the later even a MEGA might stuggle

so answer is "you will just have to compile your code onto the boards to see if either is good enough for your purpose!"

good luck! :slight_smile:

thats what I tought... I'll see how it goes thanks