In one of my last year’s posts I was describing how to build an Internet Radio using an ESP32. I was using this together with an external DAC to feed an Amplifier.

Lately I got some really cheap Piezo Electric Elements and I was wondering if I could use the solution w/o any additional Audio Electronics Hardware by just using the built in DAC of the ESP32 and connecting the 2 output pins to the Piezo Elements.

To my surprise this works pretty well. It’s just necessary to use some additional ‘surface’ to amplify the sound: Some scrap plastic packaging containers are doing just a great job.

There are no changes to my original sketch:

#include "AudioFileSourceICYStream.h"
#include "AudioGeneratorMP3.h"
#include "AudioOutputI2SNoDAC.h"
#include "WiFi.h"

// Enter your WiFi setup here:
const char *SSID = "mySSID";
const char *PASSWORD = "pwd";

// Randomly picked URL
const char *URL = "http://strm112.1.fm/blues_mobile_mp3";

AudioGenerator *audio;
AudioFileSourceHTTPStream *file;
AudioOutputI2S *out;

void setup() {
    Serial.begin(115200);
    delay(1000);

    WiFi.disconnect();
    WiFi.mode(WIFI_STA);
    WiFi.begin(SSID, PASSWORD);

    // Try to connect to Wifi forever
    while (WiFi.status() != WL_CONNECTED) {
        Serial.println("...Connecting to WiFi");
        delay(1000);
    }
    Serial.println("Connected");

    audioLogger = &Serial;
    file = new AudioFileSourceICYStream(URL);
    file->SetReconnect(5, 0);
    out = new AudioOutputI2S(0, 1); // (0,1) is using the internal DAC
    audio = new AudioGeneratorMP3();
    audio->begin(file, out);
}

void loop() {
    if (audio->isRunning()) {
        audio->loop();
    }
}

The wiring is pretty simple:

Pin Piezo
GPIO25 Red Cable
GND Black Cable

Optionally you can connect a second element to GPIO26.


1 Comment

lex - k · 29. March 2023 at 3:36

Thank you ,

by the way I’ve fried my esp32 connecting it to an amp today lol

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *