Ran out of DACs - need ideas producing audio output from MCU - Page 1
Forum section: electronics / beginners

author: bitman
author_rank: Unknown
timestamp: October 18, 2021, 08:57:01 pm
content: I have a small ESP32 project which is connected to a HUB75 LED display and that takes care of about every interesting pin at least from a DAC/ADC perspective. Problem is, I would like to generate a bit of sound (for a kids game). The "old" obvious way is to use the build in DACs which the ESP32 has two of, but they're both taken by the default HUB75 connectors. I have a bit of a task to figure out if they're taken because they're DACs or because they're pins, but beside that - I am looking for a way to generate audio sound waves in the 1-5kHz range.I'm looking for ideas to do this. I tried an external DAC that uses i2c but boy, that's slow and not even close to fun (at 9 bit resolution I don't even get 10Hz!). And for some reason it looks like my MCP4725 "turns off" the output when new data is sent - perhaps my "off the shelf" converter just isn't up to this task. I was thinking of using PWM to generate a sound wave but that seems to require yet another MCU and that sorta kills the idea. So I need ideas.My next experiment is setting up a timer and use a digital pin out, even though that's a square wave that may be "good enough for 4 years old". But in the mean time I was thinking I may be overlooking the obvious here, so I wanted to ask.At this point I may simply go with 555 and a fixed frequency I can turn on and offBut it would really be nice if I could have a small tune or speech from a WAV played.

author: james_s
author_rank: Super Contributor
timestamp: October 18, 2021, 10:36:15 pm
content: Seems a bit silly to use a 555 if you've got a microcontroller. A digital pin on the micro can do pretty much anything a 555 can. On FPGAs I typically use a delta-sigma DAC but I don't know if that's feasible on a microcontroller.

author: ve7xen
author_rank: Super Contributor
timestamp: October 18, 2021, 10:49:04 pm
content: ESP32 has hardware PWM? I would use that.HUB75 also seems to be a purely digital device so it likely wouldn't be hard to move it to other pins.

author: langwadt
author_rank: Super Contributor
timestamp: October 18, 2021, 10:52:42 pm
content: Quote from: ve7xen on October 18, 2021, 10:49:04 pmgoogle to the rescue:https://deepbluembedded.com/esp32-pwm-tutorial-examples-analogwrite-arduino/

author: brucehoult
author_rank: Super Contributor
timestamp: October 18, 2021, 11:47:10 pm
content: A few years ago now I did an experiment using a single GPIO to output sound. I even used the awful Arduino digitalWrite() to do it rather than manipulating the port directly.It worked pretty well right off, and could probably be improved with a bit of work.That's using the CPU to turn the GPIO on and off every single time, but if you have a PWM you'd only have to change the PWM setting at 22 kHz i.e. every 45 us.

author: ledtester
author_rank: Super Contributor
timestamp: October 19, 2021, 12:38:20 am
content: For that vintage arcade game sound you could use an old sound synthesizer chip:SAA1099 & Arduino - How to DIY -- Jacob Fieldhttps://youtu.be/0UxQgYc3ELU

author: augustol
author_rank: Contributor
timestamp: October 19, 2021, 02:29:22 am
content: Have you seen the ESP8266Audio github? I'm actually researching to see if I can get the ESP32 to output a PWM for each channel of a bluetooth received audio for a project. I've seen this, but haven't had the time to go in deeper. I want to check if I can transform the OutputWAV option in a PWM output.Don't know if it will be helpful, but might be interesting to take a look.https://github.com/earlephilhower/ESP8266Audio

author: radiolistener
author_rank: Super Contributor
timestamp: October 19, 2021, 07:24:36 am
content: You can connect external audio codec to STM32. On the other hand, you can connect some programmable sound generator which was very popular in 80-90 for the music and sound effects in computer games. Such as AY8910 orYamaha YM2149. It has 3 channels which you can mix into stereo. Each channel allows to setup it's own frequency or noise, or both together, also each channel has 16 bit DA converter for volume, also it allows to switch volume to be controlled with one of 16 predefined waveform with programmable period. PSG requires much less resources than using PCM audio to generate sound effects and music in the game.The player for PSG chip just needs to execute hundred asm opcodes every 20 ms to get a music like this:typical music size in the memory is about 2-4 kB, and there are a tons of music for PSG chips, you can download it from internet.Here is how it's channels output (before stereo mixer) looks on oscilloscope:

author: bitman
author_rank: Unknown
timestamp: October 19, 2021, 04:40:20 pm
content: Quote from: augustol on October 19, 2021, 02:29:22 amI'm trying to move to the ESP32 to gain the performance boost and memory it provides; that said I do like my old Wemo D1 minis. For ESP32 I have seen thishttps://diyi0t.com/i2s-sound-tutorial-for-esp32/which I may end up with since it bypassed the DAC and takes care of the low resolution of the DAC too). While I'm definitely not a fan of break-out boards, it will do for a prototype right now.PWM never really seemed like a real solution. It too is just 8 bit and I don't think it's precision is good enough for sound generation even if 8 bit would work.

author: bitman
author_rank: Unknown
timestamp: October 19, 2021, 04:46:44 pm
content: Quote from: ve7xen on October 18, 2021, 10:49:04 pmThat's what I'm coming to too - the only drawback is that I would need to customize a "standard library" but that's not a biggie. I'll update here once I've done some testing. That said, the onboard DACs may not really be an option if they are 8 bit only (according to specs they are).

author: bitman
author_rank: Unknown
timestamp: October 19, 2021, 04:48:17 pm
content: Quote from: james_s on October 18, 2021, 10:36:15 pmYes and no. It would free up the MCU from having to loop/interrupt while updating a screen at 10-20FPS, and instead I would just have an "enable" digital pin that would turn on/off sound. But yeah, it's a bit simple and not what I really want.

author: ve7xen
author_rank: Super Contributor
timestamp: October 19, 2021, 06:19:31 pm
content: Quote from: bitman on October 19, 2021, 04:46:44 pm8-bit is fine for basic sound effects; at least considerably better than the square wave that you were considering.I believe the PWM peripheral offers up to 16-bit, but resolution will trade off against PWM frequency (which is typically the peripheral clock / <2^BIT_DEPTH>), and you probably don't have a fast enough clock available to run it at 20KHz. But maybe you can get 12-bit @ ~20KHz (80MHz / 2^12 = 19531) which should give you something like 8KHz of audio bandwidth depending on your filters etc.If you want 'high fidelity' you will need a dedicated audio DAC. Either of the above options is probably fine for the use case you're describing though.

author: ledtester
author_rank: Super Contributor
timestamp: October 20, 2021, 02:07:04 am
content: I've read that the ESP32 supports I2S (with DMA), so maybe you could use that and a I2S amp, e.g.:https://www.adafruit.com/product/3006

author: james_s
author_rank: Super Contributor
timestamp: October 22, 2021, 12:20:27 am
content: Another option if you want something simple and versatile is one of those ICs that can play back digitized sounds. I think some of them will play MP3 or WAV files right off a SD card and they are controllable via a simple interface. Then you can use whatever sounds you want, and update them easily at any time.