# Adafruit Speaker Bonnet for Raspberry Pi

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/126/559/medium800/adafruit_products_3346-13.jpg?1702066435)

Hey Mr. DJ! Turn up that Raspberry Pi mix to the _max_ with this cute 3W Stereo Amplifier Bonnet for Raspberry Pi. (It's not big enough to be an official HAT, so we called it a bonnet, you see?) 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 offical operating system) and Retropie.

![](https://cdn-learn.adafruit.com/assets/assets/000/126/560/medium800/adafruit_products_3346-11.jpg?1702066448)

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 headphone jack. And it's super easy to get started. Just plug in any 4 to 8 ohm speakers, up to 3 Watts, run our installer script on any Raspberry Pi, reboot and you're ready to jam!

![](https://cdn-learn.adafruit.com/assets/assets/000/126/561/medium800/adafruit_products_3346-16.jpg?1702066458)

Each order comes as a fully assembled PCB with a 2x20 header and 2x terminal blocks. Once that's done [either plug one of our enclosed speaker sets](https://www.adafruit.com/products/1669) right into the JST jack in the middle _or_ you can solder the terminal blocks in and then connect any speaker you like.

Don't forget to make sure you have a good strong 5V power supply - especially if you're using the 3W 4 ohm speakers! [Our 2.4A power plug is recommended](https://www.adafruit.com/products/1995)

![](https://cdn-learn.adafruit.com/assets/assets/000/126/562/medium800/adafruit_products_3346-08.jpg?1702066465)

 **Revision History:**

- **As of December 5, 2023** &nbsp;- This speaker bonnet&nbsp;now has the terminal blocks pre-soldered.&nbsp;We've also updated this PCB with&nbsp;[Adafruit Pinguin](https://github.com/adafruit/Adafruit_Pinguin)&nbsp;to make a lovely and legible silkscreen.
- **As of June 24, 2022 -&nbsp;** We have added some breakout pads to the PCB to make configuration and muting easier for advanced users
- **As of March 16, 2018** &nbsp;This bonnet now comes with the 2x20 female header already soldered on

# Adafruit Speaker Bonnet for Raspberry Pi

## Assembly

Warning: 

![](https://cdn-learn.adafruit.com/assets/assets/000/045/588/medium800/raspberry_pi_3346-02.jpg?1503287710)

Place the 2x20 female header in so the connector is on the **bottom**.&nbsp;To make it easier to keep it&nbsp;in place, you can use some tape. Tacky clay also works, whatever you've got handy!

![raspberry_pi_DSC_3800.jpg](https://cdn-learn.adafruit.com/assets/assets/000/045/589/medium640/raspberry_pi_DSC_3800.jpg?1503287803)

## And Solder!

&nbsp;

Be sure to solder all pins for reliable electrical contact.  
  
_(For tips on soldering, be sure to check out our&nbsp;_[_Guide to Excellent Soldering_](http://learn.adafruit.com/adafruit-guide-excellent-soldering)_)._

Start by soldering the first row of header

![raspberry_pi_DSC_3802.jpg](https://cdn-learn.adafruit.com/assets/assets/000/045/590/medium640/raspberry_pi_DSC_3802.jpg?1503287849)

![raspberry_pi_DSC_3804.jpg](https://cdn-learn.adafruit.com/assets/assets/000/045/591/medium640/raspberry_pi_DSC_3804.jpg?1503287860)

![raspberry_pi_DSC_3808.jpg](https://cdn-learn.adafruit.com/assets/assets/000/045/592/medium640/raspberry_pi_DSC_3808.jpg?1503287868)

![raspberry_pi_DSC_3816.jpg](https://cdn-learn.adafruit.com/assets/assets/000/045/593/medium640/raspberry_pi_DSC_3816.jpg?1503287884)

Now flip around and solder the other row completely

![raspberry_pi_DSC_3817.jpg](https://cdn-learn.adafruit.com/assets/assets/000/045/594/medium640/raspberry_pi_DSC_3817.jpg?1503287943)

![raspberry_pi_DSC_3818.jpg](https://cdn-learn.adafruit.com/assets/assets/000/045/595/medium640/raspberry_pi_DSC_3818.jpg?1503287951)

![raspberry_pi_DSC_3824.jpg](https://cdn-learn.adafruit.com/assets/assets/000/045/598/medium640/raspberry_pi_DSC_3824.jpg?1503288030)

![raspberry_pi_DSC_3827.jpg](https://cdn-learn.adafruit.com/assets/assets/000/045/599/medium640/raspberry_pi_DSC_3827.jpg?1503288054)

You're done with the header strips.

&nbsp;

Check your solder joints visually and continue onto the next steps

![raspberry_pi_DSC_3828.jpg](https://cdn-learn.adafruit.com/assets/assets/000/045/600/medium640/raspberry_pi_DSC_3828.jpg?1503288078)

Next we will solder in the 2&nbsp;3.5mm terminal blocks used to connect _speakers_&nbsp;to the _Speaker_ Bonnet.&nbsp;

&nbsp;

**Make sure the open parts of the terminals face outwards** &nbsp;so you can easily connect wires.

&nbsp;

To make it easier to keep these in place, you can use some tape to hold down the two header pieces. Tacky clay also works, whatever you've got handy!

![raspberry_pi_DSC_3830.jpg](https://cdn-learn.adafruit.com/assets/assets/000/045/601/medium640/raspberry_pi_DSC_3830.jpg?1503288145)

Solder in each block, make sure you get to each of the 4&nbsp;pins

![raspberry_pi_DSC_3752.jpg](https://cdn-learn.adafruit.com/assets/assets/000/045/602/medium640/raspberry_pi_DSC_3752.jpg?1503288252)

![raspberry_pi_DSC_3832.jpg](https://cdn-learn.adafruit.com/assets/assets/000/045/603/medium640/raspberry_pi_DSC_3832.jpg?1503288270)

![raspberry_pi_DSC_3834.jpg](https://cdn-learn.adafruit.com/assets/assets/000/045/604/medium640/raspberry_pi_DSC_3834.jpg?1503288287)

Now that you're done with the terminal&nbsp;blocks, check your work make sure that each solder joint is done and looks shiny

![raspberry_pi_DSC_3835.jpg](https://cdn-learn.adafruit.com/assets/assets/000/045/605/medium640/raspberry_pi_DSC_3835.jpg?1503288377)

OK you're done!

![raspberry_pi_DSC_3836.jpg](https://cdn-learn.adafruit.com/assets/assets/000/045/606/medium640/raspberry_pi_DSC_3836.jpg?1503288432)

# Adafruit Speaker Bonnet for Raspberry Pi

## Pinouts

# Power Supply
![](https://cdn-learn.adafruit.com/assets/assets/000/037/887/medium800/raspberry_pi_powerpins.jpg?1481137099)

The two amplifier chips use the 3V + 5V + GND power pin at the 'top' of the 2x20 header. If using 3W speakers, you can draw a significant amount of current, over 1.5 Amps! Make sure you power your Pi with a good wall adapter like our 2.4A microUSB power plug.

### 5V 2.5A Switching Power Supply with 20AWG MicroUSB Cable

[5V 2.5A Switching Power Supply with 20AWG MicroUSB Cable](https://www.adafruit.com/product/1995)
Our all-in-one 5V 2.5 Amp + MicroUSB cable power adapter is the perfect choice for powering single-board computers like Raspberry Pi, BeagleBone, or anything else that's power-hungry!

This adapter was specifically designed to provide 5.25V, not 5V, but we still call it a 5V USB...

In Stock
[Buy Now](https://www.adafruit.com/product/1995)
[Related Guides to the Product](https://learn.adafruit.com/products/1995/guides)
![MicroUSB power supply with bundled cable and U.S. plugs.](https://cdn-shop.adafruit.com/640x480/1995-02.jpg)

Alternatively, if you _really_ need a lot of power, use a 5V 4A power adapter and then a DC to micro USB adapter

### 5V 4A (4000mA) switching power supply - UL Listed

[5V 4A (4000mA) switching power supply - UL Listed](https://www.adafruit.com/product/1466)
Need a lot of 5V power? This switching supply gives a clean regulated 5V output at up to **4 Amps** (4000mA). 110 or 240 input, so it works in any country. The plugs are "US 2-prong" style so you may need a plug adapter, but you can pick one up at any hardware store for $1 or so,...

In Stock
[Buy Now](https://www.adafruit.com/product/1466)
[Related Guides to the Product](https://learn.adafruit.com/products/1466/guides)
![Angled shot of 5V 4A switching power supply brick with power cable.](https://cdn-shop.adafruit.com/640x480/1466-10.jpg)

### MicroUSB Plug to 5.5/2.1mm DC Barrel Jack Adapter

[MicroUSB Plug to 5.5/2.1mm DC Barrel Jack Adapter](https://www.adafruit.com/product/2727)
The premise of this&nbsp; **MicroUSB Plug to 5.5/2.1mm Jack adapter&nbsp;** is simple. There's a MicroUSB plug on one end and a 5.5mm/2.1mm DC barrel jack with center positive polarity on the other. This allows you to&nbsp;connect one of our&nbsp;<a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/2727)
[Related Guides to the Product](https://learn.adafruit.com/products/2727/guides)
![Micro USB Plug to 5.5/2.1mm DC Barrel Jack Adapter](https://cdn-shop.adafruit.com/640x480/2727-06.jpg)

# I2S Audio Data Pins
![](https://cdn-learn.adafruit.com/assets/assets/000/037/888/medium800/raspberry_pi_i2spins.jpg?1481137291)

The Bonnet uses 3 data pins **and they cannot be changed!** GPIO pins #18, #19 and #21 are used. All other pins are available

# Gain
![](https://cdn-learn.adafruit.com/assets/assets/000/136/194/medium800/adafruit_products_gain.jpg?1744050526)

The GAIN pads above the Right speaker output connection set the default gain amplifier gain **6dB** , because the left and center pads are pre-connected with a jumper. If you cut that jumper, the gain becomes **9dB**. If you cut the jumper and then connect the center and right pads, the gain is **3dB**. (Connecting all three pads together still selects&nbsp; **6dB**.)

# Speaker Outputs
![](https://cdn-learn.adafruit.com/assets/assets/000/037/889/medium800/raspberry_pi_spkout.jpg?1481137341)

This is the fun part, you get stereo output - one left and one right channel. Each one is Bridge-Tied-Load so **do not connect both outputs together** to get more volume!

You can use the JST 4-pin plug in the center if you're going to just plug in one of our enclosed speaker kits:

### Stereo Enclosed Speaker Set - 3W 4 Ohm

[Stereo Enclosed Speaker Set - 3W 4 Ohm](https://www.adafruit.com/product/1669)
Listen up! This set of two 2.8" x 1.2" speakers are the perfect addition to any audio project where you need 4 ohm impedance and 3W or less of power. We particularly like these speakers as they are small and enclosed for good audio volume and quality. The pair have a handy SPK input...

In Stock
[Buy Now](https://www.adafruit.com/product/1669)
[Related Guides to the Product](https://learn.adafruit.com/products/1669/guides)
![Rectangular Stereo Enclosed Speaker Set with JST cable](https://cdn-shop.adafruit.com/640x480/1669-06.jpg)

 **OR**

You can use the terminal block spots on the left & right to connect regular speaker cones. You'll need to solder wires on and such but this way you can use whatever speaker you like! We suggest 8 ohm 1W or 4 ohm 3W. For louder audio, but more power usage, use the 4 ohm speakers. For quieter audio, but less power usage, use the 8 ohm speakers

### Speaker - 3" Diameter - 4 Ohm 3 Watt

[Speaker - 3" Diameter - 4 Ohm 3 Watt](https://www.adafruit.com/product/1314)
Listen up! This 3" diameter speaker cone is the perfect addition to any audio project where you need an 4 ohm impedance and 3W or less of power. We particularly like this cone as it has 4 handy mounting tabs 60mm apart.  
  
Works great with our <a...></a...>

Out of Stock
[Buy Now](https://www.adafruit.com/product/1314)
[Related Guides to the Product](https://learn.adafruit.com/products/1314/guides)
![Overhead shot of 3" diameter speaker.](https://cdn-shop.adafruit.com/640x480/1314-00.jpg)

### Speaker - 3" Diameter - 8 Ohm 1 Watt

[Speaker - 3" Diameter - 8 Ohm 1 Watt](https://www.adafruit.com/product/1313)
Listen up! This 3" diameter speaker cone is the perfect addition to any audio project where you need an 8 ohm impedance and 1W or less of power. We particularly like this cone as it has 4 handy mounting tabs 60mm apart.  
  
Works great with our <a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/1313)
[Related Guides to the Product](https://learn.adafruit.com/products/1313/guides)
![Overhead shot of 3" diameter speaker.](https://cdn-shop.adafruit.com/640x480/1313-00.jpg)

You can also use other 'audio' devices like sonic transducers! These are devices you can put down onto a surface like a table, to make it into a speaker.

### Medium Surface Transducer with Wires - 4 Ohm 3 Watt

[Medium Surface Transducer with Wires - 4 Ohm 3 Watt](https://www.adafruit.com/product/1785)
Turn any surface/wall/table etc into a speaker with a surface transducer. This type of speaker does not have a moving cone like most speakers you've seen. Instead, a small metal rod is wrapped with the voice coil. When current is pulsed through the coil, the magnetic field causes a piece...

In Stock
[Buy Now](https://www.adafruit.com/product/1785)
[Related Guides to the Product](https://learn.adafruit.com/products/1785/guides)
![Angle Shot of Medium Surface Transducer](https://cdn-shop.adafruit.com/640x480/1785-06.jpg)

# Adafruit Speaker 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 Speaker 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 Speaker Bonnet for Raspberry Pi

## Pi I2S Tweaks

Warning: 

# Reducing popping

For people who followed our original installation instructions with the simple alsa config, they may find that the I2S audio pops when playing new audio.

The workaround is to use a software mixer to output a fixed sample rate to the I2S device so the bit clock does not change. I use ALSA so I configured **dmixer** and I no longer have any pops or clicks. Note that the RaspPi I2S driver does not support **dmixer** by default and you must [follow these instructions provided](https://support.hifiberry.com/hc/en-us/articles/207397665-Mixing-different-audio-sources) to add it. Continue on for step-by-step on how to enable it!

## Step 1

Start by modify **/boot/firmware/config.txt** to add`dtoverlay=i2s-mmap`

Run **sudo nano /boot/firmware/config.txt** and add the text to the bottom like so:

![](https://cdn-learn.adafruit.com/assets/assets/000/044/822/medium800/adafruit_products_i2smmap.png?1501793168)

Warning: 

Save and exit.

Then change **/etc/asound.conf** to:

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

pcm.!default { 
   type plug 
   slave.pcm "dmixer" 
}

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 
}
```

By running `sudo nano /etc/asound.conf`

This creates a PCM device called speakerbonnet which is connected to the hardware I2S device. Then we make a new 'dmix' device (`type dmix`) called `pcm.dmixer`. We give it a unique Inter Process Communication key (`ipc_key 1024`) and permissions that are world-read-writeable `(ipc_perm 0666`) The mixer will control the hardware pcm device speakerbonnet (pcm "speakerbonnet") and has a buffer set up so its nice and fast. The communication buffer is set up so there's no delays (`period_time 0`, `period_size 1024` and `buffer_size 8192` work well). The default mixed rate is 44.1khz stereo (`rate 44100 channels 2`)

Finally we set up a control interface but it ended up working best to just put in the hardware device here -`ctl.dmixer { type hw card 0 }`

![](https://cdn-learn.adafruit.com/assets/assets/000/044/823/medium800/adafruit_products_alsaconfig.png?1501793177)

Save and exit. Then reboot the Pi to enable the mixer. Also, while it will _greatly_ reduce popping, you still may get one once in a while - especially when first playing audio!

# Add software volume control

The basic I2S chipset used here does not have software control built in. So we have to 'trick' the Pi into creating a software volume control. [Luckily, its not hard once you know how to do it](http://alsa.opensrc.org/How_to_use_softvol_to_control_the_master_volume).

Create a new audio config file in **~/.asoundrc** with `nano ~/.asoundrc` and inside put the following text:

```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"
}
```

Warning: 

![](https://cdn-learn.adafruit.com/assets/assets/000/044/883/medium800/adafruit_products_softmaster.png?1501994429)

Warning: Pi OS Desktop will trash the ~/.asoundrc configuration file everytime it starts up.

You can set the immutable bit to prevent this file from being destroyed. This is due to the&nbsp;

lxplug-volumepulse desktop widget. Pi OS Lite does not have this issue.

```auto
chattr +i ~/.asoundrc
```

Save and exit

Now, here's the trick, you have to reboot, then play some audio through alsa, then reboot to get the alsamixer to sync up right:

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

_Then_ you can type **alsamixer** to control the volume with the 'classic' alsa mixing interface

![](https://cdn-learn.adafruit.com/assets/assets/000/044/880/medium800/adafruit_products_alsamixer.png?1501993968)

Just press the up and down arrows to set the volume, and ESC to quit

# Adafruit Speaker 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 Speaker 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) > 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 Speaker Bonnet for Raspberry Pi

## Downloads

# Datasheets & Files

- [MAX98357 Datasheet](https://cdn-shop.adafruit.com/product-files/3006/MAX98357A-MAX98357B.pdf)
- [EagleCAD PCB files](https://github.com/adafruit/Adafruit-Stereo-Speaker-Bonnet-PCB)
- [Fritzing object in Adafruit Fritzing library](https://github.com/adafruit/Fritzing-Library/)

# Schematic and Fab Print - Rev D
![](https://cdn-learn.adafruit.com/assets/assets/000/133/905/medium800/adafruit_products_schem.png?1733159333)

![](https://cdn-learn.adafruit.com/assets/assets/000/133/906/medium800/adafruit_products_fab.png?1733159341)

# Schematic - Original
![](https://cdn-learn.adafruit.com/assets/assets/000/037/882/medium800/raspberry_pi_schem.png?1481135658)

# Fabrication Print

Dims in mm

![](https://cdn-learn.adafruit.com/assets/assets/000/037/881/medium800/raspberry_pi_fab.png?1481135643)


## Primary Products

### Adafruit I2S 3W Stereo Speaker Bonnet for Raspberry Pi

[Adafruit I2S 3W Stereo Speaker Bonnet for Raspberry Pi](https://www.adafruit.com/product/3346)
Hey Mr. DJ! Turn up that Raspberry Pi mix to the&nbsp;_max_&nbsp;with this cute **3W Stereo Amplifier Bonnet for Raspberry Pi**. (It's not big enough to be an official HAT, so we called it a bonnet, you see?) It's the exact same size as a Raspberry Pi Zero but works...

In Stock
[Buy Now](https://www.adafruit.com/product/3346)
[Related Guides to the Product](https://learn.adafruit.com/products/3346/guides)

## Featured Products

### Stereo Enclosed Speaker Set - 3W 4 Ohm

[Stereo Enclosed Speaker Set - 3W 4 Ohm](https://www.adafruit.com/product/1669)
Listen up! This set of two 2.8" x 1.2" speakers are the perfect addition to any audio project where you need 4 ohm impedance and 3W or less of power. We particularly like these speakers as they are small and enclosed for good audio volume and quality. The pair have a handy SPK input...

In Stock
[Buy Now](https://www.adafruit.com/product/1669)
[Related Guides to the Product](https://learn.adafruit.com/products/1669/guides)
### Speaker - 3" Diameter - 8 Ohm 1 Watt

[Speaker - 3" Diameter - 8 Ohm 1 Watt](https://www.adafruit.com/product/1313)
Listen up! This 3" diameter speaker cone is the perfect addition to any audio project where you need an 8 ohm impedance and 1W or less of power. We particularly like this cone as it has 4 handy mounting tabs 60mm apart.  
  
Works great with our <a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/1313)
[Related Guides to the Product](https://learn.adafruit.com/products/1313/guides)
### Speaker - 3" Diameter - 4 Ohm 3 Watt

[Speaker - 3" Diameter - 4 Ohm 3 Watt](https://www.adafruit.com/product/1314)
Listen up! This 3" diameter speaker cone is the perfect addition to any audio project where you need an 4 ohm impedance and 3W or less of power. We particularly like this cone as it has 4 handy mounting tabs 60mm apart.  
  
Works great with our <a...></a...>

Out of Stock
[Buy Now](https://www.adafruit.com/product/1314)
[Related Guides to the Product](https://learn.adafruit.com/products/1314/guides)
### Medium Surface Transducer with Wires - 4 Ohm 3 Watt

[Medium Surface Transducer with Wires - 4 Ohm 3 Watt](https://www.adafruit.com/product/1785)
Turn any surface/wall/table etc into a speaker with a surface transducer. This type of speaker does not have a moving cone like most speakers you've seen. Instead, a small metal rod is wrapped with the voice coil. When current is pulsed through the coil, the magnetic field causes a piece...

In Stock
[Buy Now](https://www.adafruit.com/product/1785)
[Related Guides to the Product](https://learn.adafruit.com/products/1785/guides)
### Bone Conductor Transducer with Wires - 8 Ohm 1 Watt

[Bone Conductor Transducer with Wires - 8 Ohm 1 Watt](https://www.adafruit.com/product/1674)
Drown out the voices in your head with a bone conduction transducer! This incredible speaker does not have a moving cone like most speakers you've seen, instead, a small metal rod is wrapped with the voice coil. When current is pulsed through the coil, the magnetic field causes a piece of...

Out of Stock
[Buy Now](https://www.adafruit.com/product/1674)
[Related Guides to the Product](https://learn.adafruit.com/products/1674/guides)
### Stacking Header - 2x20 Extra Tall Header

[Stacking Header - 2x20 Extra Tall Header](https://www.adafruit.com/product/1979)
Stack multiple plates, breakouts etc onto your Raspberry Pi Model B+ with this custom-made extra-tall and extra-long 2x20&nbsp;female header. The female header part has extra spaces to make it 23mm tall: when placed on your Pi, a PCB will clear the Ethernet and USB jacks. The stacky pin part...

In Stock
[Buy Now](https://www.adafruit.com/product/1979)
[Related Guides to the Product](https://learn.adafruit.com/products/1979/guides)

## Related Guides

- [Boomy Pi Airplay Boombox](https://learn.adafruit.com/boomy-pi-airplay.md)
- [Raspberry Pi Halloween Costume Detector](https://learn.adafruit.com/raspberry-pi-halloween-costume-detector.md)
- [Face Mask Efficacy Testing with SCIENCE!](https://learn.adafruit.com/raspberry-pi-camera-vision-face-mask-efficacy-testing.md)
- [Local Models for Translation, Speech, & Wardrobe on Pi 5](https://learn.adafruit.com/local-models-for-translation-speech-wardrobe-on-pi-5.md)
- [Soundboard Speaker for Bikes & Scooters](https://learn.adafruit.com/soundboard-speaker-for-bikes-scooters.md)
- [Raspberry Pi Selfie Bot](https://learn.adafruit.com/raspberry-pi-selfie-bot.md)
- [Adafruit Feather HUZZAH ESP8266](https://learn.adafruit.com/adafruit-feather-huzzah-esp8266.md)
- [Smart Bathroom App](https://learn.adafruit.com/smart-bathroom-app.md)
- [OpenClaw on Raspberry Pi](https://learn.adafruit.com/openclaw-on-raspberry-pi.md)
- [Adafruit Prototyping Pi Plate](https://learn.adafruit.com/adafruit-prototyping-pi-plate.md)
- [Using IFTTT with Adafruit IO to Make an IoT Door Detector](https://learn.adafruit.com/using-ifttt-with-adafruit-io.md)
- [Generating AI “Art” with VQGAN+CLIP](https://learn.adafruit.com/generating-ai-art-with-vqgan-clip.md)
- [CircuitPython Libraries on Linux and Orange Pi](https://learn.adafruit.com/circuitpython-on-orangepi-linux.md)
- [Adafruit DS3502 I2C Digital Potentiometer](https://learn.adafruit.com/ds3502-i2c-potentiometer.md)
- [Introducing Adafruit CLUE](https://learn.adafruit.com/adafruit-clue.md)
