I2S Audio Breakout Hookup Guide

Pages
Contributors: Alex the Giant
Favorited Favorite 2

Introduction

The I2S Audio Breakout board uses the MAX98357A digital to analog converter (DAC), which converts I2S (not be confused with I2C) audio to an analog signal to drive speakers. The MAX98357A has a built in class D amplifier which can deliver up to 3.2W of power into a 4Ω load. For more information, see the Hardware Overview section below.

SparkFun I2S Audio Breakout - MAX98357A

SparkFun I2S Audio Breakout - MAX98357A

DEV-14809
$6.50
2

Suggested Tools

You will need a soldering iron, solder, general soldering accessories, screw driver, and hobby knife.

Weller WE1010 Soldering Station

Weller WE1010 Soldering Station

TOL-14734
$155.00
4
Solder Lead Free - 100-gram Spool

Solder Lead Free - 100-gram Spool

TOL-09325
$9.95
8
Pocket Screwdriver Set

Pocket Screwdriver Set

TOL-12891
$4.50
5
Hobby Knife

Hobby Knife

TOL-09200
$3.50
2

Suggested Reading

If you aren't familiar with the following concepts, we recommend checking out these tutorials before continuing.

How to Solder: Through-Hole Soldering

This tutorial covers everything you need to know about through-hole soldering.

How to Power a Project

A tutorial to help figure out the power requirements of your project.

Button and Switch Basics

A tutorial on electronics' most overlooked and underappreciated component: the switch! Here we explain the difference between momentary and maintained switches and what all those acronyms (NO, NC, SPDT, SPST, ...) stand for.

ESP32 Thing Hookup Guide

An introduction to the ESP32 Thing's hardware features, and a primer on using the WiFi system-on-chip in Arduino.

How to Work with Jumper Pads and PCB Traces

Handling PCB jumper pads and traces is an essential skill. Learn how to cut a PCB trace, add a solder jumper between pads to reroute connections, and repair a trace with the green wire method if a trace is damaged.

ESP32 Thing Motion Shield Hookup Guide

Getting started with the ESP32 Thing Motion Shield to detect movements using the on-board LSM9DS1 IMU and adding a GPS receiver. Data can be easily logged by adding an microSD card to the slot.

Hardware Overview

The I2S audio breakout converts the digital audio signals using the I2S standard to an analog signal and amplifies the signal using a class D amplifier. The board can be configured to output only the left channel, right channel, or both. For more information about how to configure the board, refer to the Jumper Selection section below.

I2S Audio Breakout Board Top View

Board Specs

ParameterDescription
Supply Voltage Range2.5V - 5.5V.
Output Power3.2W into 4Ω at 5V.
Output Channel SelectionLeft, Right, or Left/2 + Right/2 (Default).
Sample Rate8kHz - 96kHz.
Sample Resolution16/32 bit.
Quiescent Current2.4mA.
Additional FeaturesFilterless Class D outputs, no MCLK required, click and pop reduction, short-circuit and thermal protection.


Pin Descriptions

The SparkFun I2S audio breakout board is fairly simple, requiring only a few pin connections to get the board working.

Highlight of Input Connections

Inputs

Pin LabelDescription
LRCLKFrame clock (left/right clock) input.
BCLKBit clock input.
DINSerial data input.
GAINGain setting. Can be set to +3/6/9/12/15dB. Set to +9dB by default.
SDShutdown and channel select. Pull low to shutdown, or use the jumpers to select the channel output (see jumper selection for more information).
GNDConnect to ground
VDDPower input. Must be between 2.5 and 5.5VDC.


Outputs

The output is where you'll connect your speaker.

Highlight of Output Connections

Pin LabelDescription
+Positive speaker output.
-Negative speaker output.


Speaker wire can either be soldered directly to the output pads, but if screw terminals are more your style, you can use our 3.5mm screw terminals.

Screw Terminals 3.5mm Pitch (2-Pin)

Screw Terminals 3.5mm Pitch (2-Pin)

PRT-08084
$1.05

Jumper Selection

By default the board is configured in "mono" operation, meaning the left and right signals are combined together to drive a single speaker.

