# 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>)

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

Content type: tutorial

Language: en

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

Topics: [Microcontroller](<https://devfeed.tech/topics/microcontroller.md>), [MicroPython](<https://devfeed.tech/topics/micropython.md>), [Hardware](<https://devfeed.tech/topics/hardware.md>), [Software](<https://devfeed.tech/topics/software.md>)

Tags: [audio](<https://devfeed.tech/tags/audio.md>), [devices](<https://devfeed.tech/tags/devices.md>), [embedded](<https://devfeed.tech/tags/embedded.md>), [mcu](<https://devfeed.tech/tags/mcu.md>), [micropython](<https://devfeed.tech/tags/micropython.md>), [pwm](<https://devfeed.tech/tags/pwm.md>)

## AI overview

A tutorial explores playing audio files on a Raspberry Pi Pico without a DAC. It introduces PWM-generated sound, notes the limitations of square waves, and moves toward generating more complex waveforms for WAV playback.

## 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