Posted on

Light Activated MP3 player (Part 1/2)

Giftbox
Light Activated MP3 Player

Light activated MP3 player was incepted when I wanted to add something special to the present that I gave to my Mrs. So while I was learning about Arduino and have a play with the DFPlayer Mini, which is essentially an MP3 player.

Step 1. Prepare the MP3 Player

First thing to do is to connect the DF Player mini to Arduino using the following circuit.

DFplayerTest

The SD card will need to contain an MP3 file, let’s say the file is 001.mp3, this is located in the 006 folder of the SD card, i.e.: sd:\\006\001.mp3.

Once you connect the diagram above you can use the following code to test to ensure that the DF Player is playing the song you had saved. If you came from Instructables, step 4. you would want the following code to test your DF player.

// this example will play a random track from all on the sd card
// it expects the sd card to contain some mp3 files
#include <SoftwareSerial.h>
#include <DFMiniMp3.h>
// implement a notification class,
// its member methods will get called 
//
class Mp3Notify
{
public:
  static void OnError(uint16_t errorCode)
  {
    // see DfMp3_Error for code meaning
    Serial.println();
    Serial.print("Com Error ");
    Serial.println(errorCode);
  }
  static void OnPlayFinished(uint16_t track)
  {
    Serial.print("Play finished for #");
    Serial.println(track);  
  }
  static void OnCardOnline(uint16_t code)
  {
    Serial.println("Card online ");
  }
  static void OnCardInserted(uint16_t code)
  {
    Serial.println("Card inserted ");
  }
  static void OnCardRemoved(uint16_t code)
  {
    Serial.println("Card removed ");
  }
};
// instance a DFMiniMp3 object, 
// defined with the above notification class and the hardware serial class
//
//DFMiniMp3<hardwareserial, mp3notify> mp3(Serial1);
// Some arduino boards only have one hardware serial port, so a software serial port is needed instead.
// comment out the above definition and uncomment these lines
SoftwareSerial secondarySerial(10, 11); // RX, TX
DFMiniMp3<SoftwareSerial, mp3notify> mp3(secondarySerial);
int Song=1;   // 001.mp3
void setup() {
  Serial.begin(115200);
  Serial.println("initializing...");
  mp3.begin();
  mp3.reset(); 
  
  // show some properties and set the volume
  uint16_t volume = mp3.getVolume();
  Serial.print("volume ");
  Serial.println(volume);
  mp3.setVolume(20);
  
  uint16_t count = mp3.getTotalTrackCount();
  Serial.print("files ");
  Serial.println(count);
  uint16_t mode = mp3.getPlaybackMode();
  Serial.print("playback mode ");
  Serial.println(mode);
  
  Serial.println("starting...");
  
  //mp3.playRandomTrackFromAll(); // random of all folders on sd
  mp3.playFolderTrack(6, Song); // play song on sd:\\006\001.mp3
  delay(5000);
}

void loop() 
{
  // calling mp3.loop() periodically allows for notifications 
  // to be handled without interrupts
  mp3.loop();
}

If you are having trouble loading the “DFMiniMp3” library you can download it from the Instructables step 6, the library called “DFPlayer_Mini_MP3”.

To take it to the next level I added the LDR, which is essentially a resistor which are photosensitive. The resistance changes when there is a light shine through it, combination of the 2 makes up the Light activated MP3 Player. We will go through this in the next part.

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.