Arduino Mega 2560, MP3 player, and PIR Sensor

Hello! May someone, please, help with my project? My project consist of these objects:

  1. An Arduino Mega 2560

  2. DFPlayer Mini (MP3 player)

  3. PIR Sensor

I would like for the sensor to produce signals for when a person is in front of the PIR sensor or not. Basically, HIGH or LOW signals should be produced by the PIR sensor based upon movement. Sometimes, they do, but the PIR sensor almost always seems to be HIGH. I also would like the LED to turn off and on based on the PIR signals. In addition to that, I would like for the DFPlayer Mini (MP3 player) to play a sound based on the PIR signals. Someone has made a schematic for the DFPlayer and Arduino Mega 2560 that I am using. Also, everytime the power is unplugged or turned off, the DFPlayer produces a terrible sound. May someone explain what that is, please? Anyway, so, in the video I am going to post, you’ll be able to see two LEDs on, one for PWR and one for the PIR sensor’s signals. You can see that the LED on the right is the PIR sensor’s LED and PWR is on the left. For some reason, also, the LED on the 5V board on my breadboard, that is powering the DFPlayer Mini, is also power that LED. Very strange. Why is it doing that? So, also, the speaker is occasionally making noise (the sound I picked), which will be in the video as well. The rest will be schematic and code.

Schematic/Wiring Diagram:

Arduino Mega 2560 DFPlayer Wiring Diagram

Arduino Mega 2560 My Schematic

PIR information (lesson 17)

Basic starting out code:

//www.elegoo.com
//2016.12.9

int ledPin = 13;  // LED on Pin 13 of Arduino
int pirPin = 7; // Input for HC-S501

int pirValue; // Place to store read PIR Value


void setup() {
  
  pinMode(ledPin, OUTPUT);
  pinMode(pirPin, INPUT);
 
  digitalWrite(ledPin, LOW);
  
  
  
}

void loop() {
  pirValue = digitalRead(pirPin);
  digitalWrite(ledPin, pirValue);

}
//
// sd:/mp3/0001.mp3
// sd:/mp3/0002.mp3
// sd:/mp3/0003.mp3

#include <SoftwareSerial.h>
#include <DFMiniMp3.h>

// 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 globalTrack)
  {
    Serial.println();
    Serial.print("Play finished for #");
    Serial.println(globalTrack);   
  }

  static void OnCardOnline(uint16_t code)
  {
    Serial.println();
    Serial.print("Card online ");
    Serial.println(code);     
  }

  static void OnUsbOnline(uint16_t code)
  {
    Serial.println();
    Serial.print("USB Disk online ");
    Serial.println(code);     
  }

  static void OnCardInserted(uint16_t code)
  {
    Serial.println();
    Serial.print("Card inserted ");
    Serial.println(code); 
  }

  static void OnUsbInserted(uint16_t code)
  {
    Serial.println();
    Serial.print("USB Disk inserted ");
    Serial.println(code); 
  }

  static void OnCardRemoved(uint16_t code)
  {
    Serial.println();
    Serial.print("Card removed ");
    Serial.println(code);  
  }

  static void OnUsbRemoved(uint16_t code)
  {
    Serial.println();
    Serial.print("USB Disk removed ");
    Serial.println(code);  
  }
};

// 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);

void setup() 
{
  Serial.begin(115200);

  Serial.println("initializing...");
  
  mp3.begin();

  uint16_t volume = mp3.getVolume();
  Serial.print("volume ");
  Serial.println(volume);
  mp3.setVolume(24);
  
  uint16_t count = mp3.getTotalTrackCount();
  Serial.print("files ");
  Serial.println(count);
  
  Serial.println("starting...");
}

void waitMilliseconds(uint16_t msWait)
{
  uint32_t start = millis();
  
  while ((millis() - start) < msWait)
  {
    // calling mp3.loop() periodically allows for notifications 
    // to be handled without interrupts
    mp3.loop(); 
    delay(1);
  }
}

void loop() 
{
  Serial.println("track 1"); 
  mp3.playMp3FolderTrack(1);  // sd:/mp3/0001.mp3
  
  waitMilliseconds(5000);
  
  Serial.println("track 2"); 
  mp3.playMp3FolderTrack(2); // sd:/mp3/0002.mp3
  
  waitMilliseconds(5000);
  
  Serial.println("track 3");
  mp3.playMp3FolderTrack(3); // sd:/mp3/0002.mp3
  
  waitMilliseconds(5000); 
}

Here is my code, as well:

//www.elegoo.com
//2016.12.9

#include <DFMiniMp3.h>

// 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 globalTrack)
    {
      Serial.println();
      Serial.print("Play finished for #");
      Serial.println(globalTrack);
    }

    static void OnCardOnline(uint16_t code)
    {
      Serial.println();
      Serial.print("Card online ");
      Serial.println(code);
    }

    static void OnUsbOnline(uint16_t code)
    {
      Serial.println();
      Serial.print("USB Disk online ");
      Serial.println(code);
    }

    static void OnCardInserted(uint16_t code)
    {
      Serial.println();
      Serial.print("Card inserted ");
      Serial.println(code);
    }

    static void OnUsbInserted(uint16_t code)
    {
      Serial.println();
      Serial.print("USB Disk inserted ");
      Serial.println(code);
    }

    static void OnCardRemoved(uint16_t code)
    {
      Serial.println();
      Serial.print("Card removed ");
      Serial.println(code);
    }

    static void OnUsbRemoved(uint16_t code)
    {
      Serial.println();
      Serial.print("USB Disk removed ");
      Serial.println(code);
    }
};

