Projects / ESP32 internet radio

ESP32 internet radio

Intermediate3 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. ESP32

    Test the amplifier

    Play a tone from the library's example.

  2. ESP32

    Connect to Wi-Fi

    Print the IP address.

  3. ESP32

    Play one stream

    Use a known plain http station URL.

  4. ESP3214.3 mASSD1306

    Add the OLED

    Show the station name.

  5. ESP32

    Add the encoder

    Turn to change station.

  6. ESP32

    Add mute

    Press the knob.

  7. ESP32

    Show the title

    Print the song title that the stream sends.

In short

How do you build an ESP32 internet radio?

An ESP32 streams internet radio over Wi-Fi to an I2S amplifier, with an encoder to change stations and an OLED showing the name. You need ESP32 DevKit V1, MAX98357A I2S amplifier breakout, Speaker 4 Ω 3 W, Rotary encoder module (KY-040), SSD1306 OLED Display, 5 V 2 A USB adapter and USB A to micro-USB cable (data). It takes 7 steps and about 180 minutes.

Level
Intermediate
Time
About 180 min
Parts
7

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
MAX98357A VINESP32 5V (VIN)Amplifier power
MAX98357A GNDESP32 GNDGround
MAX98357A BCLKESP32 GPIO26I2S bit clock
MAX98357A LRCESP32 GPIO25I2S word select
MAX98357A DINESP32 GPIO22I2S data
SpeakerMAX98357A + and − outputsSound
Encoder CLKESP32 GPIO32Turn
Encoder DTESP32 GPIO33Turn direction
Encoder SWESP32 GPIO27Press for mute
Encoder common (GND)ESP32 GNDGround
OLED SDA and SCLESP32 GPIO21 and GPIO19I2C display
OLED VCC and GNDESP32 3V3 and GNDPower

Code

The MAX98357A is a digital amplifier that takes the I2S signal directly, so no separate DAC is needed. It gives about 3 W into a 4 Ω speaker from 5 V; keep the volume modest at first, as it can be loud and may make the USB supply sag. The library handles MP3 and AAC streams over plain http; many stations offer such a link, and some only offer https, which may need extra setup. Fill in the Wi-Fi placeholders yourself and keep them out of shared code. Station addresses change over time, so keep the list easy to edit. The encoder code is a simple interrupt reader; if steps skip, add a 100 nF capacitor from each encoder pin to ground. Arduino IDE with the ESP32 boards package, board "ESP32 Dev Module".

Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// Internet radio: ESP32 + MAX98357A. Libraries: ESP32-audioI2S (schreibfaul1), Adafruit SSD1306.
#include <WiFi.h>
#include <Audio.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

const char* WIFI_SSID = "YOUR_WIFI_NAME";
const char* WIFI_PASS = "YOUR_WIFI_PASSWORD";

// Replace with stations you want. Use plain http streams that the library can play.
const char* NAMES[] = {"Station one", "Station two", "Station three"};
const char* URLS[]  = {"http://example.com/stream1.mp3", "http://example.com/stream2.mp3", "http://example.com/stream3.mp3"};
const int NSTATIONS = 3;

#define I2S_BCLK 26
#define I2S_LRC  25
#define I2S_DOUT 22
#define ENC_A 32
#define ENC_B 33
#define ENC_BTN 27

Audio audio;
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
int station = 0; bool muted = false;
volatile int steps = 0; int lastA = HIGH;

void IRAM_ATTR onTurn() {
  int a = digitalRead(ENC_A);
  if (a != lastA && a == LOW) steps += (digitalRead(ENC_B) == HIGH) ? 1 : -1;
  lastA = a;
}

void showStation() {
  oled.clearDisplay(); oled.setTextColor(SSD1306_WHITE);
  oled.setTextSize(1); oled.setCursor(0, 0); oled.print("Internet radio");
  oled.setTextSize(2); oled.setCursor(0, 24); oled.print(NAMES[station]);
  oled.setTextSize(1); oled.setCursor(0, 56); oled.print(muted ? "Muted" : "Playing");
  oled.display();
}

void play() { audio.connecttohost(URLS[station]); showStation(); }

void setup() {
  pinMode(ENC_A, INPUT_PULLUP); pinMode(ENC_B, INPUT_PULLUP); pinMode(ENC_BTN, INPUT_PULLUP);
  attachInterrupt(ENC_A, onTurn, CHANGE);
  Wire.begin(21, 19);
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  while (WiFi.status() != WL_CONNECTED) delay(300);
  audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
  audio.setVolume(10);                         // 0 to 21; start low
  play();
}

void loop() {
  audio.loop();                                // must run often
  if (steps != 0) {
    int s = steps; steps = 0;
    station = (station + (s > 0 ? 1 : NSTATIONS - 1)) % NSTATIONS;
    play();
  }
  static unsigned long lastBtn = 0;
  if (!digitalRead(ENC_BTN) && millis() - lastBtn > 300) {
    muted = !muted; audio.setVolume(muted ? 0 : 10); showStation(); lastBtn = millis();
  }
}

void audio_showstreamtitle(const char* info) {   // called by the library with the song title
  oled.fillRect(0, 44, 128, 12, SSD1306_BLACK);
  oled.setTextSize(1); oled.setCursor(0, 44); oled.print(String(info).substring(0, 21));
  oled.display();
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Power onStation starts within 10 seconds
Turn the knobNext or previous station
PressSilence, press again to resume
Move away from the routerGaps appear, then it recovers

If it does not work

No sound

Check BCLK, LRC and DIN against the pins in the sketch.

Stuttering

Move closer to the router or lower the stream bitrate.

Station will not play

Use a plain http URL and check it in a browser.

Hum

Use a good 5 V supply and short speaker wires.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the ESP32 internet radio?

ESP32 DevKit V1, MAX98357A I2S amplifier breakout, Speaker 4 Ω 3 W, Rotary encoder module (KY-040), SSD1306 OLED Display, 5 V 2 A USB adapter and USB A to micro-USB cable (data).

How long does the ESP32 internet radio take to build?

About 180 minutes for a intermediate maker, in 7 steps.

How do I know the ESP32 internet radio works?

Power on: Station starts within 10 seconds. Turn the knob: Next or previous station. Press: Silence, press again to resume.

No sound: what should I check?

Check BCLK, LRC and DIN against the pins in the sketch.

Stuttering: what should I check?

Move closer to the router or lower the stream bitrate.

Station will not play: what should I check?

Use a plain http URL and check it in a browser.

Hum: what should I check?

Use a good 5 V supply and short speaker wires.