In this project, we will building a music player using an Arduino Nano and SD card reader. The music player will be capable of playing MP3 files stored on an SD card in wave form and we will have basic controls for play, pause, forward and backward. The project is a great introduction to working with Arduino boards and programming them to control external hardware.

To build the music player, we will need an Arduino Nano, an SD card reader module, a speaker, some connecting wires, and a breadboard. We will also need to install an MP3 library for Arduino Nano, which will allow us to decode and play MP3 files from the SD card in the wave form. Once we have all the components and libraries in his place, we will start by make the circuit on the breadboard and then move on to programming the Arduino to read MP3 files from the SD card and play them through the speaker.

 

Component Required

To build a DIY music player with Arduino Nano and SD Card Reader, you will need the following components:

 

SD Card Reader Pinout 

SD Card Reader Pinout

The pinout of an SD (Secure Digital) card reader can vary depending on the specific model or type of reader. However, here is a typical pinout for an SD card reader:

  1. VCC - power supply (3.3V or 5V)
  2. GND - ground
  3. MOSI - master output, slave input (data line)
  4. MISO - master input, slave output (data line)
  5. CS - chip select (slave select)
  6. SCK - clock signal

 

Working of SD Card Reader

The working of an SD card reader typically involves several steps, which I will outline below:

Power supply: The SD card reader is typically powered by the host device, such as a computer or microcontroller that it is connected to. The SD card itself also requires power to operate.

Initialization: The host device initializes the SD card reader by sending it a series of commands, which may include a reset command and a command to set the communication speed.

Card detection: The SD card reader detects the presence of an SD card by monitoring the state of the card detect pin (usually labeled CD or CS). When an SD card is inserted, the card detect pin is pulled low, indicating that a card is present.

Command transmission: The host device communicates with the SD card by sending it a series of commands, which are typically in the form of SPI (Serial Peripheral Interface) commands. These commands include commands to read and write data, as well as commands to control the operation of the SD card.

Data transfer: The host device transfers data to and from the SD card by sending and receiving SPI data packets. The SD card typically operates in one of two modes: block mode, where data is transferred in fixed-size blocks, or byte mode, where data is transferred one byte at a time.

Error handling: The SD card reader and host device perform error checking and correction to ensure that data is transferred accurately and reliably. This may involve sending checksums or error codes to detect and correct errors during data transfer.

Overall, the SD card reader acts as an interface between the host device and the SD card, facilitating communication and data transfer between the two.

 

Library

Additionally we need and Arduino library ,in IDL go to Tools < Manager library < and search for TMRpcm and click and install.

 

Audio Convert Into Wave Form

A software we generally use to convert Audio signal into wave form. - Convert audio to WAV

 

Circuit Diagram of Arduino Music System

Circuit diagram of Arduino music system

Here you can see interfacing of Arduino Nano with SD card reader. Connected with amplifier and speaker. In this music system, we use SD card reader connected his vcc with 5 voltage and ground connect with GND. Here MISO pin connected with D12 in Arduino Nano. Here we use MOSI connected with D11 pin in Arduino Nano where we connect SCK with D13 pin and CS pin connected with D4. Now we connect three button with D5, D6, D7 in Arduino Nano.

Now we connect D9 and ground with amplifier and another terminal of this amplifier connected with speaker.

 

Arduino Code 

#include <SD.h> // need to include the SD library

#define SD_ChipSelectPin 4 //connect pin 4 of arduino to cs pin of sd card

#include <TMRpcm.h> //Arduino library for asynchronous playback of PCM/WAV files

#include <SPI.h> //  need to include the SPI library

TMRpcm tmrpcm; // create an object for use in this sketch

int temp=1;

int pp=5;

int next=6;

int prev=7;

void setup()

{

 pinMode(pp,INPUT_PULLUP);

 pinMode(next,INPUT_PULLUP);

 pinMode(prev,INPUT_PULLUP);

 tmrpcm.speakerPin = 9; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc

 Serial.begin(9600);

 if (!SD.begin(SD_ChipSelectPin)) // returns 1 if the card is present

 {

  Serial.println("SD fail");

  return;

 }

 tmrpcm.setVolume(5); //

 tmrpcm.play("song1.wav"); //the sound file "song" will play each time the arduino powers up, or is reset

                          //try to provide the file name with extension                

void loop() {

  while(digitalRead(pp)==0 || digitalRead(next)==0 || digitalRead(prev)==0)

  {

    if(digitalRead(pp)==0)

    {

      tmrpcm.pause();

      while(digitalRead(pp)==0);

      delay(200);

    }

    else if(digitalRead(next)==0)

    {

      if(temp<4)//temp should be lesser than no. of songs

      temp=temp+1;

      while(digitalRead(next)==0);

      delay(200);

      song();

    }

    else if(digitalRead(prev)==0)

    {

      if(temp>1)

      temp=temp-1;

      while(digitalRead(prev)==0);

      delay(200);

      song();

    }

  }

}

void song (void) {

  if(temp==1)

  {

    tmrpcm.play("Song1.wav"); 

  }

  else if(temp==2)

  {

    tmrpcm.play("Song2.wav"); 

  }

  else if(temp==3)

  {

    tmrpcm.play("Song3.wav"); 

  }

  else if(temp==4)

  {

    tmrpcm.play("Song4.wav"); 

  }

}

 

Code Explanation

  1. The code includes the necessary libraries for the project: the SD library, SPI library, and TMRpcm
  2. A TMRpcm object is created, which will be used to control audio playback. The object is named "tmrpcm".
  3. The pin modes for buttons pp, next, and prev are set to INPUT_PULLUP, which means that they will read HIGH when not pressed and LOW when pressed.
  4. The speaker pin for the TMRpcm object is set to pin 9.
  5. The SD card is initialized using the SD library. If the initialization fails, an error message is printed and the setup function returns.
  6. The volume for audio playback is set to 5.
  7. The first song ("wav") is played using the TMRpcm object.
  8. In the loop function, a while loop is used to wait for any of the three buttons to be pressed (pp, next, or prev).
  9. If the pp button is pressed, audio playback is paused until the pp button is released.
  10. If the next button is pressed and the current song is not the last song, the "temp" variable is incremented and the "song" function is called to play the next song.
  11. If the prev button is pressed and the current song is not the first song, the "temp" variable is decremented and the "song" function is called to play the previous song.
  12. The "song" function is defined to play the corresponding song based on the value of the "temp" variable. Each song is named "Song1.wav", "Song2.wav", etc.
  13. That's a basic explanation of what the code does. Let me know if you have any further questions!

Leave a comment

Please note, comments must be approved before they are published

Your cart

×