// 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);


int pirPin = 7; // Input for HC-S501

int pirValue; // Place to store read PIR Value

int ledPin = 13;  // LED on Pin 13 of Arduino

void setup() {

  pinMode(ledPin, OUTPUT);

  mp3.begin();

  mp3.setVolume(24);

  pinMode(pirPin, INPUT);

  digitalWrite(ledPin, LOW);

}

void loop() {

  pirValue = digitalRead(pirPin);

  digitalWrite(ledPin, pirValue);

  while (pirValue == HIGH) {
    mp3.playMp3FolderTrack(1);  // sd:/mp3/0001.mp3
  }

}

My Video

You first have to post a complete schematic. The "my schematic" link is incomplete. No power supply to the Arduino, and that is what I was looking for in relation to your "lots of noise" comment.

Here is the schematic (Arduino is connected to a USB type B; the link is also updated):

I miss the required GND connection from the Arduino's GND pins to the sensor and DF player GND.

Well, I have the Arduino and DFPlayer to GND (their grounds are connected).

Please, somebody help!

Well, usual suspects include 1k resistors in Tx & Rx; make sure all grounds are connected (including all the ground pins of the DF Player board), and an extra power supply decoupling cap at the DF player.

After that... well the best you can do is start digging around blog posts related to the DF Player, maybe some other suggestions are there.

Thanks for all the help!

Here is the complete working code WITH a IR sensor:

/*Software from Mert Arudino:
https://www.hackster.io/mertarduino/how-to-make-a-talking-pir-motion-security-system-d74e0a
Special thanks to all who helped as well:
https://forum.arduino.cc/index.php?topic=657163.msg4431743#msg4431743
*/


#include <DFPlayerMini_Fast.h>

DFPlayerMini_Fast myMP3;
void printDetail(uint8_t type, int value);

#define SENSORPIN 7
#define SENSOR2PIN 6
#define PAUSETIME 2000

void setup() {
  Serial1.begin(9600);
  Serial.begin(9600);
  pinMode(SENSORPIN, INPUT);
  pinMode(SENSOR2PIN, INPUT);

  Serial.println();
  Serial.println(F("Initializing DFPlayer..."));

  //Use softwareSerial to communicate with MP3
  if (!myMP3.begin(Serial1)) {
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while(true);
  }
  Serial.println(F("DFPlayer Mini online."));

  //Set volume value (From 0 to 30)
  myMP3.volume(30);
}

void sensor2Activated() {
  int IRSensor = digitalRead(SENSOR2PIN);
  if(IRSensor == LOW)
  {
    Serial.println("Sensor Activated");
    Serial.println("DFPlayer Working...");
    myMP3.play(9);
  }
  return;
}

void sensorActivated() {
  int pirSensor = digitalRead(SENSORPIN);
  if(pirSensor == HIGH)
  {
    Serial.println("Sensor Activated");
    Serial.println("DFPlayer Working...");
    myMP3.play(10);
  }
  return;
}

void loop() {
  sensorActivated();
  sensor2Activated();
  delay(PAUSETIME);
}

Even better code (the sound plays only once):

/*Software from Mert Arudino:
https://www.hackster.io/mertarduino/how-to-make-a-talking-pir-motion-security-system-d74e0a
Special thanks to all who helped as well:
https://forum.arduino.cc/index.php?topic=657163.msg4431743#msg4431743
*/

#include <DFPlayerMini_Fast.h>

DFPlayerMini_Fast myMP3;
void printDetail(uint8_t type, int value);

#define SENSORPIN 7
#define SENSOR2PIN 6
#define PAUSETIME 2000

byte SENSORCOUNT = 1;
byte SENSOR2COUNT = 0;

void setup() {
  Serial1.begin(9600);
  Serial.begin(9600);
  pinMode(SENSORPIN, INPUT);
  pinMode(SENSOR2PIN, INPUT);

  Serial.println();
  Serial.println(F("Initializing DFPlayer..."));

  //Use softwareSerial to communicate with MP3
  if (!myMP3.begin(Serial1)) {
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while(true);
  }
  Serial.println(F("DFPlayer Mini online."));

  //Set volume value (From 0 to 30)
  myMP3.volume(30);
}

void sensor2Activated() {
  
  int IRSensor = digitalRead(SENSOR2PIN);
  
  if(IRSensor == LOW && SENSOR2COUNT == 0)
  {
    SENSOR2COUNT++;
    Serial.println("Sensor Activated");
    Serial.println("DFPlayer Working...");
    myMP3.play(9);
  }
  else{
    myMP3.sleep();
  }
  return;
}

void sensorActivated() {
  
  int pirSensor = digitalRead(SENSORPIN);
  
  if(pirSensor == HIGH && SENSORCOUNT == 1)
  {
    SENSORCOUNT++;
    Serial.println("Sensor Activated");
    Serial.println("DFPlayer Working...");
    myMP3.play(10);
    delay(1500);
  }
  else{
    myMP3.sleep();
  }
  return;
}

void loop() {
  sensorActivated();
  sensor2Activated();
  delay(PAUSETIME);
}