PIR Sensor triggers DFPlayer - can't play mp3

Hello,

I'm trying to create an installation that, when triggered by a PIR sensor, plays a specific track. When it's not triggered, it loops another track.

The problem I am running into is that it seemingly cannot play the second track. Any advice?

  • I am using an Arduino UNO and a DF Player
  • The mp3 files are formatted as 0001.mp3 and 0002.mp3 in a folder titled "mp3"
  • It currently successfully loops track 1 but will not play track 2 when triggered
  • I have confirmed that the PIR sensor is working
  • I have successfully gotten it to trigger audio, but not the track I want. If it triggers audio, it won't loop the audio when no motion is detected.

#include <DFRobotDFPlayerMini.h>
#include <SoftwareSerial.h>

int pirPin = 8;
int motionStatus = 0;
int pirState = 0;
int rxPin = 10;
int txPin = 11; 

SoftwareSerial fxSerial (rxPin, txPin);
DFRobotDFPlayerMini fxPlayer;

void setup() {
  pinMode(pirPin, INPUT);
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  Serial.begin(9600);
  fxSerial.begin(9600);
  fxPlayer.begin(fxSerial);
  fxPlayer.volume(30);
  delay(3000);
}

void loop () {
  motionStatus = digitalRead(pirPin);

  if(motionStatus == HIGH) {

    if(pirState == LOW) {
      Serial.println("Motion detected");
      pirState = HIGH;

      fxPlayer.play(2);
      delay(5000);
    }
  }

  else {
    if(pirState == HIGH) {
      Serial.println("Motion ended");
      pirState = LOW;

      fxPlayer.loop(1);
    }
  }
} 

Is it fxPlayer play or loop, you use one for the true side and the other for the false. Seems suspicious.

You only want to issue the play command in loop() after sufficient time for the track to complete, giving it time enough - otherwise you keep telling it to play it from the start, play it from the start, play it from the start,...

What do D.J.'s actually do... do... do... do...

1 Like

Probably not the objective here - but who knows?

Where are you changing motionStatus?
Seems like you’re using pirState and motionStatus is redundant?

See my reply today to another user apparently trying the same sketch.
From the project video 'Motion-activated Sound Effects with Arduino, PIR Sensor & MP3 Player' ?

https://forum.arduino.cc/t/pir-sensor-dfplayer-and-neopixels/1302674/4

Hey, thanks for the thoughts. I did get this problem resolved. I think it was a problem with the tracks themselves? It seems like the DF Robot Mp3 isn't reading them in a logical order.

Pleased you sorted it. But sounds like you didn’t read the documentation. :slightly_smiling_face:

I’d call it non-intuitive, or even just 'not like File Explorer', rather than illogical. It indexes the tracks in the order in which they were physically added to the mSD. One of the frequent causes of ‘sketch not working’ posts.