Go Down

Topic: generating the output waveforms using sine wave (Read 4311 times) previous topic - next topic

nithesh26

Hello all,

I am trying to generate sine wave forms using arduino Uno. I have to update values of n and angle thetha to get the wave forms.
I've tried the code to my knowledge. Can any one help me with this.

int Pin = 11;

void setup()
{
  Serial.begin(9600);
  pinMode(Pin, OUTPUT);
}

void loop()
{
  const float pi = 3.14;
  float deg = 30;
  float rad  = deg * pi / 180;
  float x;
  for (int n = 1; n<= 6; n++)
  x = sin (rad*n - deg);
  Serial.println (x);
  analogWrite(Pin,x);
}

raschemmel


nithesh26

So in order to get sine wave as output should we necessarily require DAC?
Can you suggest few alternatives.

raschemmel

Quote
So in order to get sine wave as output should we necessarily require DAC?

Quote
Can you suggest few alternatives.


There are no alternatives. You get a DAC or you forgot about your idea. It's that simple. I'm not going to go into a discussion of low pass filters and what not to convert a PWM signal with someone who doesn't know the difference between PWM and analog. It's very simple. One is digital and the other is analog. A DAC is called a DAC because it is a Digital to Analog converter.
If you are really determined to use your arduino PWM for a DAC application, then I'm not going to try to stop you. Before you read  the following tutorial on converting your arduino PWM to analog I am just going to tell you that if you can afford $5 (the cost of a sandwich), you can buy an MCP4725 12-BIT DAC.     I just received mine yesterday and generated sinewaves and triangle waves within about 15 minutes after opening the package, about the time it took me to solder the header onto it and turn on my Oscilloscope.
http://www.instructables.com/id/Analog-Output-Convert-PWM-to-Voltage/ 

nithesh26

#4
May 24, 2014, 11:49 pm Last Edit: May 24, 2014, 11:59 pm by nithesh26 Reason: 1
Actually my project is controlling AC motor using the PWM signals which are generated by using the above code.  Also implementing Cordic algorithm and to calculate the time periods. If I get PWM signal as output, that is more than sufficient.


Thank you so much and also can you please verify the code I've written.

Grumpy_Mike

This is what PWM is:-
http://www.thebox.myzen.co.uk/Tutorial/PWM.html

Quote
can you please verify the code I've written.

Yes I can verify it will not work very well, why can't you verify that?

Normally the sin values are pre calculated  either in an array initialized with a table or calculated in the setup. Also printing out sin values while trying to generate a signal will put a variable delay between samples as the value you are printing will have a variable number of digits.

nithesh26

What are the errors and what should I do to eliminate them.?

Can anyone help me with the code.

Grumpy_Mike


What are the errors and what should I do to eliminate them?

I thought I had just told you that?

Grumpy_Mike

This produces two sin waves 90 degrees apart, it is for a Due but you get the idea:-
Code: [Select]

