In today’s discussion we are going to discuss about an Arduino based music/audio player using micro SD card module and lm386 audio amplifier module. Using this Project, you can play, pause or change a song (audio file of a specific format) using an Arduino board. It’s going to more excited when you build this. So, let’s get started with required components.
Components Required
- Arduino Uno/Nano
- Micro SD Card Adapter Module
- LM386 Audio Amplifier Module
- Push buttons (2 Nos)
- Breadboard
- Connecting Wires
- Speaker
- SD card
Circuit Diagram for Arduino Music Player
The circuit diagram shows the connections between components. The SD card reader, push buttons, and audio amplifier module is connected to the Arduino board. Additionally, a speaker is connected to the amplifier module. The circuit diagram of the project is shown below.
The CS Pin of the SD Card Module is connected to Pin 10. You can connect it to any digital pin of the Arduino board. SCK is connected to Pin 13, the MOSI and MISO pins are connected to Pins 11 and 12 of Arduino respectively. Push buttons are for control the music playback functions like Play / Pause and the next track. The play / Pause button is connected to Pin 3 and the next Track button is connected to Pin 2 of Arduino. All these buttons are configured with internal pullups in the program.
The input pin of the audio amplifier module is connected to pin 9 of Arduino and the output is connected to the speaker. Once you have a circuit you can implement several sound effects in the projects, which are not possible under normal circumstances.
Preparing Audio Files and PCM Library
Audio files:
There are some things you need to keep in mind before proceeding further. The first thing is you have to convert the audio/music files to WAVE format i.e. they should be .wav files. This is because, the Arduino board supports only PCM Audio in WAVE File Format (.wav). For conversion, you can use any audio converter software or website. Here I use a website named ONLINE-CONVERT.com. It supports multiple files like archives, audio, documents, etc.
So, for conversion you can follow the steps given below:
- Go to the website Online Music Converter
- Upload the file you want to convert in wave format
- Change the following accordingly
- Bit resolution: 8 Bit
- Sampling rate: 16000 Hz
- Audio channels: mono
- PCM format: PCM unsigned 8-bit
Now, click on start conversion and download the converted file.
PCM Library:
This is the second important thing to add a special library in the Arduino IDE. The library is TMRpcm developed by TMRh20. From the below given link, you can directly download the zip file:
Now, you can add this Zip file into your Arduino IDE by selecting Sketch->Include Library -> Add .ZIP Library.
Project Building
The audio files are saved into the SD card, so we need to interface the SD card reader module with our Arduino board. The Arduino and SD card communicate using the SPI communication protocol so made the connection accordingly.
Now, Arduino will be able to read the music file from the SD card and through pin 9 it will be played on the speaker. But the audio signals are not so clear hence we have amplified it by using the LM386 audio amplifier module.
The amplifier is designed for the Gain of 200. The Vdd pin is powered by the 5V, if you want to increase/decrease the sound you can increase/decrease the provided voltage. It can withstand a maximum of 15V.
Arduino Programming for Music Player
As always, we need to begin the program by including the required libraries, here we will need the PCM library for Audio player which we discussed earlier.
#include "SD.h" //SD card library
#include "TMRpcm.h" //Audio File Library
#include "SPI.h" //SPI library for SD card
After that we need to check that the SD card is connected well or not.
if (!SD.begin(10))
{
Serial.println("SD fail");
return;
}
Now we have to set the volume level and quality of music by putting these conditions:
music.setVolume(5); // 0 to 7. Set volume level
music.quality(1); // Set 1 for 2x oversampling Set 0 for normal
Now we have to write the condition for play/pause the song and for play the next track as well.
if (button2== LOW) //Button 2 Pressed
{
Serial.println("PLAY / PAUSE");
music.pause();
}
if (button1 == LOW) //Button 1 Pressed
{
song_number++;
if (song_number==7)
{song_number=1;}
If we want to fast forwarding for a certain track then we can use the condition like this:
if (song_number ==1)
{music.play("1.wav",25);} //Play song 1 from 25th second
The complete code and demo video for the same is given at the end of document.
Working of Arduino music player
After all the hardware connections discussed earlier, getting audio files ready, install the required libraries, you are ready to implement your music player using Arduino.
First of all, we need to format the MicroSD Card and then copy all the WAVE audio files to the card. Place the card in the SD Card Module. Connect the Arduino board to the computer and upload the code. Then you can use the Play / Pause Button to play or pause the track and the Next button to play the next track.
Code For Arduino Music Player
#include "SD.h" //SD card library
#include "TMRpcm.h" //Audio File Library
#include "SPI.h" //SPI library for SD card
#define SD_ChipSelectPin 10
TMRpcm music;
int button1, button2;
int song_number=0;
boolean play_pause;
void setup()
{
music.speakerPin = 9; //Audio output pin from Arduino
Serial.begin(9600);
if (!SD.begin(10))
{
Serial.println("SD fail");
return;
}
pinMode(2, INPUT_PULLUP); //Pull up for change track switch
pinMode(3, INPUT_PULLUP); //Pull up for play/pause switch
pinMode(3, INPUT_PULLUP); //Pull up for fast forward
music.setVolume(5); // 0 to 7. Set volume level
music.quality(1); // Set 1 for 2x oversampling Set 0 for normal
}
void loop()
{
button1 = digitalRead(2);
button2 = digitalRead(3);
Serial.println(button1);
Serial.println(button2);
if (button2== LOW) //Button 2 Pressed
{
Serial.println("PLAY / PAUSE");
music.pause();
}
if (button1 == LOW) //Button 1 Pressed
{
song_number++;
if (song_number==7)
{song_number=1;}
Serial.println("KEY PRESSED");
Serial.print("song_number=");
Serial.println(song_number);
if (song_number ==1)
{music.play("1.wav",25);} //Play song 1 from 10th second
if (song_number ==2)
{music.play("2.wav",20);} //Play song 2 from 33rd second
if (song_number ==3)
{music.play("3.wav",15);} //Play song 3 from start
if (song_number ==4)
{music.play("4.wav",71);} //Play song 4 from 25th second
if (song_number ==5)
{music.play("5.wav");} //Play song 5 from start
if (song_number ==6)
{music.play("6.wav",58);} //Play song 6 from start }
}
delay(200);
}