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);

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


  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
    Commented Jan 5, 2016 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
    Commented Jan 5, 2016 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. Commented Jan 5, 2016 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
    Commented Jan 5, 2016 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
    Commented Jan 5, 2016 at 14:51

2 Answers 2


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.


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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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