Highlight Of Jumper Selection

If you want a separate speaker for the left and right audio channels you'll first need to cut the mono jumper as pictured below.

Mono Trace Jumper Cut

To configure the board to respond to a specific audio channel, you'll need to close the stereo jumper as shown below.

Single Channel Select Jumper

Gain Selection

In addition to being able to select the audio channel output, the gain can also be configured in a few ways. The gain of the amplifier can be configured from as low as +3dB to as high as +15dB. While the channel selection can be configured on board, the gain however is controlled externally using the gain pin. By default, the board is configured for +9dB, but can be changed using the table below.

Gain (dB)Gain Pin Connection
15Connected to GND through a 100kΩ resistor.
12Connected to GND.
9Unconnected (Default).
6Connected to VDD.
3Connected to VDD through a 100kΩ resistor.


Examples

Note: These example assumes you are using the latest version of the Arduino IDE on your desktop. If this is your first time using Arduino, please review our tutorial on installing the Arduino IDE. If you have not previously installed an Arduino library, please check out our installation guide.

This board should work with any microcontroller or single board computer that has I2S capable pins. In these examples, we're going to look at a pretty powerful library that allows you to use an ESP32 Thing to play audio from a wide variety of sources. First, we'll play an audio file which is stored in the ESP32's program memory, and once we have that working we'll look at creating a MP3 trigger. The following libraries are needed to run the examples that were originally written for the ESP8266, but also work with the ESP32.

ESP8266Audio Arduino Library

You'll need to install the ESP8266 Audio Arduino Library, written by Earle F. Philhower, which you can get from the link below. This library will allow you to play a wide variety of audio formats including: AAC, FLAC, MIDI, MOD, MP3, RTTTL, and WAV. To use the library, you can add the library from Arduino by selecting Sketch ** > **Include Library ** > **Add .ZIP Library... and selecting the .zip file from wherever you store your file downloads.

ESP8266_Spiram Arduino Library

The ESP8266 Audio library depends on the ESP8266 Spiram library, written by Giancarlo Bacchio, which will also need to be downloaded. You can download the library from the link below. Installing the library follows the same process as outlined above.

First Test

In this first example, we'll run a quick example sketch to make sure the I2S audio breakout board is wired correctly and is working.

ESP32 Connected to I2S Breakout Board

Required Materials

The parts used in this example are listed in the wishlist below. You may not need everything though depending on what you have. Add it to your cart, read through the example, and adjust the cart as necessary.

Hookup Table

The connections that need to be made to the ESP32 are list below.

ESP32 PinI2S Audio Breakout Pin
VUSB/3V3VDD
GNDGND
GPIO 22DIN
GPIO 26BCLK
GPIO 25LRCLK


Make sure to also connect a speaker to the I2S audio breakout board's output pins.

Example Code

We're going to use one of the examples that comes with the library named "PlayAACFromPROGMEM". With the library installed, open the example located in: File > Examples > ESP8266Audio > PlayAACFromPROGMEM .

Where to find Example Sketch "PlayAACFromPROGMEM"

Before we upload the code, we're going to add two lines of code (as highlighted in the image below). The first line is going to adjust the volume, which we add after we initialize the I2S output (out = new AudioOutputI2S();). After the output is initialized, we're going to add out -> SetGain(0.125);. As the name suggests this sets the gain of the output, which takes a floating point number and has a maximum value of 4.0. The second line will reduce hum at the end of the audio clip by adding aac -> stop(); in the else statement in the main loop(). After you upload the sketch to your ESP32, you should hear Homer Simpson's thoughts of perpetual motion machines if everything is working.

Hightlight of changes made to the example sketch

ESP32 MP3 Trigger

Now that we know the board is working, let's take it up a notch. In this next example, we'll create a MP3 trigger that works similar to our MP3 Trigger.

ESP32 Connected to I2S Breakout and pushbuttons

Required Materials

For this example, we'll need to add a few more parts to the ones we used in the previous example (including a second breadboard). You may not need everything though depending on what you have. Add it to your cart, read through the example, and adjust the cart as necessary.

