Build it step by step
0 of 7 doneTest the amplifier
Play a tone from the library's example.
Connect to Wi-Fi
Print the IP address.
Play one stream
Use a known plain http station URL.
Add the OLED
Show the station name.
Add the encoder
Turn to change station.
Add mute
Press the knob.
Show the title
Print the song title that the stream sends.
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
- Development BoardsBuyESP32 DevKit V1 × 1
- AudioBuy
- Motors & ActuatorsBuySpeaker 4 Ω 3 W × 1
- SensorsBuy
- DisplaysBuy
- PowerBuy
- Cables & WiresBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| MAX98357A VIN | ESP32 5V (VIN) | Amplifier power |
| MAX98357A GND | ESP32 GND | Ground |
| MAX98357A BCLK | ESP32 GPIO26 | I2S bit clock |
| MAX98357A LRC | ESP32 GPIO25 | I2S word select |
| MAX98357A DIN | ESP32 GPIO22 | I2S data |
| Speaker | MAX98357A + and − outputs | Sound |
| Encoder CLK | ESP32 GPIO32 | Turn |
| Encoder DT | ESP32 GPIO33 | Turn direction |
| Encoder SW | ESP32 GPIO27 | Press for mute |
| Encoder common (GND) | ESP32 GND | Ground |
| OLED SDA and SCL | ESP32 GPIO21 and GPIO19 | I2C display |
| OLED VCC and GND | ESP32 3V3 and GND | Power |
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.
| Check | Expected |
|---|---|
| Power on | Station starts within 10 seconds |
| Turn the knob | Next or previous station |
| Press | Silence, press again to resume |
| Move away from the router | Gaps 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 →
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.
