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);
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);
}
}
is it still pulsed DC?
-
1Yes, it is still a square wave, varying between 0V and 5V. You need some extra hardware to do that.user31481– user314812018年02月07日 14:32:15 +00:00Commented Feb 7, 2018 at 14:32
-
1What kind of frequency are you looking for? Do you want a sinewave, triangle wave, saw-tooth wave ... ?Majenko– Majenko2018年02月07日 14:42:02 +00:00Commented Feb 7, 2018 at 14:42
-
just use an opamp to turn your square wave into a sine wave with a few caps and resistorsdandavis– dandavis2018年02月07日 20:44:15 +00:00Commented Feb 7, 2018 at 20:44
1 Answer 1
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.