Sine Wave With ArduinoMega 2560

hello, i'm newbie from indonesia,

i want make a sine wave with arduino Mega2560, i have try, but it failed.
this is my code :

float wave;
const pi = 3.14;
const frequency = 50; // in Hz
const phase = 0;
double time;
int amplitude;
const bias = amplitude;

void setup()
{
pinMode(9,OUTPUT);
}
void loop()
{
amplitude = 5;
time = micros();
wave= amplitude * sin((2 * pi * f* time)+ phase ) + bias;
analogWrite(9,wave);
}

and with script in MATLAB, i haved, but it failed, this the code :

f= input('input frequency = ');

a=arduino('COM3');
a.pinMode(3,'input');
a.pinMode(9,'output');

t=0:.0001:5;

y=5sin(2pift)+5;

a.analogWrite(9,y);

plot(x,y);
ylabel ('Amplitude');
xlabel ('Time Index');

return;

and i have try it with simulink on matlab, but the output wave is like pwm, not sine.

i would control the phase of sine wave.

how it can work as sine wave?

int amplitude;
            const bias = amplitude;

amplitude is zero, therefore bias is also zero.

First of all let's correct some basic parts of the code:

const pi = 3.14;
const frequency = 50; // in Hz

"const" means that the variable you declare, can not be changed, but you need a variable type as well (e.g. const float pi = 3.14;)

int amplitude;
const int bias = amplitude;

In this part you declare 2 variables (amplitude ad bias) on startup amplitude is 0 so bias is as well.

Even though I can correct the faults is your code, it is never possible to make a sinewave in de way you want. This is because the mega only knows 1 and 0. It makes "analog" outputs using PWM and not a real analog voltage.

I have changed the program, like this :
float wave;
const pi = 3.14;
const frequency = 50; // in Hz
const phase = 0;
double time;
void setup()
{
pinMode(9,OUTPUT);
}
void loop()
{
time = micros();
wave= 5 * sin((2 * pi * f* time)+ phase ) + 5;
analogWrite(9,wave);
}

but it still do not work, the output wave from pin 9 on arduino is like spwm, not sine wave.
what's wrong with my code?
there is another code to show sine wave in arduinomega2560?

On this page you can read what analogWrite does.
It puts out a pwm signal on (in your case pin 9) with a dutycycle of wave.
Puting out a correct sine wave is a bit more complicated.

Try to google sine wave using pwm. you wil see that it involves much more then you would expect.

Does your 'code' compile...?
That's the frist requirement and I don't see where it possibly could.. generate a 50Hz sinewave..
There are many examples of sinewave generation available on the web and some on the Arduino Playground, Too.
Might be a good idea to look for sample code than ask why non compilable code won't work..

Doc

When you correct all compilation errors and calculation errors like this:

float wave;
//const pi = 3.14;//ISO C++ forbids generation of'pi' with no type
const double pi = 355.0/113.0;
//const frequency = 50; // in Hz//C++ forbids generation of'frequency' with no type
const double frequency = 50.0;
//const phase = 0;//ISO C++ forbids declaration of 'phase' with no type
const double phase = 0.0;
const int amplitude = 128;
const int offset = 128;
double time;
void setup(){
  pinMode(9,OUTPUT);
}//setup()

void loop(){
  time = micros();
//  wave= 5 * sin((2 * pi * f* time)+ phase ) + 5;//'f' was not declared in this scope
  wave= amplitude*sin((2*pi*frequency*time)+phase)+5;
  analogWrite(9,wave);
}//loop()

you will get an output like the attached one which is a PWM representation of a 50 hz sine wave. If you want an analog sine wave you will have to low pass filter the output.

thanks for all answer,
it's helpfull for me,

i have googling, all of the sine wave using arduinodue (12bit analog data), not mega2560.

nilton61:
When you correct all compilation errors and calculation errors like this:

float wave;

//const pi = 3.14;//ISO C++ forbids generation of'pi' with no type
const double pi = 355.0/113.0;
//const frequency = 50; // in Hz//C++ forbids generation of'frequency' with no type
const double frequency = 50.0;
//const phase = 0;//ISO C++ forbids declaration of 'phase' with no type
const double phase = 0.0;
const int amplitude = 128;
const int offset = 128;
double time;
void setup(){
  pinMode(9,OUTPUT);
}//setup()

void loop(){
  time = micros();
//  wave= 5 * sin((2 * pi * f* time)+ phase ) + 5;//'f' was not declared in this scope
  wave= amplitudesin((2pifrequencytime)+phase)+5;
  analogWrite(9,wave);
}//loop()




you will get an output like the attached one which is a PWM representation of a 50 hz sine wave. If you want an analog sine wave you will have to low pass filter the output.

Hello, sorry i can't understant what is 128 value of amplitude and 5 offset? Can explain me better please?