How to connect PIR to Arduino Mega + Code?

Hi,
I am trying to make a touch activated mp3 player using Arduino Mega, PIR sensor and DFPlayer.
However, I am confused as to how to connect the PIR to Arduino Mega. Where is the sensor pin located on Arduino Mega?
With reference to the code below by @UptownKitten , why are there two sensor pins and how should i modify the code if I only need 1 sensor pin?

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

Well it looks like the code is expected it connected to these pins...

Um... remove the code related to the 2nd sensor.

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;
}
1 Like

Okay, thank you for your help :slight_smile:

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