# Adafruit I2S Audio Bonnet for Raspberry Pi 

## Overview

Add some easy-listenin' tunes to your Raspberry Pi using this basic audio bonnet. It'll give you stereo line out from a digital I2S converter for a good price, and sounds nice to boot! This bonnet features the UDA1334A I2S Stereo DAC, a perfect match for any I2S-output audio interface. It's affordable but sounds great - music to our ears.

![](https://cdn-learn.adafruit.com/assets/assets/000/066/505/medium800/adafruit_products_4037_iso_ORIG_2018_11.jpg?1543380758)

It's the exact same size as a Raspberry Pi Zero but works with **any and all** Raspberry Pi computers with a 2x20 connector - A+, B+, Zero, Pi 2, Pi 3, etc. We've tested it out with Raspbian (the official operating system) and Retropie.

![](https://cdn-learn.adafruit.com/assets/assets/000/066/508/medium800/adafruit_products_4037_top_ORIG_2018_11.jpg?1543380776)

This Bonnet uses I2S a digital sound standard, so you get really crisp audio. The digital data goes right into the amplifier so there's no static like you hear from the onboard headphone jack. And it's super easy to get started. Just plug in any line level output into powered speakers or audio system. You _can_ plug in headphones but you'll get some distortion - so its best used into something that is expecting line level out

![](https://cdn-learn.adafruit.com/assets/assets/000/066/507/medium800/adafruit_products_4037_side_detail_ORIG_2018_11.jpg?1543380771)

Each order comes as a fully tested and assembled&nbsp;PCB with a 2x20 socket connector. No soldering required! Plug it right onto your pi, install the script and reboot. Pow!

![](https://cdn-learn.adafruit.com/assets/assets/000/066/506/medium800/adafruit_products_4037_quarter_ORIG_2018_11.jpg?1543380767)

# Adafruit I2S Audio Bonnet for Raspberry Pi 

## Pinouts

![](https://cdn-learn.adafruit.com/assets/assets/000/066/509/medium800/adafruit_products_front.jpg?1543381760)

# I2S Audio Pins

The UDA1334A is an **I2S** amplifier - it does not use analog inputs, it only has digital audio input support! Don't confuse I2S with I2C, I2S is a sound protocol whereas I2C is for small amounts of data.

There are 3 required data pins used on the Pi for I2S Audio, and 2 pins that are optional but good to know about.

![](https://cdn-learn.adafruit.com/assets/assets/000/066/510/medium800/adafruit_products_pins.jpg?1543381822)

Required pins

- **Pin #18** - I2S Clock
- **Pin #19** - I2S LR Clock (also sometimes called WS Clock)
- **Pin #21** - I2S Data Into the amplifier (out from Pi)

Optional pins

- **Pin #16** - Mute pin, you can use this to mute the output complete, sometimes helpful if you want to keep audio running and then turn it on/off with a switch or button.
- **Pin #20** - I2S Data Out from a microphone (into the Pi)

![](https://cdn-learn.adafruit.com/assets/assets/000/066/511/medium800/adafruit_products_back.jpg?1543381986)

You don't have to do anything with that info other than know that pins 18, 19, 21 are not available for to use and pin #16 is by default used for muting. Pin #20 isnt used by the bonnet, but its handy if you plan to connect some other I2S microphone in.

# Audio Output

The output from the DAC is into a standard 3.5mm stereo headphone jack. Output is 'live level' (approx 1Vpp). You _can_ plug in a set of headphones but the output isn't designed for driving low impedance so you will get some distortions at loud volume. The output is fully AC coupled with 47uF capacitors, sleeve ground is connected to the Pi ground.

![](https://cdn-learn.adafruit.com/assets/assets/000/066/512/medium800/adafruit_products_output.jpg?1543382170)

There's also a breakout slot for soldering in dual/stereo RCA jacks, not included with the bonnet.

Or you can just use one of these cables:

# Adafruit I2S Audio Bonnet for Raspberry Pi 

## Raspberry Pi Setup

Warning: 

Danger: 

# Setup Virtual Environment

If you are installing on the Bookworm version of Raspberry Pi OS or later, you will need to install your python modules in a virtual environment. You can find more information in the&nbsp;[Python Virtual Environment Usage on Raspberry Pi](https://learn.adafruit.com/python-virtual-environment-usage-on-raspberry-pi)&nbsp;guide. To Install and activate the virtual environment, use the following commands:

```terminal
sudo apt install python3-venv
python -m venv env --system-site-packages
```

To activate the virtual environment:

```terminal
source env/bin/activate
```

# Installer script

Luckily its quite easy to install support for I2S DACs on Raspbian.

[These instructions are totally cribbed from the PhatDAC instructions at the lovely folks at Pimoroni!](http://learn.pimoroni.com/tutorial/phat/raspberry-pi-phat-dac-install)

Run the following from your Raspberry Pi with Internet connectivity:

```terminal
sudo apt install -y wget
pip3 install adafruit-python-shell
wget https://github.com/adafruit/Raspberry-Pi-Installer-Scripts/raw/main/i2samp.py
sudo -E env PATH=$PATH python3 i2samp.py
```

![](https://cdn-learn.adafruit.com/assets/assets/000/129/696/medium800/adafruit_products_quickinstall.png?1714405053)

We've added an extra helper systemd script that will play quiet audio when the I2S peripheral isn't in use. This removes popping when playback starts or stops. It uses a tiny amount of CPU time (on a Pi Zero, 5%, on a Pi 2 or 3 its negligible). You don't need this on RetroPie because it never releases the I2S device, but it's great for Raspbian.

![](https://cdn-learn.adafruit.com/assets/assets/000/066/516/medium800/adafruit_products_image.png?1543384670)

You will need to reboot once installed.

![](https://cdn-learn.adafruit.com/assets/assets/000/044/812/medium800/adafruit_products_reboot.png?1501791755)

Warning: 

After rebooting, log back in and re-run the script again...It will ask you if you want to test the speaker. Say **y** es and listen for audio to come out of your speakers...

![](https://cdn-learn.adafruit.com/assets/assets/000/044/813/medium800/adafruit_products_spkrtest.png?1501791845)

If it sounds really distorted, it could be the volume is too high. However, in order to have volume control appear in Raspbian desktop or Retropie you must reboot a second time after doing the speaker test, with **sudo reboot**

Warning: 

Once rebooted, try running **alsamixer** and use arrow keys to lower the volume, 50% is a good place to start.

If you're still having audio problems, try re-running the script and saying **N** (disable) the `/dev/zero playback service`.

You can then go to the next page on testing and optimizing your setup. Skip the rest of this page on **Detailed**  **Installation** if the script worked for you!

# Detailed Install

If, for some reason, you can't just run the script and you want to go through the install by hand - here's all the steps!

## Update /etc/modprobe.d (if it exists)

Log into your Pi and get into a serial console (either via a console cable, the TV console, RXVT, or what have you)

Edit the raspi blacklist with

```terminal
sudo nano /etc/modprobe.d/raspi-blacklist.conf
```

![](https://cdn-learn.adafruit.com/assets/assets/000/032/629/medium800/adafruit_products_blacklist.png?1464031033)

 **If the file is empty, just skip this step**

However, if you see the following lines:

`blacklist i2c-bcm2708`  
`blacklist snd-soc-pcm512x `  
`blacklist snd-soc-wm8804`

![](https://cdn-learn.adafruit.com/assets/assets/000/032/630/medium800/adafruit_products_empty.png?1464031122)

Update the lines by putting a **#** before each line

![](https://cdn-learn.adafruit.com/assets/assets/000/032/631/medium800/adafruit_products_blacklisted.png?1464031154)

Save by typing **Control-X Y \<return\>**

## Disable headphone audio (if it's set)

Edit the raspi modules list with

```terminal
sudo nano /etc/modules
```

 **If the file is empty, just skip this step**

However, if you see the following line:

`snd_bcm2835`

![](https://cdn-learn.adafruit.com/assets/assets/000/032/632/medium800/adafruit_products_sndmod.png?1464031844)

Put a **#** in front of it

![](https://cdn-learn.adafruit.com/assets/assets/000/032/633/medium800/adafruit_products_nosnd.png?1464031943)

and save with **Control-X Y \<return\>**

## Create asound.conf file

Edit the raspi modules list with

```terminal
sudo nano /etc/asound.conf
```

This file ought to be blank!

![](https://cdn-learn.adafruit.com/assets/assets/000/032/634/medium800/adafruit_products_newasound.png?1464032034)

Copy and paste the following text into the file

```auto
pcm.speakerbonnet {
   type hw card 0
}

pcm.dmixer {
   type dmix
   ipc_key 1024
   ipc_perm 0666
   slave {
     pcm "speakerbonnet"
     period_time 0
     period_size 1024
     buffer_size 8192
     rate 44100
     channels 2
   }
}

ctl.dmixer {
    type hw card 0
}

pcm.softvol {
    type softvol
    slave.pcm "dmixer"
    control.name "PCM"
    control.card 0
}

ctl.softvol {
    type hw card 0
}

pcm.!default {
    type             plug
    slave.pcm       "softvol"
}
```

![](https://cdn-learn.adafruit.com/assets/assets/000/045/128/medium800/adafruit_products_asound.png?1502334751)

Save the file as usual

## Add Device Tree Overlay

&nbsp;Edit your Pi configuration file with

Info: For older versions of Raspberry Pi, edit /boot/config.txt instead.

```terminal
sudo nano /boot/firmware/config.txt
```

And scroll down to the bottom. If you see a line that says: `dtparam=audio=on`

![](https://cdn-learn.adafruit.com/assets/assets/000/032/636/medium800/adafruit_products_dtover.png?1464032257)

Disable it by putting a **#** in front.

Then add:

```auto
dtoverlay=max98357a
```

on the next line. Save the file and reboot your Pi with

```terminal
sudo reboot
```

# Adafruit I2S Audio Bonnet for Raspberry Pi 

## Raspberry Pi Test

# Speaker Tests!

OK you can use whatever software you like to play audio but if you'd like to test the speaker output, here's some quick commands that will let you verify your amp and speaker are working as they should!

## Simple white noise speaker test

Run

```terminal
speaker-test -c2
```

to generate white noise out of the speaker, alternating left and right.

If you have a mono output amplifier, the I2S amp merges left and right channels, so you'll hear continuous white noise

## Simple WAV speaker test

Once you've got something coming out, try to play an audio file with **speaker-test** (for WAV files, not MP3)

```terminal
speaker-test -c2 --test=wav -w /usr/share/sounds/alsa/Front_Center.wav
```

You'll hear audio coming from left and right alternating speakers

## Simple MP3 speaker test

If you want to play a stream of music, you can try

```terminal
sudo apt-get install -y mpg123
mpg123 http://ice1.somafm.com/u80s-128-mp3
```

[If you want to play MP3's on command, check out this tutorial which covers how to set that up](http://learn.adafruit.com/playing-sounds-and-using-buttons-with-raspberry-pi "Link: http://learn.adafruit.com/playing-sounds-and-using-buttons-with-raspberry-pi")

At this time, Jessie Raspbery Pi kernel **does not support mono audio** out of the I2S interface, **you can only play stereo** , so any mono audio files may need conversion to stereo!

Info: 

# Volume adjustment

Many programs like PyGame and Sonic Pi have volume control within the application. For other programs you can set the volume using the command line tool called **alsamixer**. Just type alsamixer in and then use the up/down arrows to set the volume. Press Escape once its set

![](https://cdn-learn.adafruit.com/assets/assets/000/045/130/medium800/adafruit_products_alsamixer.png?1502336173)

In Raspbian PIXEL you can set the volume using the menu item control. If it has an X through it, try restarting the Pi (you have to restart twice after install to get PIXEL to recognize the volume control

![](https://cdn-learn.adafruit.com/assets/assets/000/045/131/medium800/adafruit_products_2017-08-10-033949_1280x800_scrot.png?1502336515)

# Adafruit I2S Audio Bonnet for Raspberry Pi 

## Play Audio with PyGame

You can use **mpg123** for basic testing but it's a little clumsy for use where you want to dynamically change the volume or have an interactive program. For more powerful audio playback we suggest using PyGame to playback a variety of audio formats (MP3 included!)

# Install PyGame

Start by installing pygame support, you'll need to open up a console on your Pi with network access and run:

```auto
sudo apt-get install python3-pygame
```

Next, download this pygame example zip to your Pi

[Click to download PyGame example code &amp; sample mp3s](https://cdn-learn.adafruit.com/assets/assets/000/041/506/original/pygame_example.zip?1493840708)
On the command line, run

`wget https://cdn-learn.adafruit.com/assets/assets/000/041/506/original/pygame_example.zip`

`unzip pygame_example.zip`

# Run Demo

Inside the zip is an example called **pygameMP3.py**

This example will playback all MP3's within the script's folder. To demonstrate that you can also adjust the volume within pygame, the second argument is the volume for playback. Specify a volume to playback with a command line argument between 0.0 and 1.0

For example here is how to play at 75% volume:

```auto
python pygameMP3.py 0.75
```

Here's the code if you have your own mp3s!

```auto
''' pg_midi_sound101.py
play midi music files (also mp3 files) using pygame
tested with Python273/331 and pygame192 by vegaseat
'''
#code modified by James DeVito from here: https://www.daniweb.com/programming/software-development/code/454835/let-pygame-play-your-midi-or-mp3-files


#!/usr/bin/python

import sys
import pygame as pg
import os
import time


def play_music(music_file):
    '''
    stream music with mixer.music module in blocking manner
    this will stream the sound from disk while playing
    '''
    clock = pg.time.Clock()
    try:
        pg.mixer.music.load(music_file)
        print("Music file {} loaded!".format(music_file))
    except pygame.error:
        print("File {} not found! {}".format(music_file, pg.get_error()))
        return

    pg.mixer.music.play()
    
    # If you want to fade in the audio...
    # for x in range(0,100):
    #     pg.mixer.music.set_volume(float(x)/100.0)
    #     time.sleep(.0075)
    # # check if playback has finished
    while pg.mixer.music.get_busy():
        clock.tick(30)


freq = 44100    # audio CD quality
bitsize = -16   # unsigned 16 bit
channels = 2    # 1 is mono, 2 is stereo
buffer = 2048   # number of samples (experiment to get right sound)
pg.mixer.init(freq, bitsize, channels, buffer)


if len(sys.argv) &gt; 1:

    try: 
        user_volume = float(sys.argv[1])
    except ValueError:
        print "Volume argument invalid. Please use a float (0.0 - 1.0)"
        pg.mixer.music.fadeout(1000)
        pg.mixer.music.stop()
        raise SystemExit

    print("Playing at volume: " + str(user_volume)+ "\n")
    pg.mixer.music.set_volume(user_volume)
    mp3s = []
    for file in os.listdir("."):
        if file.endswith(".mp3"):
            mp3s.append(file)

    print mp3s

    for x in mp3s: 
        try:
            play_music(x)
            time.sleep(.25)
        except KeyboardInterrupt:
            # if user hits Ctrl/C then exit
            # (works only in console mode)
            pg.mixer.music.fadeout(1000)
            pg.mixer.music.stop()
            raise SystemExit
else:
    print("Please specify volume as a float! (0.0 - 1.0)")
```

# Adafruit I2S Audio Bonnet for Raspberry Pi 

## I2S Audio FAQ

### 

Try rebooting once after playing some audio. Also make sure you have our latest alsa configuration (check the detailed install page on the Raspberry Pi Setup page for the `/etc/asound.conf`!

If its still not working, you can still change the volume, just use **alsamixer** from a Terminal command prompt.

### 

Yep, this is a known Raspbian Linux thing. Yay Linux! We don't have a fix for it. If it makes you feel better, my fancy Windows development computer does the same thing with my desktop speakers.

You can reduce popping a lot with the '/dev/zero play' option in the i2s setup script. (We added it in October 2018) but you still might get that one initial pop

### 

Make sure you've lowered the volume. The default is 100% which is waaaay too high! Use **alsamixer** to reduce the volume to 50% or so

### 

It will work with _anything_ that has alsa audio support. There's thousands of linux programs so we can't guarantee all of them will work but here's what we found does for sure!

- **PyGame** - see our page on [playing audio with PyGame](../../../../adafruit-speaker-bonnet-for-raspberry-pi/audio-with-pygame) for example code. Volume can be controlled within pygame
- **mpg123** - command line mp3 audio playback. use alsamixer to control the volume
- **aplay** - for playing wav files on the command line
- **Sonic Pi** - tested in the Pixel Desktop. Use the Sonic Pi settings panel to change the volume - it does not seem to care about what global audio volume you set!
- **Scratch 2** - tested in the Pixel Desktop. Works fine but may have a delay and make a popping sound the first time you play audio. You can set volume with alsamixer and also via the app by using the **set volume to nn%** block
- **Scratch 1** - doesn't work, something not set up with Scratch 1 to use alsa?
- **RetroPie/Emulation Station** - audio works within games (we tested NES and MAME libretro) but does not work in the 'main screen' (selecting which game to play interface)

# Adafruit I2S Audio Bonnet for Raspberry Pi 

## Downloads

# Files

- [EagleCAD PCB Files](https://github.com/adafruit/Adafruit-I2S-Audio-Bonnet-for-Raspberry-Pi-PCB)
- [UDA1334A Datasheet](https://cdn-shop.adafruit.com/product-files/3678/UDA1334ATS.pdf)

# Schematic & Fabrication Print

Click to embiggen

![](https://cdn-learn.adafruit.com/assets/assets/000/066/513/medium800/adafruit_products_schem.png?1543383079)

![](https://cdn-learn.adafruit.com/assets/assets/000/066/514/medium800/adafruit_products_fab.png?1543383083)


## Primary Products

### Adafruit I2S Audio Bonnet for Raspberry Pi

[Adafruit I2S Audio Bonnet for Raspberry Pi](https://www.adafruit.com/product/4037)
 **Discontinued** -[you can grab this Adafruit I2S 3W Stereo Speaker Bonnet for Raspberry Pi - Mini Kit&nbsp;instead!](https://www.adafruit.com/product/3346)

Add some easy-listenin' tunes to your Raspberry Pi using this basic audio bonnet. It'll give you stereo line...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/4037)
[Related Guides to the Product](https://learn.adafruit.com/products/4037/guides)

## Featured Products

### 3.5mm Male/Male Stereo Cable

[3.5mm Male/Male Stereo Cable](https://www.adafruit.com/product/2698)
Seamlessly transmit high-quality stereo audio with this&nbsp; **3.5mm Male/Male Stereo Cable.&nbsp;**

Ideal for "passing the AUX cord," or&nbsp;connecting audio devices&nbsp;such as&nbsp;smartphones, tablets, MP3 players, car stereos, portable speakers or any other...

In Stock
[Buy Now](https://www.adafruit.com/product/2698)
[Related Guides to the Product](https://learn.adafruit.com/products/2698/guides)
### 3.5mm Stereo to RCA (Composite Audio) Cable 6 feet

[3.5mm Stereo to RCA (Composite Audio) Cable 6 feet](https://www.adafruit.com/product/1013)
One of the most common converter cables you'll want in your cable collection - this 6 foot long cable converts a standard 3.5mm stereo jack to two color-coded mono RCA coaxial jacks.  
  
You'll want this to convert a 'headphone' or 'line out' jack to your TV or...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/1013)
[Related Guides to the Product](https://learn.adafruit.com/products/1013/guides)

## Related Guides

- [Adafruit I2C QT Rotary Encoder](https://learn.adafruit.com/adafruit-i2c-qt-rotary-encoder.md)
- [Adafruit QT 3V to 5V Level Booster Breakout](https://learn.adafruit.com/adafruit-qt-3v-to-5v-level-booster-breakout.md)
- [Adafruit PCA9546 4-Channel I2C Multiplexer](https://learn.adafruit.com/adafruit-pca9546-4-channel-i2c-multiplexer.md)
- [DS3231 Precision RTC FeatherWing](https://learn.adafruit.com/ds3231-precision-rtc-featherwing.md)
- [IoT Temperature Logger with Analog Devices ADT7410, Raspberry Pi, and Adafruit IO](https://learn.adafruit.com/iot-temperature-logger-with-python-and-adafruit-io.md)
- [Adafruit S-35710 Low-Power Wake Up Timer Breakout](https://learn.adafruit.com/adafruit-s-35710-low-power-wake-up-timer-breakout.md)
- [Adafruit MicroSD SPI or SDIO Card Breakout Board](https://learn.adafruit.com/adafruit-microsd-spi-sdio.md)
- [CircuitPython on the Arduino Nano RP2040 Connect](https://learn.adafruit.com/circuitpython-on-the-arduino-nano-rp2040-connect.md)
- [Bare E-Ink Displays Crash Course](https://learn.adafruit.com/bare-e-ink-displays-crash-course.md)
- [Adafruit STEMMA Speaker](https://learn.adafruit.com/adafruit-stemma-speaker.md)
- [reef-pi Guide 3: Temperature Controller](https://learn.adafruit.com/reef-pi-guide-3-temperature-controller.md)
- [Introducing Feather RP2040 SCORPIO](https://learn.adafruit.com/introducing-feather-rp2040-scorpio.md)
- [Adafruit MCP9600 I2C Thermocouple Amplifier](https://learn.adafruit.com/adafruit-mcp9600-i2c-thermocouple-amplifier.md)
- [Adafruit LiIon or LiPoly Charger BFF Add-On for QT Py](https://learn.adafruit.com/adafruit-qt-py-charger-bff.md)
- [Adafruit Proximity Trinkey](https://learn.adafruit.com/adafruit-proximity-trinkey.md)
