Hello! May someone, please, help with my project? My project consist of these objects:
-
An Arduino Mega 2560
-
DFPlayer Mini (MP3 player)
-
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
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
}
}