I need some help, i'm beginner in arduino, and i want to create three sin waves with 50hz of frequency , and 120º offset. Thank you
Is this homework?
I'll assume it is, so here's some pointers:
sinf() to get your basic sin shape. It takes radians so you'll have to scale your input to get your 50Hz frequency.
You'll have to add your offsets in. again need to convert from degrees to radians.
analogWrite() to send the signal out, for three waves you'll need 3 calls and 3 pins.
Yes, but I want to create sine wave and use all values of the sine wave to another equation.
So, just use the values from the table.
tks, i will try!
I'm sorry for my doubts but i can't generate a sin wave i try this:
int Pin = 3;
void setup()
{
Serial.begin(9600);
pinMode(Pin, OUTPUT);
}
void loop()
{
int value = 1* sin(502.0PI);
analogWrite(Pin,value);
}
I want to see the wave form in osciloscope, i connect to Pin 3 but i don't see anything...
sin gives you a value between -1.0 and 1.0.
int value = 1* sin(50*2.0*PI);
analogWrite(Pin,value);
Here you are multiplying 1 (why?! multiplying anything by 1 is pointless) by a value between -1.0 and 1.0. So ignoring anythign else you get a value of -1 to 1. AnalogueWrite takes a value between 0-255.
You use an int to store that value. Sin returns a double, so you've lost all your precision.
The value you pass in to sin should be variable over time, you are passing in a constant expression, so you'll get a constant return value.
This should give you a decent starting point to at least get some waveform on your scope:
double value = sin(millis());
value *= 255.0;
value += 128.0;
analogWrite(Pin,value);
Just put the 1 to better visualize the amplitude.I have tested this code, and I'm getting a square wave.
tammyTam:
You do know that analogWrite() is pwm based and not a DAC, right?
He will be very disappointed if he follows your advice.
ricperes:
Just put the 1 to better visualize the amplitude.I have tested this code, and I'm getting a square wave.
AnalogWrite() is pwm based. you will need to filter it to get a sine wave.
thanks for your help, but Now I want to make use of all the sine values over a period of time to another equation, can you help me?
See reply #3
I read the answer but not yet understood what to do, I apologize!
KeithRB:
tammyTam:You do know that analogWrite() is pwm based and not a DAC, right?
He will be very disappointed if he follows your advice.
And this is precisely why I should stick to the programming forum and not venture out haha. Sorry OP, like its been said, mine will produce a square wave and you need to filter it down with a cap + resistor to get your waveform.
The most efficient way to produce a sine wave is with a recursion equation.
charliesixpack:
The most efficient way to produce a sine wave is with a recursion equation. I threw this together.
What grade do you think he will get for your code?
Chuck.
Check out my Kickstarter Project Memory Panes an expansion RAM Shield for Mega2560's. It adds 1MB of RAM for those projects where 8KB is not enough.
Hi,
Sorry but the first question should be.
What model arduino are you using?
Some have DAC output, others have only PWM.
Do you want three 50Hz analog waveforms 120Deg apart to be on three outputs of the arduino?
You say you want to use the values in an equation elsewhere, do you want the values as an analog waveform or as a string of numbers representing the result of the Sine function?
Hi, what is your electronics, programming, arduino, hardware experience?
What do you need the 3 phase output for, what is the application?
Tom...... ![]()
That's not recursive, it is iterative.
KeithRB:
That's not recursive, it is iterative.
It is a recursive filter. The sine wave is the impulse response of an unstable recursive filter. The output of one cycle becomes an input of the next cycle which is the definition of recursion.
Read this
3-phase sine wave generator(pwm)
The generator has 3 outputs: out1, out2, out3. It also has 2 inputs:
const byte out1 = 11; // PWM pin
const byte out2 = 10; // PWM pin
const byte out3 = 9; // PWM pin
const float pi = 3.1415;
float x = 0;
const byte enable = 8;
const byte freqpin = A0; // Freq adjust pot
unsigned char p1 = 0;
unsigned char pp1 = 0;
unsigned char pp2 = 0;
unsigned char pp3 = 0;
unsigned char p2 = 0;
unsigned char p3 = 0;
const float y = pi/30;
const float ph1 = 2*pi/3;
const float ph2 = 4*pi/3;
const float ph3 = 2*pi;
void setup() {
pinMode(out1, OUTPUT);
pinMode(out2, OUTPUT);
pinMode(out3, OUTPUT);
pinMode(enable, INPUT);
pinMode(freqpin, INPUT);
}
void loop() {
while(digitalRead(enable) == HIGH) {
x = x + y;
analogWrite(out1, p1);
analogWrite(out2, p2);
analogWrite(out3, p3);
pp1 = 126*sin(x+ph1);
p1 = pp1+128;
pp2 = 126*sin(x+ph2);
p2 = pp2+128;
pp3 = 126*sin(x+ph3);
p3 = pp3+128;
if(x >= 2*pi) x = 0;
delay(analogRead(freqpin));
}
analogWrite(out1, 0);
analogWrite(out2, 0);
analogWrite(out3, 0);
x = 0;
}