Pages: [1]   Go Down
Author Topic: Sinewave generator  (Read 885 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/* 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?
« Last Edit: November 13, 2012, 09:40:13 am by nicksek » Logged

Check out my Electrical Engineering Channel at:

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

0
Offline Offline
God Member
*****
Karma: 39
Posts: 986
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Check out my Electrical Engineering Channel at:

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 241
Posts: 24481
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int POT = 3;//Postive waveform ON TOP MOSFET OF BRIDGE RECTIFIER
int POB = 5;//Positive waveform ON BOTTOM MOSFET OF BRIDGE RECTIFIER

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

You don't really mean "bridge rectifier", do you?
« Last Edit: November 13, 2012, 09:32:16 am by AWOL » Logged

"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.

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

No sorry I don't mean rectifier, just in a H bridge configuration... Thanks for seeing that error for me. I'll edit it right away.

And no way it it will work ? Sorry misleading me here, can you give a hint as to why or is it not even worth saving a single bit?
Logged

Check out my Electrical Engineering Channel at:

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 241
Posts: 24481
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you understand the implications of the bitwise AND operation I pointed out in reply #3?
Do you understand PWM?
« Last Edit: November 13, 2012, 09:45:02 am by AWOL » Logged

"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.

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
can you give a hint as to why or is it not even worth saving a single bit?

An H-bridge is essentially two switches that link the output to either the rail or ground. So your top mosfet and your bottom mosfet receive the same signal, 180degrees opposite.

If both of them are on, you short the rail to ground - a very bad thing;

If both of them are off, you have tri-stated the output - a very bad thing if you are looking to output some serious power to a load.

If you want, forget about an external h-bridge, use your output pin which is driven by an internal h-bridge and work out your code first.

Again, there is zero chance your code will work.
Logged

Pages: [1]   Go Up
Jump to: