I'm tinkering with an application where I like to create a voltage swing, with no dc offset. I figure I'll use a dpdt relay triggered by one of the digital outputs. This way I can get a voltage swing in the interval [-pwm, pwm]. Are there any other neat ways, or lets call it, standard ways, of putting out AC from a Arduino?
I haven't got any device in mind. I'm just 'playing' atm, so I can't answer the frequency, current question.
However, you're right about the sin wave, that's what I like to set up. If I would like to control the frequency I should be able to use one of the analog input to do so I think. I guess I'll filter the signal to get it a bit smoother. Or do you have some other suggestions to how this could be done?
Go to a scrap yard, buy an old car alternator and play with that. You will need something to spin it and you will need to feed the rotor with DC for the field current.
Some audio DACs generate a negative rail internally and can output bipolar analog voltages.
This is done to save needing a large DC-blocking capacitor on the PCB for use in mobile devices.
However you'd need an Arduino that can drive the I2S bus (not the same thing as I2C, note!)
**The "standard solution" for "signals" (such as audio) is an RC- High-pass filter. ** The DC component is zero-Hz so it gets filtered-out. You'll get a voltage that goes from (about) -2.5 to +2.5V (assuming a regular 5V Arduino).
For AC "power" you'd need a different approach.
An H-bridge (or relay) can reverse the connections to reverse a motor (etc.) but the voltage doesn't go negative relative to ground so the motor (etc.) can't have a ground connection.
I should be able to use one of the analog input to do so I think. I guess I'll filter the signal to get it a bit smoother. Or do you have some other suggestions to how this could be done?
The regular Arduino doesn't have a true-analog output. analogWrite() is [u]PWM[/u] which can "simulate" analog to control the speed of a motor or to make an LED appear dim.
If the signal frequency is low compared to the PWM frequency it can be filtered to analog.* This time it's a low-pass filter, so combined you have a bandpass filter.
* Class D audio amplifiers use a kind of PWM in the MHz range. With class D amps, the pulse width is analog so there's no sample rate, just a PWM clock. The frequency is high enough that it (obviously) isn't audible and it doesn't interfere with the audio signal. There's usually an LC filter but some small-cheap class D amps don't bother with filtering. The inductance of the speaker coil does some filtering and the rest is filtered-out mechanically since the speaker cone can't move at MHz rates.
Sinusoidal PWM (SPWM)
Here is a unipolar SPWM approach ... just uses 1 PWM output and a simple RC filter, or get better results with an active filter as shown. The sine will be self-centered at 2.5V (VCC/2).
liquidfuzz:
Edit, seems it is common to set up a 256 table with sin values. Is this 'better' than using the math-sin function multiply with 256 and truncate?
The sin() function on an 8-bit/16 MHz Arduino takes something on the order of 120 microseconds to execute while a table lookup is on the order of a microsecond, so the table look up is very much faster. The table consumes memory space, so "which is better" is a trade of depending upon which resource is more valuable.
When using a relay frequency is limited to a few Hz at most...
Another way to create AC (alternating current) is by connecting a capacitor (blocks DC, passes AC).
You can create a block wave in many different ways, e.g. the PWM output of an Arduino pin or a 555 in astable mode. The first is easier to change in frequency. The analogWrite option as given above may give you a couple hundred Hz sine.
You can quite easily create pure sine waves using an LC oscillator, such as a Clapp or Colpitts circuit. It is quite straightforward to get them to the hundreds of kHz or MHz ranges. I don't know how to make such circuits frequency adjustable, though.