0

I'm trying to make AC current (50Hz) with an Arduino Uno. It seems to be not working (or wrong code).

int up;
int down;
int out = 11;
void setup() {
  pinMode(out, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  for (up = 0; up <= 255; up++) {
    analogWrite(out, up);
    delay(1);
  }
  for (down = 255; down >= 0; down--) {
    analogWrite(out, down);
    delay(1);
  }
}

Update:

  1. Now the code above is giving output voltage of 0-5V (getting increased from 0 to 5 and then decreased) not giving a negative cycle (-5V).

  2. After setting the minimum delay of 1, the output wave is still half wave rectifier with frequency 2Hz.

  3. Without delay LED is blinking so fast I don't know what's its frequency.

How do I make make 50Hz AC current a with negative cycle?

  • 1
    The UNO doesn't have a DAC. So you need to filter the PWM output to get an analog AC voltage. – Gerben Jan 5 '16 at 13:58
  • 1
    What you have there (apart from your lack of brackets on the for which breaks it all) is code for a saw-tooth wave (if you filter it) at 1.95Hz. – Majenko Jan 5 '16 at 14:09
  • 1
    ohhh, i thought arudino analogWrite output is sin wave. After watching this arduino.cc/en/Tutorial/PWM it's seems like arduino gives output only in digital wave. – editinit Jan 5 '16 at 14:15
  • 1
    It's digital (as in Pulse Width Modulation) but when filtered (low pass filter) it forms an analog voltage. By setting the duty cycle (analogWrite) in the right way you can form one discrete part of an AC waveform. If you want a sine wave then you have to form a sine wave out of discrete values. – Majenko Jan 5 '16 at 14:50
  • 2
    Normally that is done using a look-up table (LUT) since calculating values of sin is far too slow on a little 8-bit MCU. – Majenko Jan 5 '16 at 14:51
1

I ran your sketch and this is the output signal:

square wave

analogWrite produce a square wave.

As Majenko said, you have to add a low pass filter to get that sine wave.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.