Go Down

Topic: Sinewave generator (Read 1 time) previous topic - next topic

nicksek

Nov 13, 2012, 02:35 pm Last Edit: Nov 13, 2012, 03:40 pm by nicksek Reason: 1
This is my second attempt of a code in order to generate a command voltage to drive a mosfet H bridge configuration which will act as a full wave inverter. I don't know how well this code will work but I'm looking for feedback on it, any help would be greatly appreciated. The delay between each progression of the sine wave is determined by analog command voltage, thus making it variable. As if this works I'm looking to drive an AC motor, and being able to vary the frequency will enable me to control the speed as synchronous speed is = (120*F)/P
where F= frequency and P is poles on the motor.

The pot I'm using to command the time is a 25 turn, so accuracy of this should actually be fairly good.

Thanks again for any feedback.

Nick
Code: [Select]

/* Inverter Full Wave Bridge Inverter -
SINEWave Inteverter operating at A FREQUENCY DETERMINE BY AN ANALOG VOLTAGE
OUTPUT OF THE ANALOG PINS(3,5,6,9) ARE CONNECTED TO CAPACITORS TO SMOOTH THE TRANSISTION
CREATED BY NICK SEK*/

int POT = 3;//Postive waveform ON TOP MOSFET OF BRIDGE RECTIFIER
int POB = 5;//Positive waveform ON BOTTOM MOSFET OF BRIDGE RECTIFIER
int NOT = 6;//Negative Waveform ON TOP MOSFET OF BRIDGE RECTIFIER
int NOB = 9;//Negative Waveform ON BOTTOM MOSFET OF BRIDGE RECTIFIER
int LED = 8;//LED to indicate circuit is running
int sensorPin = A0;// select the input pin for the potentiometer
int sensorValue = 0;// variable to store the value coming from the sensor

void setup(){
pinMode(POT,  OUTPUT);//Enable POT AS AN OUTPUT
pinMode (POB, OUTPUT);//ENABLE POB AS AN OUTPUT
pinMode (NOT, OUTPUT);//ENABLE NOT AS AN OUTPUT
pinMode (NOB, OUTPUT);//ENABLE NOT AS AN OUTPUT
pinMode (LED, OUTPUT);//ENABLE LED AS AN OUTPUT
}
 
void loop(){
 digitalWrite (LED, HIGH);//visual indication that Circuit is running
 sensorValue = analogRead(sensorPin);
 
 //Postive Side of the SineWave//
 
 analogWrite(POT & POB, 0); //ENABLE CURRENT PATH FOR POSITIVE WAVEFORM
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(POT & POB, 50); //sin11.25*255=approx.= 50
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(POT & POB, 100); //sin22.5*255=approx.= 100
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(POT & POB,142); //sin33.75*255=approx.= 142
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(POT & POB,180); //sin45*255=approx.= 180
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(POT & POB,212); //sin56.25*255=approx.= 212
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(POT & POB,236); //sin67.5*255=approx.= 236
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(POT & POB,250); //sin78.75*255=approx.= 250
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(POT & POB,255); //sin90*255 =255
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(POT & POB,250); //sin78.75*255=approx.= 250
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(POT & POB,236); //sin67.5*255=approx.= 236
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(POT & POB,212); //sin56.25*255=approx.= 212
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(POT & POB,180); //sin45*255=approx.= 180
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(POT & POB,142); //sin33.75*255=approx.= 142
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(POT & POB, 100); //sin22.5*255=approx.= 100
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(POT & POB, 50); //sin11.25*255=approx.= 50
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 
  //Negative Side of the SineWave//
 
 analogWrite(NOT & NOB, 0); //ENABLE CURRENT PATH FOR NEGATIVE WAVEFORM
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(NOT & NOB, 50); //sin11.25*255=approx.= 50
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(NOT & NOB, 100); //sin22.5*255=approx.= 100
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(NOT & NOB,142); //sin33.75*255=approx.= 142
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(NOT & NOB,180); //sin45*255=approx.= 180
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(NOT & NOB,212); //sin56.25*255=approx.= 212
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(NOT & NOB,236); //sin67.5*255=approx.= 236
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(NOT & NOB,250); //sin78.75*255=approx.= 250
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(NOT & NOB,255); //sin90*255 =255
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(NOT & NOB,250); //sin78.75*255=approx.= 250
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(NOT & NOB,236); //sin67.5*255=approx.= 236
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(NOT & NOB,212); //sin56.25*255=approx.= 212
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(NOT & NOB,180); //sin45*255=approx.= 180
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(NOT & NOB,142); //sin33.75*255=approx.= 142
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(NOT & NOB, 100); //sin22.5*255=approx.= 100
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
 analogWrite(NOT & NOB, 50); //sin11.25*255=approx.= 50
 delay(sensorValue);//DELAY IS ESTABLISHED BY value from sensor
}


Moderator edit: CODE TAGS AGAIN! People, is it really so hard?
Check out my Electrical Engineering Channel at:

http://www.youtube.com/user/ElectricalNick?feature=mhee

billroy

This code looks good.  It's a little repetitive, though, and that means you have the opportunity to shorten it by using a table (properly, a C array) of sin values and a short function to pick a value from the table and push it to the PWM.

There are lots of examples on the net if you google for "arduino dds"; here's one that shows how you might set up the table in program memory:  http://adrianfreed.com/content/arduino-sketch-high-frequency-precision-sine-wave-tone-sound-synthesis

Good luck with your project.

-br

nicksek

Thanks BR,

New to the programming game and any feedback is great and the idea of saving memory is huge - as I want to extend the ideas of this program, using feedback and a couple variable sensing abilities such as over current for protection... so memory saving is great!

Nick
Check out my Electrical Engineering Channel at:

http://www.youtube.com/user/ElectricalNick?feature=mhee

AWOL

#3
Nov 13, 2012, 03:11 pm Last Edit: Nov 13, 2012, 03:32 pm by AWOL Reason: 1
Code: [Select]
int POT = 3;//Postive waveform ON TOP MOSFET OF BRIDGE RECTIFIER
int POB = 5;//Positive waveform ON BOTTOM MOSFET OF BRIDGE RECTIFIER


Code: [Select]
analogWrite(POT & POB, 100);
3 & 5 = 1

You don't really mean "bridge rectifier", do you?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

dhenry

I have to say that I can see no way that your code will work. Absolutely zero.

You may want to go back to basics before tweaking the code further.

Go Up