Arduino music player with LEDs tutorial


Arduino music player project can be easily implemented in 10 minutes!
The popular mp3 music file is converted to wav file, stored in SD card module, then it is read by arduino and output to speaker

Hardware need
1. Arduino Pro Mini (link to purchase)
2. SD card SPI module (link to purchase)
3. SD card 8GB (link to purchase)
3. Speaker 3W 8Ohm (link to purchase)
4. Some LEDs, resistor, transistor

Step 1. Make a circuit

Output from Arduino to speaker need through some transistor to magnify current through speaker, make it louder. Or this output can connect to commercial amplifier for better sound
Four LEDs is used to display music wave form. Quantity of displayed LEDs are detected by amplitude of sound, which is represented via TIMER1

Step 2. Code works
Before make the code, an library called TMRpcm for music need to be installed

Arduino code
#include <SD.h>               // need to include the SD library
#define SD_ChipSelectPin 10   //pin10 for Arduino Pro Mini
#include <TMRpcm.h>           //  Lib to play wav file
#include <SPI.h>

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

unsigned long time = 0;
int waveform = 0;
int qtyLED = 0;
int timeLED = 25;

void setup(){
  tmrpcm.speakerPin = 9; //pin 9 for Aduino Pro Mini
  Serial.begin(9600);
  pinMode(17,OUTPUT);
  pinMode(16,OUTPUT);
  pinMode(15,OUTPUT);
  pinMode(14,OUTPUT);
  if (!SD.begin(SD_ChipSelectPin)) {  // see if the card is present and can be initialized:
    Serial.println("SD fail");  
    return;   // don't do anything more if not
  }
  else{   
    Serial.println("SD ok");   
  }
  tmrpcm.play("music.wav"); //the sound file "music" will play each time the arduino powers up, or is reset
  tmrpcm.volume(2);
}

void loop(){
  waveform = OCR1A; //read TIMER1 value, represent for wave form music
  waveform = waveform - 256;  // middle point of volume(2) is 256
  qtyLED = waveform/16;  //q'ty LED need to be ON
  if (qtyLED <= 0){
    qtyLED0();
    delay(timeLED);
  }
  if (qtyLED == 1){
    qtyLED0();
    delay(timeLED);
    qtyLED1();
    delay(timeLED);
  }
  if (qtyLED == 2){
    qtyLED0();
    delay(timeLED);
    qtyLED1();
    delay(timeLED);
    qtyLED2();
    delay(timeLED);
  }
  if (qtyLED == 3){
    qtyLED0();
    delay(timeLED);
    qtyLED1();
    delay(timeLED);
    qtyLED2();
    delay(timeLED);
    qtyLED3();
    delay(timeLED);
  }
  if (qtyLED == 4){
    qtyLED0();
    delay(timeLED);
    qtyLED1();
    delay(timeLED);
    qtyLED2();
    delay(timeLED);
    qtyLED3();
    delay(timeLED);
    qtyLED4();
    delay(timeLED);
  }

  if(Serial.available()){    
    switch(Serial.read()){
    case 'd': tmrpcm.play("music.wav"); break;
    case 'b': tmrpcm.play("BeatIt.wav"); break;
    case 'r': tmrpcm.play("Race_Car.wav"); break;
    case 'p': tmrpcm.pause(); break;
    case '?': if(tmrpcm.isPlaying()){ Serial.println("A wav file is being played");} break;
    case 'S': tmrpcm.stopPlayback(); break;
    case '=': tmrpcm.volume(1); break;
    case '-': tmrpcm.volume(0); break;
    case '0': tmrpcm.quality(0); break;
    case '1': tmrpcm.quality(1); break;
    default: break;
    }
  }
}

void qtyLED1(){
  digitalWrite(17,0);
  digitalWrite(16,0);
  digitalWrite(15,0);
  digitalWrite(14,1);
}
void qtyLED2(){
  digitalWrite(17,0);
  digitalWrite(16,0);
  digitalWrite(15,1);
  digitalWrite(14,1);
}
void qtyLED3(){
  digitalWrite(17,0);
  digitalWrite(16,1);
  digitalWrite(15,1);
  digitalWrite(14,1);
}
void qtyLED4(){
  digitalWrite(17,1);
  digitalWrite(16,1);
  digitalWrite(15,1);
  digitalWrite(14,1);
}
void qtyLED0(){
  digitalWrite(17,0);
  digitalWrite(16,0);
  digitalWrite(15,0);
  digitalWrite(14,0);
}
The main code is taken from example of library TMRpcm, then we add a section to display LEDs depend on amplitude of sound, called music wave form.
The variable name "waveform" in the code, if it is printed out, it will looks like this:
This waveform is used to detect quantity of LEDs should be displayed.

One more thing is the music file mp3 need to change to wav format. There are many web base to convert music online. In this project, we use following page to convert music (link here)
The converted music should be in 8bit, 16000Hz, mono
Then, the music file is save to SD card with same name in Arduino code

Hola, music can be run now! In order to change music, do it by monitor screen.



20 comments:

  1. Hello. Very cool project and a nice tutorial. I plan to implement this for a project but instead I want to use recorded speech. Would this still work??

    ReplyDelete
    Replies
    1. sure, record your voice by phone, then convert it into music file as in this article. I'm sure it can work normally.

      Delete
    2. thanks for your response. il be sure to post the finished project soon

      Delete
  2. Cool project! one question, do you need to program the Arduino Pro Mini every time when you recycle the power?

    ReplyDelete
    Replies
    1. Hi, we don't need to program Arduino Pro Mini when recycle power. It will remember the program.

      Delete
    2. Sounds great. is that because there's some eeprom or flash memeory in the mcu?

      Delete
    3. yes, EEPROM or flash memory is in MCU. This memory is non-volatile type, so it is still there when no power

      Delete
  3. another question, i notice that you got a single-ended analog output from the Arduino pro mini, can the Arduino output differential analog signals?

    ReplyDelete
    Replies
    1. Yes, Arduino Pro Mini has another analog output pin. You can search another pin on google.

      Delete
    2. is this so-called "analog pin" as you have mentioned acturally PWN pin?

      Delete
  4. How do you program the arduino? is there any other software and hardware needed for that?

    ReplyDelete
  5. Could you also please add links for purchasing and the program tool for this project?

    ReplyDelete
    Replies
    1. Hi, here is how to pogram Arduino Pro Mini, please visit https://www.engineer2you.com/2016/12/arduino-program-arduino-pro-mini-with.html

      Delete
    2. By the way, where Can i find the breadboard and wires needed for this project? could you also please add thhose links?

      Delete
    3. The best one will be having a kit which includes every component in it

      Delete
    4. I know what your needs, but you can search it on Ebay or Amazon. There are many KITs for Arduino for this project

      Delete
  6. Thanks for taking the time to discuss this, I feel strongly that love and read more on this topic. If possible, such as gain knowledge, would you mind updating your blog with additional information? It is very useful for me. Succulent Alley

    ReplyDelete