PIR sensor, dfplayer and neopixels

I am new to Arduino and mostly copy and past code with minor adjustments that I understand. I am working with a simple code using an PIR sensor and dfplayer to play sound with sensor is triggered. I'd like to add just two neopixels and have them turn on for the duration of the track being played from the dfplayer. Below is my current code, I am not sure how to add or modify it to achieve this. Thanks in advance for any assistance.

/*This Arduino sketch plays a random track from a DFRobot PlayerMini
mp3 player module when motion is detected by a PIR sensor.

For the wiring diagram, step-by-step video, code explanation and more
motion-activated sound effects projects with Arduino, visit:

*/

#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h> // download this library from Arduino IDE "Manage Libraries"

int pirPin = 7; // Arduino pin the PIR sensor is connected to
int rxPin = 3; // Arduino pin the TX pin of mp3 player is connected to
int txPin = 2; // Arduino pin the RX pin of mp3 player is connected to

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

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
  
  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
  fxPlayer.volume(30); // mp3 player volume (pick a value 10-30)
  
  delay(30000); // allow 30-60 secs for PIR sensor to calibrate before proceeding with sketch
}

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
      pirState = HIGH; // update the previous PIR state to HIGH
      
      fxPlayer.play(random(1,8)); // play a random track from 1 to 7 from micro SD card
      delay(10000); // length in microseconds of longest track
    }
  }
    
  // or else if PIR pin output is LOW:
  else {
      
    if (pirState == HIGH) {
      Serial.println ("Motion Ended"); //print result to the serial monitor
      pirState = LOW; // update the previous PIR state to LOW
    }
  }
}

Your sketch seems almost identical to that in 'PIR Sensor triggers DFPlayer - can't play mp3' posted by @emilydz. I assume you both just happen to be using the same sketch source?

In that other post I queried the use of the variable motionStatus, but had no reply.

Thanks for the response. I copied the code from the video:

I’ve read the dfplayer can power neopixels, so I am not sure if I need to connect them to the dfplayer or Arduino to have them light up during the duration of the track.

I tried adding the fast led sketch, but I know I was doing something wrong. I’m trying to finish this for an event this weekend and have to finish by Friday. I know it’s simple, I just don’t have the knowledge yet.

I changed nothing except pin numbers (to suit the breadboard at hand) but it ran here on a Uno. (Did you say what board you're using?)

I'll add my code anyway.
EDIT, 19 Sep. Meant to mention the other change I made, in the module’s initialisation, to fix the annoying noise on startup.

If that doesn't run as it stands on your Uno or Nano, then one other thing you could try is to change from version 1.0.6 of the DFR library, which is buggy, to 1.0.5, which I use.

/*This Arduino sketch plays a random track from a DFRobot PlayerMini
  mp3 player module when motion is detected by a PIR sensor.

  For the wiring diagram, step-by-step video, code explanation and more
  motion-activated sound effects projects with Arduino, visit:

*/

#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h> // download this library from Arduino IDE "Manage Libraries"

int pirPin = 12; // Arduino pin the PIR sensor is connected to
// int pirPin = 7; // Arduino pin the PIR sensor is connected to
int rxPin = 10; // Arduino pin the TX pin of mp3 player is connected to
// int rxPin = 3; // Arduino pin the TX pin of mp3 player is connected to
int txPin = 11; // Arduino pin the RX pin of mp3 player is connected to
// int txPin = 2; // Arduino pin the RX pin of mp3 player is connected to

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

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

  Serial.begin(9600); // initialize Serial Monitor (for looking at PIR readings)
  fxSerial.begin(9600); // initialize Serial communication for mp3 player
  fxPlayer.begin(fxSerial, true, false); // tells mp3 player to use fxSerial for communication
  fxPlayer.volume(20); // mp3 player volume (pick a value 10-30)
  delay(1000); // my edit
  //  delay(30000); // allow 30-60 secs for PIR sensor to calibrate before proceeding with sketch
}

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
      pirState = HIGH; // update the previous PIR state to HIGH

      fxPlayer.play(random(1, 8)); // play a random track from 1 to 7 from micro SD card
      delay(10000); // length in microseconds of longest track
    }
  }

  // or else if PIR pin output is LOW:
  else
  {

    if (pirState == HIGH)
    {
      Serial.println ("Motion Ended"); //print result to the serial monitor
      pirState = LOW; // update the previous PIR state to LOW
    }
  }
}

Sorry I forgot to mention the board. It’s a nano and I’ll be using just 2 WS2812B pixels. I am looking to have those 2 pixels light up only during the duration of the track being played on the dfplayer.

OK. Come back when you’ve tried my two suggestions .

Thank you for making that change for the start up, nice and quite now.

I might have not been clear in my original post, but the code I had posted is working just fine. I was look to adjust the code to include the function to add leds, and for them to light up once the PIR is triggered and stay lit up for roughly the length of the track (around 8-10 seconds) then turn off. Again, I apologize for my lack of knowledge on this and appreciate the help.

Since you and emily seem to be good with copy/paste, here is a pre-made sketch with your neopixel need...

How far have you got?

Did you study the many tutorials, such as this example;

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.