We saw in the last instalment (here) how to simply play an MP3 on our ESP32. In this article/video we look at how to add volume control and to play all the MP3’s that we put on the root directory of our SD card (ignoring anything else). The video below takes you through all this, in addition the source code and affiliate links are also available further down.
Affiliate links for the items shown: Clicking these costs you the same price but gives me a small commission, please consider supporting this channel by using them 🙂
MAX98357A : https://amzn.to/3eovdrd
(You will need two of these for stereo sound)
Potentiometer : https://amzn.to/3dDBorD
ESP32 : https://amzn.to/2Xzhc3k
Breadboards: https://amzn.to/2THZTvy
Speakers : https://amzn.to/2zBhK0F
16Gb SanDisk SD card : https://amzn.to/2XH1sLA
SD Card Reader (https://amzn.to/2ApOppK), note this is the only one I could quickly find that said it supported 3.3v and it’s for the normal size SD cards not Micro, you could always by a micro one and do my hack
The Demo Code (Full code with volume and ability to cycle through all MP3’s on root of SD card).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
// will play MP3's from the root of an SD card, ignoring other files // By XTronical, www.xtronical.com, use as you wish // Based on work and on the audio library of schreibfaul1 // See github page : https://github.com/schreibfaul1/ESP32-audioI2S // Also has volume control via a potentiometer attached to pin 13 #include "Arduino.h" #include "Audio.h" #include "SD.h" #include "FS.h" // Digital I/O used #define SD_CS 5 #define SPI_MOSI 23 // SD Card #define SPI_MISO 19 #define SPI_SCK 18 #define I2S_DOUT 25 #define I2S_BCLK 27 // I2S #define I2S_LRC 26 #define VolPin 13 Audio audio; uint8_t Volume; // range is 0 to 21 File RootDir; void setup() { pinMode(SD_CS, OUTPUT); digitalWrite(SD_CS, HIGH); SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI); Serial.begin(115200); if(!SD.begin(SD_CS)) { Serial.println("Error talking to SD card!"); while(true); // end program } audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT); RootDir = SD.open("/"); PlayNextSong(); // Play next song, which will be the first at this point } void loop() { audio.loop(); audio.setVolume(GetVolume()); // Check volume level and adjust if necassary } void audio_eof_mp3(const char *info){ //end of file PlayNextSong(); } void PlayNextSong() { bool SongFound=false; bool DirRewound=false; while(SongFound==false) { File entry = RootDir.openNextFile(); if (!entry) // no more files { if(DirRewound==true) // If we've already rewound once then there are no songs to play in this DIR { Serial.println("No MP3 files found to play"); entry.close(); return; } //else we've reached the end of all files in this directory, just rewind back to beginning RootDir.rewindDirectory(); // reset back to beginning DirRewound=true; // Flag that we've rewound } else { if (!entry.isDirectory()) // only enter this if not a DIR { if(MusicFile(entry.name())) // Only enter if one of the acceptable music files { Serial.print("Playing ");Serial.println(entry.name()); audio.connecttoSD(entry.name()); // Play the file SongFound=true; } } } entry.close(); } } bool MusicFile(String FileName) { // returns true if file is one of the supported file types, i.e. mp3,aac String ext; ext=FileName.substring(FileName.indexOf('.')+1); if((ext=="mp3")|(ext=="aac")) return true; else return false; } uint8_t GetVolume() { // looks at the ADC pin that the potentiometer is connected to. // returns the value as a volume setting // The esp32's ADC has linerality problems at top and bottom we will ignore them and only respond to values in the middle range uint16_t VolumeSettingReading; VolumeSettingReading=analogRead(VolPin); if(VolumeSettingReading<25) // because of problems mentioned above, anything below 25 will be 0 volume return 0; if(VolumeSettingReading>4000) // because of problems mentioned above, anything above 4000 will be 21 volume return 21; // If we get this far we are in the middle range that should be linear 500-4000 return uint8_t(((VolumeSettingReading-25)/190)); // this will give the correct 0-21 range } |
Hi, Following your I2S work with interest and fascination. I’ve come across a problem, I’m trying to add a volume control to your Internet Radio project with no success. Everything I’ve tried points to the value of the volume control not being read! I’ve created a standalone sketch using your code to read and display the READ value and the ADJUSTED value which is working fine but just not in the radio project…. Have you any thoughts??