1

I'm trying to generate a sine wave on my Arduino micro. I used this code.

int Pin = 9;

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

void loop()
{
 float something = millis()/10000.0;
 int value = 128.0 + 128 * sin( something * 2.0 * PI  );
 analogWrite(Pin,value);
}

I have a low pass filter at the output of the pin to smooth the wave. The problem is : I get a nice square wave with a duty cycle from 0 to 100%, but not a sine wave. Ideally, I'd like to set a frequency of 10 Hz. Can anyone tell me where I made a mistake ? Thanks a lot !!!

Ultra67
  • 111
  • 2

2 Answers2

1

it is how PWM works. digital pins can do only 1 and 0.

Juraj
  • 17,050
  • 4
  • 27
  • 43
  • So it's not possible to do a sine wave? – Ultra67 Aug 30 '17 at 09:43
  • Maybe use the PWM to charge a capacitor? You can balance charge and discharge with resistors to possibly get a pretty sinusoidal voltage on it. – Filip Franik Aug 30 '17 at 10:07
  • 1
    I found you a tutorial that should help you to create a circuit that changes a square wave to sinusoidal wave. All you need to do is to output from your arduino 0 for 50ms and 1 for 50ms (to get 10Hz) and adjust the capacitor sizes to get a sin wave. (Tutorial shows circuit working for 10kHz square wave) http://www.learningaboutelectronics.com/Articles/Square-to-sine-wave-converter-circuit.php – Filip Franik Aug 30 '17 at 10:18
0

Can anyone tell me where I made a mistake ?

put a lpf on the pwm output.

dannyf
  • 2,730
  • 9
  • 13