Arduino Getting Started Tutorial 3 - Using RapidUp SAMD21 to Play Audio via MAX98357A Over I2S

Using RapidUp SAMD21 to Play Audio via MAX98357A Over I2S

Introduction

The MAX98357A is a digital-to-analog converter (DAC) with a built-in amplifier that allows easy audio playback using the I2S protocol. The RapidUp SAMD21 features built-in I2S capabilities, making it ideal for audio applications. This guide will show you how to connect the MAX98357A to the RapidUp SAMD21 and play an audio file.

Materials Needed

  1. RapidUp SAMD21

  2. I2S AUDIO SPEAKER - MAX98357A

  3. Speaker (4Ω or 8Ω)

  4. MicroSD card Board(formatted as FAT32)

  5. Jumper wires

Audio Wiring Diagram

You can directly connect two boards or connect the MAX98357A to the RapidUp SAMD21 as follows:

MAX98357A Pin RapidUp SAMD21 Pin
VIN 3.3V
GND GND
LRCLK No.13 (FS)
BCLK No.14 (SCK)
DIN No.15 (SDOUT)
SD 3.3V (Always enabled)

Connect the speaker to the MAX98357A’s + and - output terminals.

SD CARD BOARD Wiring Diagram

Connect the SD CARD to the RapidUp SAMD21 as follows:

SD CARD BOARD Pin RapidUp SAMD21 Pin
VIN 3.3V
GND GND
CS No.7
SCK No.6
MOSI No.5
MISO No.8


Installing Required Libraries

 To play audio files from an SD card, install the following libraries in the Arduino IDE:

  1. ArduinoSound (for I2S audio playback)

  2. SD (for reading audio files from the SD card)

You can install these from the Arduino Library Manager.

Arduino Code to Play Audio

#include <ArduinoSound.h>
#include <SD.h>
#include <SPI.h>

const int chipSelect = SDCARD_SS_PIN; // SD card chip select pin
File audioFile;

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

    if (!SD.begin(chipSelect)) {
        Serial.println("SD card initialization failed!");
        while (1);
    }
    Serial.println("SD card initialized.");

    if (!Audio.begin(44100, 16)) {
        Serial.println("Failed to initialize I2S audio!");
        while (1);
    }

    audioFile = SD.open("/test.wav");
    if (!audioFile) {
        Serial.println("Failed to open audio file!");
        while (1);
    }
}

void loop() {
    if (audioFile) {
        Audio.play(audioFile);
    }
}

Explanation of Code

  1. The SD.begin() function initializes the SD card.

  2. Audio.begin() initializes the I2S interface with a 44.1kHz sampling rate and 16-bit            resolution.

  3. The SD.open("/test.wav") function loads the audio file from the SD card.

  4. The Audio.play(audioFile) function plays the WAV file continuously.

Preparing the Audio File

Ensure that your audio file meets these requirements:

  1. Format: WAV

  2. Bit depth: 16-bit

  3. Sample rate: 44.1kHz

  4. Channels: Mono

Use audio editing software like Audacity to convert files if needed.

Conclusion

With this setup, your RapidUp SAMD21 can play high-quality audio using the MAX98357A over I2S. You can expand this project by adding volume control, multiple tracks, or even real-time audio streaming! Rapid Up!

 

Back to blog