Page 1 of 1

Pico audio DAC

Posted: Sat Feb 05, 2022 9:33 am
by sushyoshi
Hello,

I was wondering if anyone here knows if there is any library in micropython for the Raspberry Pi Pico to use a DAC (PCM5102). I want to output sound from a wavetable using the DAC.
So far I have not found any code in micropython to do this, however there is a code for circuitpython that works well:

Code: Select all

import time
import array
import math
import audiocore
import board
import audiobusio

sample_rate = 8000

tone_volume = .1  # Increase or decrease this to adjust the volume of the tone.

frequency = 440  # Set this to the Hz of the tone you want to generate.

length = sample_rate // frequency  # One frequency period

sine_wave = array.array("H", [0] * length)
for i in range(length):
    sine_wave[i] = int((math.sin(math.pi * 2 * frequency * i / sample_rate) *
                        tone_volume + 1) * (2 ** 15 - 1))

audio = audiobusio.I2SOut(board.GP16, board.GP17, board.GP18)

sine_wave_sample = audiocore.RawSample(sine_wave, sample_rate=sample_rate)

audio.play(sine_wave_sample, loop=True)
time.sleep(1)
audio.stop()
time.sleep(1)

It seems micropython doesn't have the AUDIOCORE and AUDIOBUSIO needed to run this code. Does anyone knows if there is any substitute or any suggestions?

Re: Pico audio DAC

Posted: Sat Feb 05, 2022 10:10 am
by Roberthh
There is a machine.i2s class for that purpose, made by @miketeachman. But it might not drive this device, since it requires a System Clock signal, which is not provided by that class, at least not yet. You may try to use PWM to generate the System clock, until it is available.

Re: Pico audio DAC

Posted: Sat Feb 05, 2022 4:56 pm
by Mike Teachman
The PCM5102 device includes a MCK signal input (called SCK on the device and breakout board). This input is not supported in Micropython or Circuitpython. But, but if you ground the SCK pin at the breakout board, the PCM5102 device board will work with both Micropython and Circuitpython. Here are detailed instructions describing how a tone sound can be created with the pico, and the recommended breakout board wiring to the pico.

Does this help?

Re: Pico audio DAC

Posted: Sat Feb 05, 2022 10:51 pm
by scruss
Seconding Mike Teachman's excellent guide for using I2S audio on MicroPython 1.18 and the Pico. It works well with this inexpensive GY-PCM5102 I2S Player Module, which is a lot less complex to wire up than the Adafruit DAC board

Re: Pico audio DAC

Posted: Sun Feb 06, 2022 11:16 am
by sushyoshi
Thanks Mike. That is exactly what I needed. Can confirm that it is working with the cheap chinese module with the PCM5102.
I am working on a project that this is very valuable. Can you tell me what are the advantages of using a DAC over a PWM in terms of sound output? I understand that the DAC will allow a better resolution of sample reproduction, but what about processing power. Do you think using a DAC will be "easier" on the processor than using PWM? I am asking this because I wanted the Pico to be continuously outputting sound while be able to do other thing as well "simultaneously". Sorry if the question is dumb, I am still very new at this.

Thanks

Val

Re: Pico audio DAC

Posted: Mon Feb 07, 2022 5:32 am
by sushyoshi
Hello Mike,

First of all, thank you for your contribution working on this.

I have been going through your example for the sine wave tone and trying to make a use out of it, but so far I have not been successful. Maybe anyone can help me out with the following:

1 - Can we add user input to the tone player? For example, when I press a push button I would like to be able to change the frequency. Looking at your code what I really need is to have a way to change the data in the sound array, continuously checking for user input.

2 - Do you have an example of a non-blocking tone player?

I am trying to make sense of how the I2S is working with my very limited coding knowledge. I am open to any input or suggestions. I am really looking forward to seeing this I2S implementation develop, especially the use of micropython for coding sound engines.

Re: Pico audio DAC

Posted: Mon Feb 07, 2022 6:10 am
by Mike Teachman
sushyoshi wrote:
Sun Feb 06, 2022 11:16 am
Can you tell me what are the advantages of using a DAC over a PWM in terms of sound output?
Sorry, I do not have any experience with PWM techniques for audio applications. But, I can say that I2S with a DAC can produce CD audio quality.
I understand that the DAC will allow a better resolution of sample reproduction, but what about processing power. Do you think using a DAC will be "easier" on the processor than using PWM?
I can not really comment on the PWM processing power, but I2S with a DAC uses very little processing power on a Pico. All the audio sample transfers are done in the background using DMA which has little effect on the processor and the data output bitstream is created with a PIO which also does not use much processing power. The biggest processor load (by far) is reading sample data from a Wav file that is stored on a SD card.

Re: Pico audio DAC

Posted: Tue Feb 08, 2022 8:02 am
by sushyoshi
Hello Mike,

Thank you for your answer.

My focus is to play samples from an array of data, with the ability for the user to interface with the array. Kind of like a wavetable sound synthesizer.

Are you still working on your code? If so I would really appreciate if you could provide an example for playing a tone (like you did with the sine wave) but with the ability of user input to change frequency in real time.

I cannot find any other information about using micropython to achieve this.

Thanks again for your contribution.

Val

Re: Pico audio DAC

Posted: Tue Feb 21, 2023 4:01 am
by AlexYeryomin
Mike Teachman wrote:
Sat Feb 05, 2022 4:56 pm
Does this help?
Mike, yes, it did! I got stuck with PCM5102 using RP2 today, and your detailed instruction helped a lot. Everything works just fine now. Thank you so much!

One note about GY-PCM5102 I2S Player Module recommended above. I got one, and it came without any manual (as usual). I had to solder H1L=L, H2L=L, H3L=H, H4L=L tabs on its back side in order to make it working.