# Playing audio files in a Pi Pico without a DAC

DevFeed: [Playing audio files in a Pi Pico without a DAC](<https://devfeed.tech/articles/playing-audio-files-in-a-pi-pico-without-a-dac-20634.md>)

Original publisher: [Read original article](<http://antirez.com/news/143>)

Author: Antirez

Published: 2024-03-06T10:52:34Z

Content type: tutorial

Language: en

Sources: [Antirez](<https://devfeed.tech/sources/antirez.md>)

Topics: [MicroPython](<https://devfeed.tech/topics/micropython.md>)

Tags: [audio](<https://devfeed.tech/tags/audio.md>), [build](<https://devfeed.tech/tags/build.md>), [build-system](<https://devfeed.tech/tags/build-system.md>), [cmake](<https://devfeed.tech/tags/cmake.md>), [connectivity](<https://devfeed.tech/tags/connectivity.md>), [dac](<https://devfeed.tech/tags/dac.md>), [data-sheet](<https://devfeed.tech/tags/data-sheet.md>), [embedded](<https://devfeed.tech/tags/embedded.md>), [file](<https://devfeed.tech/tags/file.md>), [files](<https://devfeed.tech/tags/files.md>), [mcu](<https://devfeed.tech/tags/mcu.md>), [micropython](<https://devfeed.tech/tags/micropython.md>), [pico](<https://devfeed.tech/tags/pico.md>), [pin](<https://devfeed.tech/tags/pin.md>), [play](<https://devfeed.tech/tags/play.md>), [pwm](<https://devfeed.tech/tags/pwm.md>), [raspberry](<https://devfeed.tech/tags/raspberry.md>), [sdk](<https://devfeed.tech/tags/sdk.md>), [sound](<https://devfeed.tech/tags/sound.md>), [square](<https://devfeed.tech/tags/square.md>)

## AI overview

A Raspberry Pi Pico can approximate audio output without a DAC by using high-frequency PWM: varying the duty cycle changes the pin's average voltage to represent successive samples. The excerpt converts a WAV file to unsigned 8-bit raw PCM at 24 kHz with ffmpeg, copies it to the board with mpremote, then reads it in 4096-byte blocks and maps each sample to PWM with `sample<<8`. The MicroPython loop occupies the program while playing, and its playback rate is not precisely synchronized to the file's sample rate; the author adds repeated `x=1` statements to adjust the pitch by ear. The author reports decent results with 8-bit audio and notes 16-bit conversion could be tried. A short bonus example builds and repeatedly outputs a sine wave using PWM.

## Source excerpt

The Raspberry Pico is suddenly becoming my preferred chip for embedded development. It is well made, durable hardware, with a ton of features that appear designed with smartness and passion (the state machines driving the GPIOs are a killer feature!). Its main weakness, the lack of connectivity, is now resolved by the W variant. The data sheet is excellent and documents every aspect of the chip. Moreover, it is well supported by MicroPython (which I'm using a lot), and the C SDK environment is decent, even if full of useless complexities like today fashion demands: a cmake build system that in turn generates a Makefile, files to define this and that (used libraries, debug outputs, ...), and in general a huge overkill for the goal of compiling tiny programs for tiny devices. No, it's worse than that: all this complexity to generate programs for a FIXED hardware with a fixed set of features (if not for the W / non-W variant). Enough with the rant about how much today software sucks, but it must be remembered. One of the cool things one wants to do with an MCU like that, is generating some sound. The most obvious way to do this is using the built-in PWM feature of the chip. The GPIOs can be configured to just alterante between zero and one at the desired frequency, like that: from machine import Pin, PWM pwm = PWM(Pin(1)) pwm.freq(400) pwm.duty_u16(1000) Assuming you connected a piezo to GND and pin 1 of your Pico, you will hear a square wave sound at 400hz of frequency. Now, there are little sounds as terrible to hear as square waves. Maybe we can do better. I'll skip all the intermediate steps here, like producing a sin wave, and directly jump to playing a wav file. Once you see how to do that, you can easily generate your own other waves (sin, noise, envelops for such waveforms and so forth). Now you are likely asking yourself: how can I generate the complex wave forms to play a wav file, if the Pico can only switch the pin high or low? A proper non square waveform is