const uint16_t sinTable[] = {
0x7ff, 0x823, 0x846, 0x86a, 0x88e, 0x8b1, 0x8d5, 0x8f9, 0x91c, 0x93f,
0x963, 0x986, 0x9a9, 0x9cc, 0x9ee, 0xa11, 0xa33, 0xa56, 0xa78, 0xa9a,
0xabb, 0xadd, 0xafe, 0xb1f, 0xb40, 0xb60, 0xb81, 0xba1, 0xbc0, 0xbe0,
0xbff, 0xc1e, 0xc3c, 0xc5a, 0xc78, 0xc95, 0xcb2, 0xccf, 0xcec, 0xd08,
0xd23, 0xd3e, 0xd59, 0xd73, 0xd8d, 0xda7, 0xdc0, 0xdd8, 0xdf1, 0xe08,
0xe1f, 0xe36, 0xe4c, 0xe62, 0xe77, 0xe8c, 0xea0, 0xeb4, 0xec7, 0xeda,
0xeec, 0xefe, 0xf0f, 0xf1f, 0xf2f, 0xf3f, 0xf4d, 0xf5c, 0xf69, 0xf77,
0xf83, 0xf8f, 0xf9a, 0xfa5, 0xfaf, 0xfb9, 0xfc2, 0xfca, 0xfd2, 0xfd9,
0xfdf, 0xfe5, 0xfeb, 0xfef, 0xff3, 0xff7, 0xffa, 0xffc, 0xffd, 0xffe,
0xfff, 0xffe, 0xffd, 0xffc, 0xffa, 0xff7, 0xff3, 0xfef, 0xfeb, 0xfe5,
0xfdf, 0xfd9, 0xfd2, 0xfca, 0xfc2, 0xfb9, 0xfaf, 0xfa5, 0xf9a, 0xf8f,
0xf83, 0xf77, 0xf69, 0xf5c, 0xf4d, 0xf3f, 0xf2f, 0xf1f, 0xf0f, 0xefe,
0xeec, 0xeda, 0xec7, 0xeb4, 0xea0, 0xe8c, 0xe77, 0xe62, 0xe4c, 0xe36,
0xe1f, 0xe08, 0xdf1, 0xdd8, 0xdc0, 0xda7, 0xd8d, 0xd73, 0xd59, 0xd3e,
0xd23, 0xd08, 0xcec, 0xccf, 0xcb2, 0xc95, 0xc78, 0xc5a, 0xc3c, 0xc1e,
0xbff, 0xbe0, 0xbc0, 0xba1, 0xb81, 0xb60, 0xb40, 0xb1f, 0xafe, 0xadd,
0xabb, 0xa9a, 0xa78, 0xa56, 0xa33, 0xa11, 0x9ee, 0x9cc, 0x9a9, 0x986,
0x963, 0x93f, 0x91c, 0x8f9, 0x8d5, 0x8b1, 0x88e, 0x86a, 0x846, 0x823,
0x7ff, 0x7db, 0x7b8, 0x794, 0x770, 0x74d, 0x729, 0x705, 0x6e2, 0x6bf,
0x69b, 0x678, 0x655, 0x632, 0x610, 0x5ed, 0x5cb, 0x5a8, 0x586, 0x564,
0x543, 0x521, 0x500, 0x4df, 0x4be, 0x49e, 0x47d, 0x45d, 0x43e, 0x41e,
0x3ff, 0x3e0, 0x3c2, 0x3a4, 0x386, 0x369, 0x34c, 0x32f, 0x312, 0x2f6,
0x2db, 0x2c0, 0x2a5, 0x28b, 0x271, 0x257, 0x23e, 0x226, 0x20d, 0x1f6,
0x1df, 0x1c8, 0x1b2, 0x19c, 0x187, 0x172, 0x15e, 0x14a, 0x137, 0x124,
0x112, 0x100, 0xef, 0xdf, 0xcf, 0xbf, 0xb1, 0xa2, 0x95, 0x87,
0x7b, 0x6f, 0x64, 0x59, 0x4f, 0x45, 0x3c, 0x34, 0x2c, 0x25,
0x1f, 0x19, 0x13, 0xf, 0xb, 0x7, 0x4, 0x2, 0x1, 0x0,
0x0, 0x0, 0x1, 0x2, 0x4, 0x7, 0xb, 0xf, 0x13, 0x19,
0x1f, 0x25, 0x2c, 0x34, 0x3c, 0x45, 0x4f, 0x59, 0x64, 0x6f,
0x7b, 0x87, 0x95, 0xa2, 0xb1, 0xbf, 0xcf, 0xdf, 0xef, 0x100,
0x112, 0x124, 0x137, 0x14a, 0x15e, 0x172, 0x187, 0x19c, 0x1b2, 0x1c8,
0x1df, 0x1f6, 0x20d, 0x226, 0x23e, 0x257, 0x271, 0x28b, 0x2a5, 0x2c0,
0x2db, 0x2f6, 0x312, 0x32f, 0x34c, 0x369, 0x386, 0x3a4, 0x3c2, 0x3e0,
0x3ff, 0x41e, 0x43e, 0x45d, 0x47d, 0x49e, 0x4be, 0x4df, 0x500, 0x521,
0x543, 0x564, 0x586, 0x5a8, 0x5cb, 0x5ed, 0x610, 0x632, 0x655, 0x678,
0x69b, 0x6bf, 0x6e2, 0x705, 0x729, 0x74d, 0x770, 0x794, 0x7b8, 0x7db,
};

void setup() {
 analogWriteResolution(12);
}

void loop() {
static int k=90;
k++;
for(int i=0; i<360; i++) {
k++;
if(k>= 360) k=0;
analogWrite(DAC1, sinTable[i]);
analogWrite(DAC0, sinTable[k]);
delay(1);
}
}


nithesh26

With the code I've written can  I get the digital value.? I need to calculate the value of sine(n*rad-deg) , here n ranges from 1-6 and deg is 30 which I had given as input

raschemmel

Quote
I need to calculate the value of sine(n*rad-deg) , here n ranges from 1-6 and deg is 30 which I had given as input 


So ? Why can't you do it ?

nithesh26

With the code i got, can I get the expected results like mentioned. ?

What are the changes that I should do to calculate the value of sine(n*rad-deg)

int Pin = 11;

void setup()
{
  Serial.begin(9600);
  pinMode(Pin, OUTPUT);
}

void loop()
{
  const float pi = 3.14;
  float deg = 30;
  float rad  = deg * pi / 180;
  float x;
  for (int n = 1; n<= 6; n++)
  x = sin (rad*n - deg);
  Serial.println (x);
  analogWrite(Pin,x);
}

raschemmel

Code: [Select]
analogWriteResolution(12);
What is this ?

Grumpy_Mike


Code: [Select]
analogWriteResolution(12);
What is this ?


What does it look like? It sets the resoloution of an analogue write command to 12 bits. Due only.

Go Up