Emulators

Sound Research (Pt. 1)

by Cory on June 7, 2020 9:24 PM (Edited June 7, 2020 10:45 PM)

I've been thinking about how to add speakers to the RaspberryPi and, unfortunately, it's pretty complicated. I'm going to have to delve hardcore into some electrical/audio engineering and microcontroller work...

Every audio setup consists of at least 4 parts:

  1. The power source
  2. The audio signal source
  3. An amplifier
  4. Speaker(s) (or headphones, but here I want speakers)


Speakers

I bought some tiny speakers from PUI audio an eternity ago when I was in college. I thought I might have to use them for a microcontrollers class, but I never did.

I took a look at the datasheet and the specs are pretty crap... The frequency range, and power requirements aren't ideal. They'll probably suffice for some experimentation but I'm gonna have to put them back in the box when I do the real thing. I already saw some different parts that have much better size and range.

The Power Source

This should be the easy part. I'm just going to use the output power output from the RaspberryPi. It has gpio pins that are a 3.3V and 5V. Gonna try to see if we can make an amplifier that can use one of those voltage levels. Also I heard that the raspberry pi has "noisy" voltage rails and "quiet" ones, so I guess I'll poke around and try to find the quiet one.

The amplifier that you'll read about later works best between 9V and 16V inputs, so that could be a problem. I don't want a handheld to be required to put out that high of voltage, so I'm going to need to switch to a different, lower voltage design.

The Audio Signal Source

This is also covered by the RaspberryPI. It should have been relatively easy, but I'm reading a lot of conflicting/obsolete information out there on the net.

The final plan is to try out the I2S interface, which is robust enough to supply audio out of the raspberry pi and use it in high-fidelity audio applications. The signal is sent out through the I2S protocol, which is in stereo, so I need a decoder + DAC (digital to analog converter) chip to do this. I said "fuck it" at this point and bought one off Adafruit. It still hasn't shipped because of, y'know, coronavirus.

In the meantime, you can also coax audio out of the RaspberryPi through the PWM GPIO pins. The RaspberryPi 3 model has 4 pins you can get PWM output out of. They come in pairs, for stereo audio. Pin 18 and Pin 19 are one pair and then Pin 12 and Pin 13 are a second PWM pair. I think by default Pin 18 is what they expect the user to use for raw PWM output.

I set pin 19 to "ALT5" which apparently causes the audio signal to come out through that pin. Pin 18 and pin 19 work together to provide two channels for stereo audio output. You can do this manually through the command line like so:

gpio mode 25 alt5

The number "25" comes from the GPIO program (a.k.a. WiringPi) internal numbering. This pin is also known by other aliases like "GPIO 19" or "Physical Pin 35".

To make this default upon startup, I need to modify the config.txt:

dtoverlay=pwm,pin=19,fun=2

Also, for some reason, RetroPie interferes with your shit if you try to use pwm output from pins 12 and 13. No idea why, but I've seen people running into this problem and I have too. Just something to be aware of.

EDIT: I did some more digging because this was bothering me. It turns out you can use pwm 12 and 13 successfully but the dtoverlay that comes with the kernel by default doesn't let you do this.

dtoverlay=pwm-2chan,pin=12,fun=2,pin2=13,fun=2

Is what you would put, to assign Pin 12 and 13 ALT0 status, but the driver does not allow you to do that. You can do it manually with the GPIO utility by entering `gpio mode 26 alt0` and `gpio mode 22 alt0` into the command line. You'd need to hack the dtoverlay to allow these values.

And all this is actually moot because for some reason you must commit to either raw PWM output or audio output on ALL 4 pins. If I set the PWM mode of Pin 18 and have audio playing out of 12 and 13, the audio stops. If I absolutely needed to use PWM, I'd give it to the audio and then use a trim pot on the LCD backlight, I guess.

So now that's working for now, all I need to do is take a wire connected to my PWM audio out pin and feed it into my amplifier circuit input.

Amplifier Circuit

Not sure how a noobie would figure this out, but in microcontrollers, the LM386N op amp was a "basic" starting point for audio amplifiers. You can find info about this on like every college electronics course, and on hobbyist websites like this one. I decided to go with the second amplifier design on that page:

I don't think this is good enough for me to use in the final thing. The design calls for 9V source and I forgot how to do the circuit analysis to see how much power is going into the speaker. :( Since the PUI speakers are rated at 8 ohms and 0.7 watts, I decided to just build it and see what happens...

I bought the parts off Digi-Key last week and they came in surprisingly fast. I got to work breadboarding this and tested it using a function generator. (Ha, it's probably not reasonable to expect someone to have something like this lying around but I bought one a few years ago off eBay when I was having a quarter-life crisis. I think it was about $150...)

When driving the speaker with the function generator, the volume wasn't bad. I could twist the knob to sweep through frequencies and make it sound like an old fashion radio tuner. Cool cool cool.

Hooking this amplifier up to the GPIO pin was a little more difficult. Only because the driving power from the GPIO pin doesn't really seem sufficient for this circuit. I think it was really only by dumb luck that I figured out sound was coming out. I needed to really jiggle the trim pots a lot to find a tiiiiiny sliver where the op amp doesn't saturate (or at least I think that's what's happening) before I could hear anything, and even then, it's like an ant whispering into my ear.

It's really hard to hear anything in this video, but it's there. Just barely. It's close now, so I just need to brush up on my electronics again and figure out how to get all the voltage levels and power transfer right to boost the sound output.

I'll probably need to switch to a class D amplifier, I guess? I have no idea what that is, but it's supposed to be way more efficient and low voltage which is perfect for portable applications. Also Adafruit is selling one so that must be the way to go...



This Thought is part of Emulators

Cory tries to make the perfect handheld emulator

back to the

top