Go Down

Topic: SOLVED How to generate SINE PWM SOLVED (Read 66341 times) previous topic - next topic

StanO8


Well  I applied the Fade example last week to my arduino
I just dont understand how to "connect" the sine wave to my pwm signal


The "Fade" example does what you want but instead of a sine wave it uses a triangle wave.

dlloyd

#16
Mar 15, 2014, 06:35 am Last Edit: Mar 15, 2014, 06:43 am by dlloyd Reason: 1
Quote
I need a SINE PWM not only a SINE wave throught DAC...this is my problem and this is what I cant find


@so3ody, just wondering if you're trying to create something on the DAC like the unfiltered red signal here, where 0V would be at the bottom of the image:


MarkT

[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

so3ody

yes exactly this how can I connect the sine wave to my PWM signal. ? My code is shown on page one on this topic..
Thanks in advanc

Grumpy_Mike


yes exactly this how can I connect the sine wave to my PWM signal. ? My code is shown on page one on this topic..
Thanks in advanc

You use the analog write function to write successive values to a pin.
Those values are the  samples of a sin wave.
You are best pre calculating them in a look up table to make the output quicker.

StanO8

Quote



You are best pre calculating them in a look up table to make the output quicker.

Grumpy, 1st rule of programming: Don't optimize or refactor until you have a working algorithm.
In this case, that will not happen any time soon.
:-)

Grumpy_Mike


Quote


You are best pre calculating them in a look up table to make the output quicker.

Grumpy, 1st rule of programming: Don't optimize or refactor until you have a working algorithm.

2nd rule of computing - don't write the same lines over and over again - if you do you simply are not getting it.
So do you want to type in 256 entries in a look up table or do you want to write two lines of code.

Quote
In this case, that will not happen any time soon.

Why, this is a supremely simple problem which would have been solved long ago if the OP had extracted his digit.

so3ody

Well I think I would try to get it work then I will try to improve everything..

Grumpy_Mike

Yes but you are not trying are you?

so3ody

@
Grumpy_Mike  I try.

I can produce a sine wave throught an DAC port.

And I  can generate 3 pwm signals with variable frequency throught pwm ports..

my problem was how to connect the dac sine wave to the pwm signal...

Grumpy_Mike

Quote
Grumpy_Mike  I try.

No you don't.
I told you how to do it in reply #13 but no you would not take it.

Quote
And I  can generate 3 pwm signals with variable frequency throught pwm ports..

Yes but you do not want to do that do you. You want to generate a PWM signal with the duty cycle equal to the current sample of a sin wave.

so3ody

@ Gumpy Mike


I try but I dont have any experience with arduino..

So this is my modified Fade code in order to get a PWM sine wave..

If it will work I will modify it to varii the frequency and duty cycle. but it dosent seem to work..Can you tell me where is my failure ?

Thanks in advance
Code: [Select]
/*
Fade

This example shows how to fade an LED on pin 9
using the analogWrite() function.

This example code is in the public domain.
*/

//This is my sine look up table
int sine[] = {2048,2305,2557,2802,3034,3251,3449,3625,3777,3901,3995,4059,4092,4092,4060,3996,3902,3778,3628,3452,3254,3037,2805,2560,2308,2051,1795,1542,1297,1065,847,649,473,321,197,102,37,4,4,35,99,193,316,466,642,839,1056,1288,1533,1785,2041};


int PWM_PIN = 9;           // This is the output pin
//int brightness = 0;    // how bright the LED is
//int fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
  // declare pin 9 to be an output:
  pinMode(PWM_PIN, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  //Sine loop
  for(int i = 0; i<50;i++){
      if(sine[i]>4095) {
         sine[i]=4095;
  }
 
  // Resolution
  analogWriteResolution(12);
 
 
 
  // set the brightness of pin 9:
// analogWrite(led, brightness);

   analogWrite(led, sine[i]);
    delayMicroseconds(14);

  // change the brightness for next time through the loop:
//  brightness = brightness + fadeAmount;

  /// reverse the direction of the fading at the ends of the fade:
  //if (brightness == 0 || brightness == 255) {
    //fadeAmount = -fadeAmount ;
  }
  // wait for 30 milliseconds to see the dimming effect
  //delay(30);
}

Grumpy_Mike

Yes but that does not even compile does it.
Basically you need to do:-
Code: [Select]
analogWrite(PWM_PIN, sine[i]);

so3ody

sorry I sent the wron version of the code sure you need to set the pins name as PWM_out..So  do yu mean that im on the write way ?

PS: If the code work you need a TP Filter at the output of the Arduino Due before you can see the right signal
http://ww1.microchip.com/downloads/en/AppNotes/00538c.pdf


Thanks in advance

Grumpy_Mike

Quote
So  do yu mean that im on the write way ?


Yes, the sampling of the array look a bit iffy but basically that is right.

Quote
If the code work you need a TP Filter at the output of the Arduino Due before you can see the right signal

What is a TP filter?
Basically you need a LP (low pass) filter, sometimes it is called a restoration filter.

Go Up