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. – koolwithk 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.

| improve this answer | |
0

If you are not critical on the waveform you can use a transformer where the secondary has a center tap. The primary should have one as well. Tie the input center tap to +12 or whatever is available but DO NOT use the arduino 5 volt pin. Then using two avalanche protected N channel MOSFETs you them to drive the remaining two input leads. The output will be a pseudo sine wave, far from perfect but it will have the negative excursion you want.This type of circuirity can be found in Switch Mode Power Supplies (SMPS). Note the transformer winding ratio will determine your output voltage. You will need a little dead time between turning one MOSFET off and the Other on. A single mosfet would work but there would only be two connections to the transformer primary not 3 and the wave form will be a lousy.

| improve this answer | |
  • You do realize you're answering a more than 4 year old question, I hope. Your answer would benefit greatly from a schematic. – StarCat Sep 23 at 16:05

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.