I’ve corrected various errors and merged both sketches into one. See my embedded comments.
/*
Fri 29 Aug 2025 1441; corrected errors & merged LED and PIR sketches
Read my comments in code to see changes, most are marked "my edit".
THIS version switches OFF the LED after motion detected
and stays on for the 2s that the PIR remains high. Amend this if you
want different logic, such as staying on until next trigger, etc.
When triggered by motion it plays each successive track, in full or until
next triggered.
This version uses my own preferred Uno/Nano pins. In particular
I don't use the pair 2/3 for Rx/Tx, which can cause interrupt conflicts
in less simple projects..
Each trigger plays successive tracks, repeating after track 5.
*/
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h> // download this library from Arduino IDE "Manage Libraries"
int pirPin = 3; // my edit
// int pirPin = 12; // Arduino pin the PIR sensor is connected to
int rxPin = 10; // my edit
// int rxPin = 3; // Arduino pin the TX pin of mp3 player is connected to
int txPin = 11; // my edit
// int txPin = 2; // Arduino pin the RX pin of mp3 player is connected to
int ledPin = 13; //Arduino built-in LED, and a more visible one
int motionStatus = 0; // variable to store the PIR's current reading (high or low)
int pirState = 0; // variable to store the PIR's state change
int track = 1; // variable to store current track number
SoftwareSerial fxSerial(rxPin, txPin); // Software Serial object
DFRobotDFPlayerMini fxPlayer;
void setup()
{
pinMode(pirPin, INPUT); // set Arduino pin that PIR is connected to as an INPUT
pinMode(rxPin, INPUT); // set Arduino pin that mp3 player TX is connected to as an INPUT
pinMode(txPin, OUTPUT); // set Arduino pin that mp3 player RX is connected to as an OUTPUT
pinMode(ledPin, OUTPUT); // my edit
Serial.begin(115200); // my preference
// Serial.begin(9600); // initialize Serial Monitor (for looking at PIR readings)
fxSerial.begin(9600); // initialize Serial communication for mp3 player
fxPlayer.begin(fxSerial); // tells mp3 player to use fxSerial for communication
delay(1000); // my edit
fxPlayer.volume(15); // my edit // mp3 player volume (pick a value 10-30)
delay(8000); // my edit, allow say 8 secs for PIR sensor to settle
}
void loop()
{
motionStatus = digitalRead(pirPin); // read the PIR pin's current output (is it HIGH or LOW?)
// if PIR pin output is HIGH:
if (motionStatus == HIGH)
{
if (pirState == LOW)
{
Serial.println("Motion Detected"); // print result to the serial monitor
digitalWrite(ledPin, LOW); // turn OFF built-in LED
pirState = HIGH; // update the previous PIR state to HIGH
fxPlayer.play(track); // play up to 3 s of current track
// delay(1000); // // my edit length in MILLIseconds of the current track
// delay(5000); // length in microseconds of the longest track
track++; // add 1 to the current track for the next loop
// if track number tries to exceed 5 (I have 5 total in my case)
if (track > 5)
{ // change this value to match your total # of tracks
track = 1; // reset track number to 1
}
}
}
// or else if PIR pin output is LOW:
else
{
// if (pirState == LOW)
if (pirState == HIGH)
{
Serial.println("Motion Ended"); //print result to the serial monitor
digitalWrite(ledPin, HIGH); // turn ON built-in LED
pirState = LOW; // update the previous PIR state to LOW
}
}
}