I am trying to create an AC voltage in Arduino. Until now I have only managed to get a square wave. How can this be achieved with Arduino?

If I try:

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

is it still pulsed DC?

  • 1
    Yes, it is still a square wave, varying between 0V and 5V. You need some extra hardware to do that.
    – user31481
    Feb 7 '18 at 14:32
  • 1
    What kind of frequency are you looking for? Do you want a sinewave, triangle wave, saw-tooth wave ... ?
    – Majenko
    Feb 7 '18 at 14:42
  • just use an opamp to turn your square wave into a sine wave with a few caps and resistors
    – dandavis
    Feb 7 '18 at 20:44

You have a few options:

  • Use a low-pass filter to round the square wave's edges some so they look more AC-ish.

  • Use a low-pass filter and have your code create steps of voltage that look AC-ish by varying the PWM widths.

  • Use an external SPI DAC chip that you can control with more discrete levels to make smoother looking AC.

  • Look into Direct Digital Synthesis (DDC) to learn more. Google "analog via ddS", lots of hits.

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.