Before we add code, we'll need some audio files to play. Any MP3 audio file should work, you'll just need to copy them over to your microSD card using a microSD USB Reader. Before ejecting the microSD card from your computer, make sure to relabel the files TRACKn.mp3, where n is a integer number between 0-9. The I2S audio breakout board has the same pin connections as the previous example, but this time we're going to change the audio source from PROGMEM to our microSD card. The last step before adding the code below, is to add headers to the ESP32 Thing, as well as the Motion Shield, as outlined in the hookup guide.

language:c
/* SparkFun I2S Audio Breakout Demo
 * Created by: Alex Wende
 * 8/3/2018
 * 
 * Uses a ESP32 Thing to create a MP3 trigger using 
 * the I2S Audio Breakout board.
 * 
 * Parts you'll need:
 * - I2S Audio Breakout board (https://www.sparkfun.com/products/14809)
 * - ESP32 Thing (https://www.sparkfun.com/products/13907)
 * - Micro SD Breakout (https://www.sparkfun.com/products/544)
 * - A microSD card (https://www.sparkfun.com/products/13833)
 * - Speaker (4-8ohms)
 * 
 * The following libraries need to be installed before
 * uploading this sketch:
 * - ESP8266 Audio (https://github.com/earlephilhower/ESP8266Audio)
 * - SRam Library (https://github.com/Gianbacchio/ESP8266_Spiram)
 */

#include <Arduino.h>
#include "AudioGeneratorMP3.h"
#include "AudioOutputI2S.h"
#include "AudioFileSourceSD.h"
#include "driver/i2s.h"
#include <SD.h>

//define trigger pins
#define TRIGGER0 13
#define TRIGGER1 12
#define TRIGGER2 14
#define TRIGGER3 27
#define TRIGGER4 32
#define TRIGGER5 5
#define TRIGGER6 15
#define TRIGGER7 2
#define TRIGGER8 0
#define TRIGGER9 4

//Initialize ESP8266 Audio Library classes
AudioGeneratorMP3 *mp3;
AudioFileSourceSD *file;
AudioOutputI2S *out;

volatile bool playing = 0;
volatile byte loadTrack = 0;

//External Interrupt function with software switch debounce
void IRAM_ATTR handleInterrupt()
{
  static unsigned long last_interrupt_time = 0;
  unsigned long interrupt_time = millis();
  // If interrupts come faster than 200ms, assume it's a bounce and ignore
  if (interrupt_time - last_interrupt_time > 200) 
  {
    //Figure out which switch was triggered, and which track to play
    if(!digitalRead(TRIGGER0)) loadTrack = 1;
    else if(!digitalRead(TRIGGER1)) loadTrack = 2;
    else if(!digitalRead(TRIGGER2)) loadTrack = 3;
    else if(!digitalRead(TRIGGER3)) loadTrack = 4;
    else if(!digitalRead(TRIGGER4)) loadTrack = 5;
    else if(!digitalRead(TRIGGER5)) loadTrack = 6;
    else if(!digitalRead(TRIGGER6)) loadTrack = 7;
    else if(!digitalRead(TRIGGER7)) loadTrack = 8;
    else if(!digitalRead(TRIGGER8)) loadTrack = 9;
    else if(!digitalRead(TRIGGER9)) loadTrack = 10;
    playing = 1;
  }
  last_interrupt_time = interrupt_time;
}

