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
  • 18,264
  • 4
  • 31
  • 49
0

Can anyone tell me where I made a mistake ?

put a lpf on the pwm output.

dannyf
  • 2,813
  • 11
  • 13