# Making AC current with pin 11

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?

• The UNO doesn't have a DAC. So you need to filter the PWM output to get an analog AC voltage. Commented Jan 5, 2016 at 13:58
• 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. Commented Jan 5, 2016 at 14:09
• 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
• 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. Commented Jan 5, 2016 at 14:50
• 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. Commented Jan 5, 2016 at 14:51

`analogWrite` produce a square wave.