Simple Arduino Audio Player and Amplifier with LM386

Incorporating sounds or music into our project will reliably increase its attractiveness and make it more visually appealing. If your Arduino has many pins to spare, you can easily incorporate sound effects into your project by using an extra SD card module and a standard speaker. This article will show how easy it is to play music or add sound effects using your Arduino Board. The Arduino community is credited for developing specific libraries that streamline the construction of this project. In this configuration, we have employed IC LM386 for amplification as well as for reducing noise.

Hardware Required:

  1. Arduino UNO
  2. SD Card Reader module
  3. SD card
  4. LM386 Audio Amplifier
  5. 10uf Capacitor (2 Nos)
  6. 100uf Capacitor (2 Nos)
  7. 1K,10K Resistor
  8. Push buttons (2 Nos)
  9. Breadboard
  10. Connecting Wires

Getting ready with your WAV audio files:

To play sounds from an SD Card using Arduino, the audio files need to be in .wav format because the Arduino Board is only capable of playing audio in wav format. Numerous mp3 shields are available for use with an arduino in order to build an mp3 player. You can also use online platforms to convert audio files from your computer to WAV format so you can play mp3 files on Arduino.

To convert an audio file into wav format, adhere to these instructions:

Step 1: Click on “Online Wav Converter” to enter into the website.

Step 2: Arduino is capable of playing a wav file in the specified format. You have the option to adjust the settings afterwards, however, these settings were specifically designed for optimal quality.

Bit Resolution

8 Bit

Sampling Rate

16000 Hz

Audio Channel

Mono

PCM format

PCM unsigned 8-bit

Step 3:On the website, you should select “choose file” and pick the file you wish to convert. Next, enter the settings mentioned above. After finishing, it should appear similar to what is shown in the image below.

Step 4: Next, select “Convert File” and your Audio file will be converted to .Wav file format. Once the conversion is finished, it will be downloaded.

Step 5: In the end, format your SD card and save your .wav audio file on it. Make sure to format it before you insert this document. Remember to include the title in your audio recording. Similarly, you can select any of your four audio files and designate them as 1, 2, 3, and 4 (Titles must stay the same). I have converted four songs and saved them as 1.wav, 2.wav, 3.wav, and 4.wav as specified.

Circuit and Hardware:

Circuit Diagram for this Arduino Audio File Player is simple. The complete circuit diagram is shown in the Image below.

Because our audio files are saved on the SD card, we attach an SD card reader module to our Arduino. The Arduino communicates with the SD card via the SPI communication protocol. Hence, the Module is linked to the SPI pins of the Arduino as shown in the above diagram. It is also listed in the table provided.

Arduino

SD card module

+5V

Vcc

Gnd

Gnd

Pin 12

MISO (Master In Slave out)

Pin 11

MOSI (Master Out Slave In)

Pin 13

SCK (Synchronous Clock)

Pin 4

CS (Chip Select)

Now the Arduino will be able to read the music file from the SD card and play it on the pin number 9. But the audio signals produced by the Arduino on pin 9 will not be audible much. Hence we amplify it by using the LM386 Low voltage Audio amplifier IC.

The amplifier shown above is designed for a Gain of 200 and the Vdd (pin 6) is powered by the 5V pin of the Arduino. If you want to increase/decrease the sound you can increase/decrease the voltage provided to this pin. It can withstand a maximum of 15V. Learn more about this 200 gain amplification configuration for LM386 here.

Additionally, there are two push buttons connected to pins 2 and 3 of the Arduino. These switches are used for moving to the next song track and either starting or stopping the music. I have used these buttons just to demonstrate their capabilities; feel free to start the song whenever you want. View the demo video at the end.

You can choose to build this circuit exclusively on a Breadboard, as shown in the accompanying picture.

Programming your Arduino:

Once the Hardware and SD card are prepared, we are only one more step from being able to play those songs. Put the card into the module for the SD card and then proceed with the steps provided.

Step 1: We will use a library to guarantee the project’s success as mentioned before. Here is the provided link to the library. Choose “Clone or download” and click on it, then opt for downloading as a ZIP file.

Step 2: We will use a library to guarantee the project’s success as mentioned before. Here is the provided link to the library. Choose “Clone or download” and click on it, then opt for downloading as a ZIP file.

Step 3:

By the end of this article, the complete schedule for the arduino music player project will be provided. Simply duplicate and insert it within the Arduino Program. Afterward, choose Upload and get ready to begin listening to your audio files.

The comments in the program are helpful in aiding comprehension. Nevertheless, the TMRpcm library’s capability has also been outlined in the next section.

Playing an audio file:

