ESP32 Based Internet Radio using MAX98357A I2S Amplifier Board

audio

BySourav Gupta 3

ESP32 Based Internet Radio using MAX98357A I2S Amplifier Board

Analog Radios are getting extinct in this digital world since everything, from movies to songs is being directly streamed from the Internet today. Let’s see how an Internet radio can be made by leveraging some simple digital electronics, coding, and with an active Internet connection. Now, those who are following us regularly might already know that we have built a Raspberry Pi based Internet Radio previously, but in this tutorial, we will further simplify it by just using an ESP32.

Internet Radio, also known as streaming, web, IP, or online radio is generally used to telecast messages, music, discussions, etc. in the form of audio. In other words, it is a digital audio service transmitted via the Internet. However, broadcasting on the Internet or web is usually referred to as webcasting as no transmission happens broadly through wireless means.

The Internet radio can be created as a stand-alone device that is running through the internet, or as software running through a computer that is connected with the internet. Here, we will make a stand-alone device that will be connected to the Wi-Fi, access the internet, and finally play the audio. To build our ESP32 web radio, we have chosen the ESP32 development board (obviously) and the MAX98357A I2S Amplifier. If you are looking to build a simple FM Radio that can work offline, then you should check the linked article.

Components Required to build ESP32 Web Radio

You will need the following components to build our Internet radio using ESP32. Most of the parts should be available easily.

  1. ESP32 Devkit
  2. MAX98357A
  3. 8-Ohms Speaker with a rating of 0.5W to 1W rating.
  4.  USB Cable
  5. 5V 1A charger
  6. Active internet connection

ESP32 and I2S Communication

As most of us already know, ESP32 is a very low-cost, low-power system on chip microcontrollers that has integrated Wi-Fi and dual-mode Bluetooth. We have previously built many ESP32 projects, you can check them out if interested. However, we are not going to use the Bluetooth functionality of ESP32 here, but still, we have selected ESP32 for the three reasons listed below.

  1. It has Wi-Fi Module
  2. It is Low cost
  3. It has I2S peripheral support.

Now, why is I2S important?

The Integrated Inter-IC Sound Bus (I2S) is a serial bus interface standard that is used for connecting digital audio devices. It is made by Philips Semiconductor. This interface is most commonly used by audio ADC and DAC components.

Since for running a radio we need to use a Speaker and need to produce analog output, we need some sort of Digital-to-Analog converter. Here comes the interesting part of the project. The MAX98357A module. It offers a Digital Class D amplifier that uses the Digital Audio Interface or I2S to communicate with microcontroller units.

The benefit of MAX98357A I2S module is listed below:

  1. It is 92% efficient
  2. Could provide 3.2W output Power into 4 Ohms load at 5V
  3. Click and Pop-up noise reduction
  4. Short circuit and thermal protection
  5. Supports Left, Right or (Left/2 + Right/2) Output
  6. Works from 2.5V to 5.5V.

The MAX98357A I2S module that we have used in this project is shown below. 

MAX98357A I2S Module

Circuit Diagram to Interface MAX98357A I2S Module with ESP32

ESP32 Internet Radio Player Circuit Diagram

The schematic is simple. MAX98357 connection is given in the below table.

Module Pin

Connection

Description

LRC

IO26

Left Right Clock

BCLK

IO27

Bit Clock Input

DIN

IO25

Digital Data input

GAIN

GND

Gain Select. If connected with VDD, Gain is 6dB, If GND, Gain 12dB.

SD

-

Shutdown. To activate, pull it low.

GND

GND

Supply Ground

VIN

5V

Voltage Input

Since the Gain pin is connected with the GND, the gain is 12dB.

Internet Radio Code for ESP32

Well, this project uses the ESP32-audioI2S library that is available on GitHub. Before we get into the code, make sure you download the library from the below link and add it to Arduino IDE.

ESP32 Audio I2S Library

The full code is provided at the bottom of this page, you can directly copy and use the code, but if you want its explanation, then read further. 

First, the libraries required for the project are included, as shown below.

#include "Arduino.h"
#include "WiFi.h"
#include "Audio.h"

Then, the pinouts of the MAX98357A are specified. If you are using the exact same module shown above you can leave it as shown below. 

#define MAX98357A_I2S_DOUT  25
#define MAX98357A_I2S_BCLK 27
#define MAX98357A_I2S_LRC  26
Audio audio;

Put your Wi-Fi SSID and Password, this allows the ESP32 to connect to the internet.

String ssid = "Your User Id";
String password = "Your Password";

In the setup, the WiFi password and SSID are being set, and the WiFi is started. After the Wi-Fi is connected, the Audio Pinouts are selected, the volume is also set to the maximum.

  audio.setPinout(MAX98357A_I2S_BCLK, MAX98357A_I2S_LRC, MAX98357A_I2S_DOUT);
  audio.setVolume(100);

Set the streaming URL here, you can choose any streaming URL of your choice, you can also introduce a potentiometer to switch between multiple streaming channels if needed.

  audio.connecttohost("http://vis.media-ice.musicradio.com/CapitalMP3");
}

After all of this, the audio is played continuously in the void loop using the below code.

void loop()
{
  audio.loop();
}

Testing our ESP32 Internet Radio Player

The circuit is connected properly as per the schematic and connected with the Wi-Fi. As soon as the set-up is powered it connects to the internet and starts streaming the audio from the URL mentioned in the program.

ESP32 Internet Radio Player

I also found that the audio is quite clear and very usable without any additional amplifiers in between. You can check out the video below that demonstrates the complete project to test the audio quality yourself.

Hope you enjoyed this ESP32 Radio project and would build one on your own. Again, this is a very simple set-up and you can improvise it with a display and some control knob to switching between multiple radio stations and even control the volume just like a traditional radio. If you have any questions or ideas, feel free to drop them in the comment section below.

Code

#include "Arduino.h"
#include "WiFi.h"
#include "Audio.h"
#define I2S_DOUT     25
#define I2S_BCLK      27
#define I2S_LRC        26
Audio audio;
String ssid =    "Sourav";
String password = "87654321";
void setup() {
  WiFi.disconnect();
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid.c_str(), password.c_str());
  while (WiFi.status() != WL_CONNECTED)
  delay(1500);
  audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
  audio.setVolume(100);
  audio.connecttohost("http://vis.media-ice.musicradio.com/CapitalMP3");
}
void loop()
{
  audio.loop();
}

Video

Get Our Weekly Newsletter!

Subscribe below to receive most popular news, articles and DIY projects from Circuit Digest

Comments

  • alain piret's picture
    alain piret
    Jul 26, 2021

    bonjour a vous et merci pour le tuto

    pourriez vous expliquer la marche à suivre pour introduire un autre URL ? avec un exemple concret

    je ne comprends pas l'ajout d'un potentiometre, ne serait il pas plutot un bouton poussoir ?

    Ou le raccorder? à quelles broches du esp ?

    D'avance un grand merci !

    Piret Alain

     

  • David Harkness's picture
    David Harkness
    Oct 12, 2021

    Really useful project, i needed something simple to understand how it worked, and now i understand completely, all others i have looked at were a bit complicated, now i feel i can make my Internet Radio/Blutooth/USB/Memory card Player: Thanks :)

Log in or register to post Comment