The I2S Bus
Welcome to the I2S Bus Lab
In this lab, you will learn why the Inter-IC Sound (I2S) bus is the best way to play high-quality audio on the Pico, and you will build a robot sound player!
Why Use I2S?
The Inter-IC Sound (I2S) bus on the Raspberry Pi Pico is a great choice for digital audio projects. Here is why:
1. High-Quality Digital Audio
I2S transfers digital audio without any loss of quality. Analog signals can pick up electrical noise and sound scratchy. I2S keeps the audio in digital form until the very last moment.
2. Low Pin Count
I2S only needs three pins to transmit high-quality audio. That leaves most of the Pico's other GPIO pins free for sensors, buttons, and displays.
3. Stereo Audio Support
The I2S bus can carry stereo audio — a separate left and right channel at the same time. This makes it great for music players, audio recorders, and interactive sound art.
4. Works With Many Audio Chips
Most digital audio chips — Digital-to-Analog Converters (DACs), Analog-to-Digital Converters (ADCs), and audio codecs — support I2S directly. You can connect them to the Pico with just three wires.
5. Less Electrical Interference
Because the audio stays digital until it reaches the DAC chip, it does not pick up the electrical noise that affects analog wires. The result is cleaner sound.
6. Many Sample Rates and Bit Depths
I2S supports audio at different qualities: 16-bit at 8 kHz (good for voice), 16-bit at 44.1 kHz (CD quality), or even 24-bit at 48 kHz (studio quality). The Pico handles all of these.
7. Great for Audio Projects
I2S is perfect for:
- Building audio streaming devices
- Making digital radios or podcast players
- Creating smart speakers or voice assistants
- Building voice-controlled robots
8. Easy in MicroPython
MicroPython's machine.I2S class makes it simple to send and receive I2S audio without writing complex low-level code.
9. Learning Opportunity
Working with I2S teaches you about digital audio processing and how modern audio equipment works. The skills you learn here apply to professional audio devices too.
Key Idea
I2S keeps the audio signal digital until it reaches the DAC chip. Keeping audio digital for as long as possible means less noise and better sound quality.
Robot Sounds Project
Here is a fun project that plays robot sound files on the Raspberry Pi Pico using I2S.
Project: Robot Sound Player
This project uses the Pico with an I2S DAC to play robot sound files (beeps, boops, speech) stored on a microSD card. A button press triggers each sound.
Components Needed
- Raspberry Pi Pico (with headers for easy wiring)
- I2S DAC module (for example, PCM5102)
- MicroSD card module (for storing sound files)
- Speaker (connected to the DAC output)
- Push button or slider switch (to trigger sounds)
- Jumper wires and a breadboard
Steps to Build
1. Prepare the Sound Files
- Convert your robot sound files to 16-bit PCM WAV format at 44.1 kHz.
- Save the WAV files on a microSD card with names like
robot1.wav,robot2.wav.
2. Connect the Components
MicroSD Module to Pico:
- Connect
VCCon the SD module to3.3Von the Pico. - Connect
GNDon the SD module toGNDon the Pico. - Connect
MISOon the SD module toGP16on the Pico. - Connect
MOSIon the SD module toGP19on the Pico. - Connect
SCKon the SD module toGP18on the Pico. - Connect
CSon the SD module toGP17on the Pico.
I2S DAC to Pico:
- Connect
BCKon the DAC toGP10on the Pico. - Connect
LRCKon the DAC toGP11on the Pico. - Connect
DINon the DAC toGP9on the Pico. - Connect
GNDon the DAC toGNDon the Pico. - Connect
VCCon the DAC to3.3Vor5Von the Pico (check your module's label).
Speaker to DAC:
- Connect the
+speaker terminal to the+output on the DAC module. - Connect the
−speaker terminal to the−output on the DAC module.
Button to Pico:
- Connect one side of the button to
GP14on the Pico. - Connect the other side of the button to
GND. - Connect a 10 kΩ pull-up resistor from
GP14to3.3V.
3. Install the Required Software
- Make sure MicroPython is installed on your Pico.
- Upload the I2S audio library (for example,
i2s_audio.py) to the Pico. - Upload the SD card library to the Pico.
- Format the microSD card as FAT32 before copying sound files to it.
4. Write the Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
What Each Section Does
I2S(0, mode=I2S.TX, ...)— sets up I2S peripheral 0 to transmit audio.SPI(0, ...)anduos.mount(...)— connects to the SD card over SPI and mounts it as a drive at/sd.Pin(14, Pin.IN, Pin.PULL_UP)— sets the button pin as an input with an internal pull-up resistor.wav.seek(44)— skips the WAV file header. The first 44 bytes describe the file format, not audio.wav.read(2048)— reads 2 KB of audio at a time to avoid running out of memory.audio_out.write(buf)— sends the audio chunk to the DAC over I2S.if not button.value()— the button readsFalse(LOW) when pressed, because of the pull-up resistor.
5. Test the Setup
- Power up the Pico.
- Press the button to play
robot1.wav. - Swap in different sound files or add more buttons for additional sounds.
Extensions
- Multiple buttons: Add more buttons to trigger different sound effects.
- Random playback: Use the
randommodule to pick a random sound from the SD card. - Sensor triggers: Connect a distance sensor and play a sound when something gets close.
You Can Do This!
Setting up I2S with an SD card can feel like a lot of steps. Take it one step at a time — connect the SD card first, then add the DAC, then add the button. You've got this!
Storing Sounds in the Pico's Built-In Flash Memory
You do not need an SD card for short sound effects. You can store WAV files directly in the Pico's 2 MB of built-in flash memory. Here are the trade-offs:
Considerations
- The Pico has 2 MB of flash, shared with MicroPython and your code. A WAV file at 16-bit, 44.1 kHz uses about 88 KB per second. A file at 8 kHz uses only about 16 KB per second.
- Mono files use half as much space as stereo.
- Copy files to the Pico using Thonny's file manager or the
mpremotecommand-line tool. - Flash memory is a bit slower to read than an SD card, but for short clips this is not a problem.
Steps to Store and Play Sounds From Flash
1. Prepare the Sound Files
Convert your files to a smaller format using ffmpeg:
1 | |
This creates a mono file at 16,000 samples per second.
2. Upload Files to the Pico
- Open Thonny.
- Connect your Pico.
- Use the File menu to upload your WAV file to the Pico's flash memory.
3. Play the Sound From Flash
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
4. Optimize and Test
- Use
uos.listdir()to list the files in flash and confirm your WAV file is there. - Make sure the
ratevalue in the I2S setup matches the sample rate of your WAV file. - If the sound skips or stutters, try a smaller buffer size or a lower sample rate.
Advantages of Using Flash
- No extra hardware needed — just the Pico.
- Simple wiring.
- Good for a few short sound clips in a self-contained project.
Limitations of Using Flash
- Only 2 MB of space, shared with your code and MicroPython.
- Use low sample rates (8 kHz or 16 kHz) to fit more sounds.
- Flash memory wears out after many write cycles, so do not write new files too often.
Monty's Tip
For a small robot with just a few sound effects, flash storage is perfect. For a music player with many songs, use an SD card instead.
Great Work!
You just built a robot sound player using the I2S bus! Your Pico can now play real audio files — not just buzzer beeps.