You can play any audio that is stored in Wav format inside the SD card module by using the line below.

music.play(“3.wav”);
//object name.play (“FileName.wav”);

You can use this line at places where you want to trigger the Audio

Pause an audio File:

To pause an Audio file, you can simply call the line below.

music.pause();
//objectname.pause();

Forwarding/Rewinding an Audio: 

There are not direct ways to forward or rewind an Audio file, but you can use the line below to play a song at a particular time. This can be used to forward/rewind with some additional programming.

music.play(“2.wav”,33); //Plays the song from 33rd second
//objectname.play(“Filename.wav”,time in second);

Setting the quality of the audio:

The library gives us two qualities to play the music, one is to play as normal mode the other to play with 2X oversampling.

music.quality(0); //Normal Mode
music.quality(1); //2X over sampling mode

Setting the Volume of the audio:

Certainly, you have the ability to adjust the audio volume using software. Just use the line below to easily adjust the volume. Increased music levels can impact audio quality, so it is recommended to utilize hardware control when available.

Simple Arduino Audio Player and Amplifier with LM386 schematic
music.setVolume(5); //Plays the song at volume 5
//objectname.setVolume(Volume level);

Working of this Arduino Music Player:

After coding your Arduino, simply push the button connected to pin 2 in order to hear the first song (called 1.wav) on your Arduino board. Now, you can choose to press the button again to switch to the next track, scheduled to play as 2.wav. All four songs can be accessed in a similar manner.

Pushing the button linked to pin 3 enables you to control the song’s playback. Push the button once to pause the song and push it again to continue playing from where it stopped. Watch the video for a complete demonstration (or just relax with some music).

Arduino Music Player

I believe that you valued the project. Now it’s time for you to use your imagination and incorporate them into your projects. You can make a talking clock, voice-controlled assistant, chatty robot, vocal warning security system, and various other creations. Please let me know how you plan to use it in the comment section and if you have any problems, feel free to reach out to me on the forums or in the comment section.

Code

/*
Arduino Based Music Player

This example shows how to play three songs from SD card by pressing a push button

The circuit:
* Push Button on pin 2 and 3
* Audio Out – pin 9
* SD card attached to SPI bus as follows:
** MOSI – pin 11
** MISO – pin 12
** CLK – pin 13
** CS – pin 4

created  25 Jun 2017
by Aswinth Raj

This example code was created for CircuitDigest.com

*/

#include “SD.h” //Lib to read SD card
#include “TMRpcm.h” //Lib to play auido
#include “SPI.h” //SPI lib for SD card

#define SD_ChipSelectPin 4 //Chip select is pin number 4
TMRpcm music; //Lib object is named “music”

int song_number=0;
boolean debounce1=true;
boolean debounce2=true;
boolean play_pause;

void setup(){
music.speakerPin = 9; //Auido out on pin 9
Serial.begin(9600); //Serial Com for debugging
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println(“SD fail”);
return;
}

pinMode(2, INPUT_PULLUP); //Button 1 with internal pull up to chage track
pinMode(3, INPUT_PULLUP); //Button 2 with internal pull up to play/pause
pinMode(3, INPUT_PULLUP); //Button 2 with internal pull up to fast forward

music.setVolume(5);    //   0 to 7. Set volume level
music.quality(1);        //  Set 1 for 2x oversampling Set 0 for normal
//music.volume(0);        //   1(up) or 0(down) to control volume
//music.play(“filename”,30); plays a file starting at 30 seconds into the track
}

void loop()
{

if (digitalRead(2)==LOW  && debounce1 == true) //Button 1 Pressed
{
song_number++;
if (song_number==5)
{song_number=1;}
debounce1=false;
Serial.println(“KEY PRESSED”);
Serial.print(“song_number=”);
Serial.println(song_number);

if (song_number ==1)
{music.play(“1.wav”,10);} //Play song 1 from 10th second

if (song_number ==2)
{music.play(“2.wav”,33);} //Play song 2 from 33rd second

if (song_number ==3)
{music.play(“3.wav”);} //Play song 3 from start

if (song_number ==4)
{music.play(“4.wav”,25);} //Play song 4 from 25th second

if (digitalRead(3)==LOW  && debounce2 == true) //Button 2 Pressed
{
music.pause();  Serial.println(“PLAY / PAUSE”);
debounce2=false;
}

if (digitalRead(2)==HIGH) //Avoid debounce
debounce1=true;

if (digitalRead(3)==HIGH)//Avoid debounce
debounce2=true;
}

}

Video

Source : Simple Arduino Audio Player and Amplifier with LM386


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

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

Scroll to Top