4

Any common Audio output on any electronic device is in essence a two-channel Digital-to-Analog converter, where the voltage sent to each of the channels (Left, Right) powers the electromagnet that drives the movement of the speaker membrane. Instead of sending a cyclic waveform like a sine, you generate a "sound" that is in essence one fixed level, silence but with bias against the "zero value". By gradating that level you can use that signal to other purposes - I did use it, driving a DC motor directly from my ISA SoundBlaster16 card in the past (it had some wicked strong on-board amplifier!)

In more modern hardware though, getting there may be more difficult. Codecs and such will often identify constant signal and replace it with zero on output, or such. How is it done in Raspberry Pi? I don't know, I'm asking.

Can I drive the built-in audio output (jack) directly - bypassing codecs, just tell the chip to output certain voltage level to the audio output which then I would use to drive some external components? I realize I can do PWM but that's not entirely the same thing - I'm interested in pushing specific voltage levels, instead of just a full-voltage signal with specific fill. If it's possible, could you write, how - on the software side?

6
  • I think I read that the Pi is actually using 2 PWM pins from the broadcom chip, for audio. Probably combined with some capacitors and some other components.
    – Gerben
    Commented Aug 2, 2013 at 12:42
  • @Gerben: If that's PWM that gets neatly antialiased into what can be treated as constant levels, that would be satisfactory.
    – SF.
    Commented Aug 2, 2013 at 12:51
  • 1
    In that case you could just use the GPIO pins. Some more info on filtering this PWM output can be found at edn.com/electronics-blogs/bakers-best/4318509/…
    – Gerben
    Commented Aug 2, 2013 at 13:04
  • @Gerben: The article you linked talks about the hardware side, which, if I understand correctly, is already done by Raspberry. Now, how to do that on software side? Software PWM is not viable but there IS the hardware doing PWM (for the audio) - how can I drive it?
    – SF.
    Commented Aug 2, 2013 at 14:30
  • 1
    @Gerben: If you factor in arrangements for international shipping and going 20km by public transport to the parcel service center to pick the DAC chip (nobody home in their work hours) that ceases to be so easy. Meanwhile, we DO have all the hardware in place, there is no need to add any that would double its functions. All we need is to control it in software!
    – SF.
    Commented Aug 2, 2013 at 14:44

1 Answer 1

7
+50

Yes, however, you need to short the two smd capacitors on the back of the PCB right by the Audio output jack (labeled C34 and C48 for me, one for each channel).

You will get an output range from 0-1V. You can keep the sample values constant and get a constant voltage. For example, using a 16bit mode and only the sample value 0 you get a constant 0.5V (0 = mid point in a signed 16bit range).

However, since this was designed for audio, you can't just set an output value like you could on an adc. You need to fill a sample buffer with the sample values that you want. If you want to change the values then you need to wait until the previous buffer has been played.

For source code, see the (in rasperian) included: /opt/vc/src/hello_pi/hello_audio/audio.c

Just change the sample value for each channel to what you want, for example to 0x7FFF for max (1V), use:

for(j=0; j<nchannels; j++)
{
    if (bitdepth == 32)
        *p++ = 0;
    *p++ = 0x7FFF;
}

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.