void setup()
{  
  Serial.begin(115200);

  //Configure trigger pins to inputs with internal pull-up resistors enabled
  pinMode(TRIGGER0,INPUT_PULLUP);
  pinMode(TRIGGER1,INPUT_PULLUP);
  pinMode(TRIGGER2,INPUT_PULLUP);
  pinMode(TRIGGER3,INPUT_PULLUP);
  pinMode(TRIGGER4,INPUT_PULLUP);
  pinMode(TRIGGER5,INPUT_PULLUP);
  pinMode(TRIGGER6,INPUT_PULLUP);
  pinMode(TRIGGER7,INPUT_PULLUP);
  pinMode(TRIGGER8,INPUT_PULLUP);
  pinMode(TRIGGER9,INPUT_PULLUP);

  //Create interrupts for each trigger
  attachInterrupt(digitalPinToInterrupt(TRIGGER0),handleInterrupt,FALLING);
  attachInterrupt(digitalPinToInterrupt(TRIGGER1),handleInterrupt,FALLING);
  attachInterrupt(digitalPinToInterrupt(TRIGGER2),handleInterrupt,FALLING);
  attachInterrupt(digitalPinToInterrupt(TRIGGER3),handleInterrupt,FALLING);
  attachInterrupt(digitalPinToInterrupt(TRIGGER4),handleInterrupt,FALLING);
  attachInterrupt(digitalPinToInterrupt(TRIGGER5),handleInterrupt,FALLING);
  attachInterrupt(digitalPinToInterrupt(TRIGGER6),handleInterrupt,FALLING);
  attachInterrupt(digitalPinToInterrupt(TRIGGER7),handleInterrupt,FALLING);
  attachInterrupt(digitalPinToInterrupt(TRIGGER8),handleInterrupt,FALLING);
  attachInterrupt(digitalPinToInterrupt(TRIGGER9),handleInterrupt,FALLING);

  out = new AudioOutputI2S();
  mp3 = new AudioGeneratorMP3();

  delay(1000);
  Serial.print("Initializing SD card...");
  if (!SD.begin(33))
  {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  delay(100);
}

void loop()
{
  if(loadTrack) //Load the track we want to play
  {
    //Stop the current track if playing
    if(playing && mp3->isRunning()) mp3->stop();

    if(loadTrack == 1) file = new AudioFileSourceSD("/TRACK0.mp3");
    else if(loadTrack == 2) file = new AudioFileSourceSD("/TRACK1.mp3");
    else if(loadTrack == 3) file = new AudioFileSourceSD("/TRACK2.mp3");
    else if(loadTrack == 4) file = new AudioFileSourceSD("/TRACK3.mp3");
    else if(loadTrack == 5) file = new AudioFileSourceSD("/TRACK4.mp3");
    else if(loadTrack == 6) file = new AudioFileSourceSD("/TRACK5.mp3");
    else if(loadTrack == 7) file = new AudioFileSourceSD("/TRACK6.mp3");
    else if(loadTrack == 8) file = new AudioFileSourceSD("/TRACK7.mp3");
    else if(loadTrack == 9) file = new AudioFileSourceSD("/TRACK8.mp3");
    else if(loadTrack == 10) file = new AudioFileSourceSD("/TRACK9.mp3");

    out -> SetGain(0.08); //Set the volume
    mp3 -> begin(file,out); //Start playing the track loaded
    loadTrack = 0;
  }

  if(playing && mp3->isRunning()) {
    if (!mp3->loop())
    {
      mp3->stop();
      playing = 0;
      Serial.println("Stopped");
    }
  }
}

With the code on the board, we can see what the sketch does. You can connect momentary pushbutton switches to each of the trigger pins outlined in the table below, with the other end of the switch connected to ground. Another option is to take a ground wire and touch it to one of the trigger pins. When the pin is pulled down to ground, it triggers the corresponding track to play. If a track is still playing when a new pin is triggered, that track will stop and the new track will play.

Audio FileESP32 GPIO Pin
TRACK0.mp313
TRACK1.mp312
TRACK2.mp314
TRACK3.mp327
TRACK4.mp332
TRACK5.mp35
TRACK6.mp315
TRACK7.mp32
TRACK8.mp30
TRACK9.mp34


Resources and Going Further

Now that we know how to use the I2S Audio Breakout board, it's time to use it in your own project! For more information on the I2S audio breakout, checkout the links below:

Need some inspiration for your next project? Check out some of these related tutorials:

Cackling Apple Head Witch

Make your own cackling apple head witch to scare all of the trick or treaters this Halloween!

SIK Keyboard Instrument

We can use the parts and concepts in the SparkFun Invetor's Kit to make a primitive keyboard instrument.

Vox Imperium: Stormtrooper Voice Changer

Add some flair to your Imperial uniform by changing your voice using a Teensy 3.2 and Prop Shield.

Spectacle Sound Kit Hookup Guide

All the information you need to use the Spectacle Sound Kit in one place.