I Spent 20€ at a Flea Market and made Something AMAZING! (Internet Radio)

Creating a Radio Controller with ESP32 and Touchscreen Interface

===========================================================

As a hobbyist, I wanted to create a radio controller that could switch between different German stations using an ESP32 microcontroller and a touchscreen interface. In this project, I'll guide you through the steps of creating a custom radio controller that can easily switch between five different radio stations.

Hardware Components

-----------------

* ESP32 microcontroller

* 5x GPIO pins for input and output

* I2S amplifier board for converting digital audio signal to analogue

* 20W ClassD amplifier board for amplifying the audio signal

* Old available speaker inside the radio (optional)

* STM32 DISCO Development board with touchscreen LCD

Software Components

-----------------

* TouchGFX library for creating a graphical user interface

* STM32CubeIDE for programming the ESP32 microcontroller

* Custom code for switching between radio stations and handling touchscreen input

Step 1: Setting up the Hardware

------------------------------

First, let's set up the hardware components. I defined 5 pins of the microcontroller as inputs and added some simple lines of code to switch over to the selected radio station as soon as the corresponding pin gets connected to a high voltage.

```c

// Set up GPIO pins for input and output

const int radioPin1 = 2;

const int radioPin2 = 4;

const int radioPin3 = 5;

const int radioPin4 = 6;

const int radioPin5 = 8;

void setup() {

// Initialize GPIO pins as inputs

pinMode(radioPin1, INPUT);

pinMode(radioPin2, INPUT);

pinMode(radioPin3, INPUT);

pinMode(radioPin4, INPUT);

pinMode(radioPin5, INPUT);

// Initialize I2S pins for outputting digital audio signal

Serial.begin(115200);

}

void loop() {

// Read input from GPIO pins

int radio1 = digitalRead(radioPin1);

int radio2 = digitalRead(radioPin2);

int radio3 = digitalRead(radioPin3);

int radio4 = digitalRead(radioPin4);

int radio5 = digitalRead(radioPin5);

// Switch to selected radio station

if (radio1 == HIGH) {

// Code to switch to radio station 1

} else if (radio2 == HIGH) {

// Code to switch to radio station 2

} else if (radio3 == HIGH) {

// Code to switch to radio station 3

} else if (radio4 == HIGH) {

// Code to switch to radio station 4

} else if (radio5 == HIGH) {

// Code to switch to radio station 5

}

}

```

Step 2: Creating the Graphical User Interface

------------------------------------------

Next, I created a graphical user interface using TouchGFX library on the STM32 DISCO Development board. The UI consists of toggle buttons for each radio station that can be selected and deselected.

```c

// Create toggle buttons for radio stations

ToggleButton radio1Button(100, 200);

ToggleButton radio2Button(150, 200);

ToggleButton radio3Button(200, 200);

ToggleButton radio4Button(250, 200);

ToggleButton radio5Button(300, 200);

void setup() {

// Initialize UI components

radio1Button.begin();

radio2Button.begin();

radio3Button.begin();

radio4Button.begin();

radio5Button.begin();

// Add logic to switch to selected radio station when button is pressed

}

void loop() {

// Read input from UI buttons

if (radio1Button.isPressed()) {

// Code to switch to radio station 1

} else if (radio2Button.isPressed()) {

// Code to switch to radio station 2

} else if (radio3Button.isPressed()) {

// Code to switch to radio station 3

} else if (radio4Button.isPressed()) {

// Code to switch to radio station 4

} else if (radio5Button.isPressed()) {

// Code to switch to radio station 5

}

}

```

Step 3: Handling Touchscreen Input

--------------------------------

To handle touchscreen input, I defined additional GPIO pins as outputs and added a couple of lines of code to turn them on or off when the corresponding button is pressed.

```c

// Define additional GPIO pins for output

const int gpio1 = 10;

const int gpio2 = 11;

void setup() {

// Initialize GPIO pins as outputs

pinMode(gpio1, OUTPUT);

pinMode(gpio2, OUTPUT);

// Add logic to turn on or off GPIO pins when button is pressed

}

void loop() {

// Read input from UI buttons

if (radio1Button.isPressed()) {

digitalWrite(gpio1, HIGH);

} else {

digitalWrite(gpio1, LOW);

}

if (radio2Button.isPressed()) {

digitalWrite(gpio2, HIGH);

} else {

digitalWrite(gpio2, LOW);

}

}

```

Step 4: Amplifying the Audio Signal

---------------------------------

