Including sounds or music in our project will always make it look and sound much more appealing. If you're working with an  Arduino  and have a lot of free spins, you can easily add sound effects to your project by purchasing an extra SD card module and a standard speaker. In this article, I'll show you how to play music and add sound effects with your  Arduino board, as well as introduce the IC LM386 Amplifier  , which we'll use in this process.

We will play the.wav music files stored on an SD card in this project. The Arduino will be programmed to read these.wav files and play the audio on a speak through an LM386 Audio amplifier.

Hardware Required

  Getting Ready with Your WAV Audio Files:

The audio file to be stored on the SD card must be in.wav format and have 44100 Hz, 16-bit stereo quality. We need audio files in.wav format to play sounds from an SD card using Arduino because the Arduino Board can only play audio files in a specific format, which is wav format. There are many mp3 shields available for use with Arduino to create an Arduino mp3 player. Alternatively, to play mp3 files in Arduino, there are websites that will convert any audio file on your computer into that specific WAV file.

 Circuit

The shield is placed on top of the Due, and a micro-SD card is inserted into the slot. The card's root directory contains a.wav file called "test.wav." For a quick test, connect a pair of headphones to the ground and DAC0 while keeping the polarity in mind.

To add a speaker to the board, connect an amplification circuit between the DAC0 pin and the speaker. The amplification circuit will boost the speaker's volume,  There are numerous audio amplifiers available, with the LM386 being one of the most common. The following scheme demonstrates how to construct the circuit using the LM386 and a variety of components. You can power the LM386 by connecting the Vs pin to various voltage sources, such as the +5 V on the Arduino Due's 5V pin or an external 9V  battery,  The capacitor is connected to pins 1 and 8 of the LM386 provides the amplifier's gain. The gain is set to 200 with the 10 F capacitor, and 50 without the capacitor. The volume of the amplifier can be adjusted using the potentiometer.

Caution: Do not connect the speaker directly to the Arduino Due's pins.

Code

 /* 
Modified on Dec 15, 2020
Modified by Harold Bradshaw from Kynix Blog Examples
Home
*/

// CONNECTIONS:
// DS1302 CLK/SCLK --> 5
// DS1302 DAT/IO --> 4
// DS1302 RST/CE --> 2
// DS1302 VCC --> 3.3v - 5v
// DS1302 GND --> GND

#include <ThreeWire.h>  
#include <RtcDS1302.h>

ThreeWire myWire(6,7,8); // IO, SCLK, CE
RtcDS1302<ThreeWire> Rtc(myWire);

void setup () {
    Serial.begin(57600);
    Serial.print("compiled: ");
    Serial.print(__DATE__);
    Serial.println(__TIME__);

    Rtc.Begin();
    RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
    printDateTime(compiled);
    Serial.println();

    if (!Rtc.IsDateTimeValid()) {
        // Common Causes: 1) first time you ran and the device wasn't running yet 2) the battery on the device is low or even missing
        Serial.println("RTC lost confidence in the DateTime!");
        Rtc.SetDateTime(compiled);
    }

    if (Rtc.GetIsWriteProtected()) {
        Serial.println("RTC was write protected, enabling writing now");
        Rtc.SetIsWriteProtected(false);
    }

    if (!Rtc.GetIsRunning()) {
        Serial.println("RTC was not actively running, starting now");
        Rtc.SetIsRunning(true);
    }

    RtcDateTime now = Rtc.GetDateTime();
    if (now < compiled) {
        Serial.println("RTC is older than compile time!  (Updating DateTime)");
        Rtc.SetDateTime(compiled);
    }
    else if (now > compiled)
        Serial.println("RTC is newer than compile time. (this is expected)");
    else
        Serial.println("RTC is the same as compile time! (not expected but all is fine)");
}

void loop () {
    RtcDateTime now = Rtc.GetDateTime();
    printDateTime(now);
    if (!now.IsValid()) {
        // Common Causes: 1) the battery on the device is low or even missing and the power line was disconnected
        Serial.println("RTC lost confidence in the DateTime!");
    }
    delay(1000);
}

#define countof(a) (sizeof(a) / sizeof(a[0]))

void printDateTime(const RtcDateTime& dt) {
    char datestring[20];
    snprintf_P(datestring, 
            countof(datestring),
            PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
            dt.Month(), dt.Day(), dt.Year(),
            dt.Hour(), dt.Minute(), dt.Second() );
    Serial.print(datestring);
}

4.5 Working of this Arduino Music Player:

Simply press the button connected to pin 2 after programming your Arduino, and your Arduino will play the first song (saved as 1.wav) for you. You can now press the button again to change your track to the next song, 2.wav. Similarly, you can listen to all four songs.

You can also play/pause the song by pressing the pin 3 button. Press it once to pause the song and once more to resume it from where it left off. Watch the video below to see the entire process in action (or maybe to relax with some songs).

I hope you had a good time with the project. It is now up to your imagination to incorporate them into your projects. You can create a speaking clock, voice assistant, talking robot, voice alert security system, and many other things.

Know details about Amplifier LM386