Stereo I2S Audio Amplifier - Sound over SD card
Initialization
Important notes for uploading code:
- Under Tools -> Board, select ESP32 Dev Module and correct serial port
- Set:
- PSRAM: Enabled
- Partition Scheme: Huge APP (NO OTA)
To use the amplifier we need to initialize the audio object by including the library and setting the pins to which the amplifier board is connected.
#include "Soldered_I2S_Audio_Amplifier.h"
// Declare amplifier GPIOs used
#define I2S_DOUT 25
#define I2S_BCLK 27
#define I2S_LRCLK 26
I2SAudio audio;
// Audio callback function, gives full information of current audio source (optional but good to have)
void my_audio_info(I2SAudio::msg_t m)
{
Serial.printf("%s: %s\n", m.s, m.msg);
}
void setup()
{
// Declare callback function
I2SAudio::audio_info_callback = my_audio_info;
// Set pins used to connect to amplifier board
audio.setPinout(I2S_BCLK, I2S_LRCLK, I2S_DOUT);
}
I2SAudio audio()
Create I2SAudio object and setup I2S configuration.
Returns type: None
Function parameters:
| Type | Name | Description |
|---|---|---|
uint8_t | I2S_PORT | I2S controller port number |
I2SAudio::audio_info_callback
Sets audio callback function to log the events. Gives detailed message about current audio source.
audio.setPinout()
Sets the pins for I2S interface protocol.
Returns type: Bool
Returns value: Returns true if setup was successful.
Function parameters:
| Type | Name | Description |
|---|---|---|
uint8_t | BCLK | Bit clock pin |
uint8_t | LRCLK | Left/Right clock for channel selection |
uint8_t | DOUT | Data out pin from microcontroller |
Playing audio from SD card
To play audio from SD card, SPI communication is initialized to read the files on the card. Functions connecttoFS() and setVolume() are used to select the audio file and volume control.
// Declare SD card SPI pins
#define SD_CS 5
#define SPI_MOSI 23
#define SPI_MISO 19
#define SPI_SCK 18
void setup()
{
// Manually control the SPI chip select, pulled high to deselect the bus
pinMode(SD_CS, OUTPUT);
digitalWrite(SD_CS, HIGH);
// Begin SPI communication with set frequency at 1MHz
SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI);
SPI.setFrequency(1000000);
// Initialize the SD card over SPI bus
SD.begin(SD_CS);
// Set volume, default 0 ... 21
audio.setVolume(5);
// IIR filter to adjust bass and treble (3 band equalizer)
// setTone(0, 0, 0) is default setting, values can be between -40 ... +6 (dB)
// audio.setTone(2, -10, 2);
// setBalance(); values between -16 ... 16 mute the left or right channel
// audio.setBalance(16);
// Open stored file from SD card in correct format and pass to audio buffer
audio.connecttoFS(SD, "example.wav");
}
void loop()
{
// audio.loop() must be called constantly
audio.loop();
// Prevent distortion
vTaskDelay(1);
}
audio.setVolume()
Control the output volume of the speakers.
Returns type: None
Function parameters:
| Type | Name | Description |
|---|---|---|
uint8_t | vol | Set volume to value from 0 to 21. |
audio.setTone()
Built-in IIR filter that simulates a 3 band equalizer.
Returns type: None
Function parameters:
| Type | Name | Description |
|---|---|---|
int8_t | gainLowPass | Adjusts the low frequencies gain (bass). |
int8_t | gainBandPass | Adjusts the mid frequencies gain (midrange). |
int8_t | gainHighPass | Adjusts the high frequencies gain (treble). |
audio.setBalance()
Controls the Left/Right channel balance.
Returns type: None
Function parameters:
| Type | Name | Description |
|---|---|---|
int8_t | balance | Control output channel balance, values between -16 ... 16 mute the left or right channel. |
audio.connecttoFS()
Opens the file specified on SD card and passes it to audio buffer for playback.
Returns type: Bool
Returns value: Returns true if file was found in correct format and passed to buffer.
Function parameters:
| Type | Name | Description |
|---|---|---|
fs::FS | fs | File system module. |
const char* | path | Specify the file name on SD card with correct format |
audio.loop()
Keep audio process running for audio playback, must be called constantly.
Full Example
Audio_Over_SD_Card.ino
Full example on GitHub