Author Options:

Arduino DFPlayer MP3 player with Light sensor (LDR Module) as trigger Answered

Hi everyone :)
Yet another question...

So I got my self a DFPlayer mini (mp3 player module) for Arduino, and a nice tutorial on how make it work, but I want to use s Light sensor (LDR Module) instead of a button (I just need play and pause or stop for my gift box)

So at the moment if I connect the LDR instead of the button and give it some light, it turns the music on and off constantly/repeatedly because the LDR is constantly sending a HIGH signal, not just once like the button.

Here is the question part: How to modify the code, so it only turns on the music when it gets HIGH signal and off if it gets LOW ???

Here is the modified code (I took out the auto play and the next and previous commands:

///              MP3 PLAYER PROJECT
/// http://educ8s.tv/arduino-mp3-player/

#include "SoftwareSerial.h"
SoftwareSerial mySerial(10, 11);
# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
# define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]


int buttonPlay = 3;
boolean isPlaying = false;

void setup () {

  pinMode(buttonPlay, INPUT);
  digitalWrite(buttonPlay, HIGH);

  mySerial.begin (9600);
  isPlaying = true;


void loop () {

  if (digitalRead(buttonPlay) == ACTIVATED)
    if (isPlaying)
      isPlaying = false;
    } else
      isPlaying = true;

void playFirst()

void pause()
  execute_CMD(0x0E, 0, 0);

void play()
  execute_CMD(0x0D, 0, 1);

void setVolume(int volume)
  execute_CMD(0x06, 0, volume); // Set the volume (0x00~0x30)

void execute_CMD(byte CMD, byte Par1, byte Par2)
// Excecute the command and parameters
  // Calculate the checksum (2 bytes)
  word checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
  // Build the command line
  byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge,
                            Par1, Par2, highByte(checksum), lowByte(checksum), End_Byte
  //Send the command line to the module
  for (byte k = 0; k < 10; k++)
    mySerial.write( Command_line[k]);

Thank you ever so much!!!


Ok this is a quick little one really. The line:


It says that things are activated when the input goes low. Replace it by


and the program will instead start playing when the signal is High and shuts off when LOW. I would suggest reading more on coding really. this is a really easy modification.

Go to the Arduino playground and check the tutorials and example code for the use of sensors, switches and such.
For obvious reasons that would have been the place to start and would have provided the answers and code you need.