To amplify the audio signal, I used a 20W ClassD amplifier board. The amplifier board takes the digital audio signal from the ESP32 microcontroller and amplifies it to a higher level.

```c

// Initialize amplifier board

const int amplifierPin = 12;

void setup() {

// Initialize amplifier pin as output

pinMode(amplifierPin, OUTPUT);

}

void loop() {

// Read input from I2S pins

int i2s1 = analogRead(0); // replace with your I2S pin

int i2s2 = analogRead(1); // replace with your I2S pin

// Amplify audio signal using amplifier board

analogWrite(amplifierPin, map(i2s1, 0, 1023, 0, 255));

}

```

Conclusion

----------

In this project, we created a custom radio controller that can switch between five different German stations using an ESP32 microcontroller and a touchscreen interface. We also amplified the audio signal using a ClassD amplifier board. With this project, you can create your own radio controller with a user-friendly interface and advanced features like amplifying the audio signal.

WEBVTTKind: captionsLanguage: enRecently I have been to a flea market whereI saw this!An old school radio at least 50 years old.Now truth be told I have been looking fora new internet radio for a while now thatcould sit right on my fridge.Only problem is that I am not the biggestfan of modern radio designs and that is whyof course I bought myself the old vintageone for only 20€.And yes while time definitely took its tollon the radio, I just love the wooden enclosurein combination with metal highlights and evena bit of fabric.But of course it was never my intention touse the half a century old electronics insidethe radio.My idea instead was to create a new electronicssystem that can not only playback internetradio livestreams, but should also featureBluetooth Music and should be controllablethrough a touchscreen LCD.Is all of that possible and can you easilybuild it a home?Well, we will find out in this video!Let's get startedThis video is sponsored by Onshape which isthe CAD software I used to design the enclosurefor my touchscreen later on.And if you want to design your own mechanicalparts as well then you are in luck becauseOnshape is free to use for everyone.But of course if you are a business who needsmore capabilities like Simulation, PCB connectoror built in PDM then there are also specialplans for that.Now using the software is pretty straightforwardand super easy to grasp and needless to sayit also comes with all the features you couldever need when it comes to CAD design.And since it is running in your browser youalso do not require powerful hardware to runit.So If I peaked your interest then head overto Onshape.Pro/GreatScott to try it out orclick the link in the video description.Now before getting to the electronics stuffI firstly wanted to refurbish the old radioenclosure and make it all look good.For that I firstly removed the big metal partfrom the inside of the radio onto which allthe electronics parts were mounted to.At this point I of course had to take a closerlook at this beauty and play a bit with thevariable capacitor before then removing thepotentiometer and faceplate and ripping outall of the old electronics components.And as soon as that was done, I cleaned themetal cage with a wet rag which I then alsoused to scrub the radio enclosure on the insideand outside.And after then removing the Grundig lettering,it was time for the step which I honestlywas not so sure about and that was using sandpaperto roughen up the surface of the outer housing.I did this so that my favourite wood stainwould attach nicely and after applying twolayers of it, I have to say that this wasa good decision; at least in my opinion.And after then once again using sandpaperto clean the metal highlights and puttingit all back together, I think there was alreadya big improvement visible.The only thing left that didn't look so goodwas the bent back plate; but luckily all weneed to replicate it, is a 3mm thick sheetof MDF.After clamping the old plate to it, I useda pencil to trace its outline and some ofthe ventilation holes for not only betterlooks but also a special wire later on.Then I simply used a handsaw to cut out mynew backplate and the really cool thing aboutMDF is that you can use a wide variety oftools to create cut-outs because it is a rathersoft material to work with.But anyway after then drilling all the holes,I was done with the backplate for now andcontinued with this AC socket with integratedfuse and switch.As you would imagine this socket later deliversthe 230V mains AC voltage that is necessaryto power all of the electronics and thus Iof course had to mount it to the radios enclosurewhich I did by simply marking the shape ofit onto the metal cage and then using an anglegrinder to create the fitting cut out.After then also drilling mounting holes forit, I secured the metal part once again inthe radio and closed it all up the with backplateto mark the required AC socket cut-out ontothe backplate as well.And the rest was pretty self explanatory andinvolved creating the last cut out, paintingthe back plate with the same wood stain asbefore, clean the faceplate and get that incombination with the potentiometers all backonto the metal cage and finally put everythingback together.And just like that the mechanical part ofthis build was pretty much done and lookinggood which means it was finally time to moveon to the electronics which mainly consistof three big segments: Bluetooth Music, WiFiRadio and Touchscreen support.Let's start off with Bluetooth and WiFi Musicbecause there is actually a powerful microcontrollerthat can do both and that is the ESP32.And luckily for me there exists a popularBluetooth Music Library for this microcontrollerbut sadly this library didn't work at allfor me.Now I was able to connect to the microcontrollervia Bluetooth but no matter what I tried itdidn't feel like outputting any audio information.Of course I tried different settings in thelibrary and also looked for other solutionsbut after wasting way too much time and notgetting any results, I had to scratch theBluetooth function.But on the positive side the WiFi radio functionwas super easy to implement and worked likea charm.Once again an already existing library doesthe trick here in which we only have to addour WiFi information as well as the radioURL we want to open which you usually findon the website of the radio station.All in all I wanted to change between these5 different German stations and to all Germanviewers out there please do not judge me here.Now to easily change between them I defined5 pins of the microcontroller as inputs andadded some simple lines of code whose jobit is to switch over to the selected radiostation as soon as the corresponding pin getsconnected to a high voltage.And the last thing to note here in the codeare the I2S pins which are responsible foroutputting the audio signal.But unlike the analogue audio signal you areprobably more familiar with, the I2S pinsoutput a digital audio signal consisting of1 and 0 meaning we need a special I2S amplifierto convert it back to analogue so that wecan hook up normals speakers.I know it sounds a bit complicated but youcan always check out my dedicated video aboutI2S to learn more.Anyway to drive my speakers sufficiently Iwanted to go with a 20W Amp, but after doingresearch on I2S amps, I could not find a fittingboard and instead only such an IC that shouldbe able to do the trick though.So long story short I created a custom PCBaround it which ultimately though failed tofunction correctly.Yes, failing is also part of engineering andbecause troubleshooting would have taken toomuch time; I simply went with another solutionconsisting of two separate boards.The first one is an I2S Amp that connectsto the ESP and converts the digital signalto an analogue one.And the second board is simply a 20W ClassD Amp which then amplifies the audio for theloud speaker and best of all you can use anormal potentiometer like from the radio tofine adjust the volume level.And in case you are asking yourself here,yes I will be using the old available speakerinside the radio as well because it actuallysounds pretty good.And I would love to present a sound demo herebut as you might know radio stations are notoriousfor broadcasting music that I am not allowedto use in a YouTube video....sorry.OK; with that out of the way most of the electronicsare done and the big thing left was only atouchscreen that could interact with the inputpins of the ESP to change radio stations.And for that I got myself this STM32 DISCODevelopment board that not only comes withan 800x480 pixel Touchscreen LCD but alsowith lots of pins on its backside that wecan use.So with the help of the software TouchGFXI very easily created myself a graphical userinterface consisting of toggle buttons forthe radio stations that could change betweena selected and non selected state/image.And after then filling them up with a bitof logic and uploading the generated codeto the board you can see that my touchscreencontroller was already half way done in lessthan an hour.And the last thing to do now was to get theGPIOs pins to trigger when choosing a station.For that I simply imported my code in theSTM32CubeIDE in which I then defined 5 additionalGPIOs as outputs and had to write a coupleof additional lines of code to basically letthem individually turn on or off.And I know that this step might seem a bitintimidating and if you are lost right nowthen feel free to check out the video descriptionfor more detailed explanations.But anyway after then uploading this finalcode, the touchscreen and GPIO pins seemsto react accordingly which means it was timeto design an enclosure for the touchscreenwith the help of Onshape, 3D print it all,solder a cable with lots of wires to the touchscreenpins, connect that to the ESP, for which Ialso already prepared a new perfboard withall the mandatory components on it, and ultimatelyput the screen in its enclosure.And according to my final wiring diagram onlythe two power supplies were missing but theywere not really hard to find and also prettyeasy to wire up.So to finish this project, I pretty much onlydid the remaining wire connections accordingto my diagram which also includes the mainsvoltage input and here I have to warn youthat working with mains voltage should onlybe done by a professional, you have been warned.With that being said all that was left forme to do was getting everything in place andclosing everything up and just like that myproject came to an end.And I got to say I am really satisfied withmy new radio and all in all it was not suchan expensive project to pull off.I hope you enjoyed it and maybe got inspiredto make something on your own.If so consider supporting me through Patreonto keep the show going.Don't forget to like, share, subscribe andhit the notification bell.Stay creative and I will see you next time.