Light Activated MP3 player (Part 2/2)
This is the continuation of the Light Activated Mp3 player Part 1.
Once we had tested that our MP3 is fully functional, it is now the time to add the Light sensor. The Light sensor LDR (Light Dependend Resistor) is a passive component that is reactive to light, when the light shine through the LDR, their resistance value decreases. So in order to use this we had to connect the LDR with another Resistor to build a voltage divider. This will provide the feed into the Arduino input.
The updated circuit can be found below:
As shown in the circuit above the connection from the voltage divider of LDR and resistor 10K is connected to Arduino input A0. Assuming that you had a working code from Part 1. The following snippet of code allows you to detect the light.
const int LDRpin = A0; // LDR connected to pin A0 ldrStatus = analogRead(LDRpin); //Serial.println(ldrStatus); if (ldrStatus >=300){ digitalWrite(LED_BUILTIN, LOW); Serial.print("LED Light"); mp3.start(); onStatus = true; }
The above code will play the mp3 player when the light is shining on the LDR. The threshold of the brightness in the code is 300, this can be adjusted if you have less light. If you have less light, that means you will need to lower the threshold, a bit of trial and error will give the best result.
And finally instead of using the internal Arduino light, I had added another LED connected to PIN 13 or Arduino. This is shown in the following diagram
The full source code can be found below:
// this example will play a Track 001 in folder 006 // when light is on the LDR // it expects the sd card to contain some mp3 files // LDR is connected with Resistor 10K in series // Free LDR leg is connected to 5V // other end of R10K is connected to GND // joined LDR leg and R10k is connected to pin A0 // LED is connected to pin D13 #include #include #define DEBUG 0 // 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); const int LDRpin = A0; // LDR connected to pin A0 const int LEDpin = 13; // LED pin D13 int Song=1; //sd:/06/001.mp3 int ldrStatus; boolean onStatus = true; boolean takeLowTime; //the timer to reset song long unsigned int timeoutTimer = 0, resetCount = 0; //the amount of milliseconds the sensor has to be low //before we assume all motion has stopped long unsigned int pause = 2000; void setup() { Serial.begin(115200); if (DEBUG) Serial.println("initializing..."); pinMode(LDRpin, INPUT); // setup LDR pinMode(LEDpin, OUTPUT); 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); if (DEBUG) Serial.println("starting..."); mp3.playFolderTrack(6, Song); } void loop() { ldrStatus = analogRead(LDRpin); if (ldrStatus >=300){ digitalWrite(LEDpin, HIGH); mp3.start(); onStatus = true; } } else { digitalWrite(LEDpin, LOW); mp3.pause(); } // calling mp3.loop() periodically allows for notifications // to be handled without interrupts mp3.loop(); }
I hope you had enjoyed the process of building the Light Activated Mp3 player, and hopefully you are well rewarded by giving this gift. Enjoy the final video of the gift that I had made. If you like this please leave me a comment.