Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino-Controlled PIR Motion Sensor with MP3 Playback and LED Lightning Effect

View and Edit Project In Cirkit DesignerImage of Arduino-Controlled PIR Motion Sensor with MP3 Playback and LED Lightning Effect

Circuit Documentation

Summary

The circuit is designed to detect motion using a PIR sensor and, upon detection, trigger a sequence that simulates lightning using an LED strip while playing a sound through an MP3 player module. The Arduino UNO serves as the central controller, interfacing with the PIR sensor, MP3 player, and relay module that controls the LED strip. The relay module is used to switch the high-power LED strip, and the MP3 player communicates with the Arduino via serial communication. A pushbutton is included to manually trigger the relay.

Component List

PIR Sensor SB312A

Arduino UNO

5V 8-Channel Relay

Serial MP3 Player

12V White LED Strip

Pushbutton

240v Power Source

Wiring Details

PIR Sensor SB312A

Arduino UNO

5V 8-Channel Relay

Serial MP3 Player

12V White LED Strip

Pushbutton

240v Power Source

Documented Code

#include <SoftwareSerial.h>
#include <DFPlayerMini_Fast.h>

// Pin definitions
#define PIR_PIN 2          // PIR sensor connected to pin D2
#define LED_STRIP_PIN 9     // LED strip pin (used to simulate lightning effect)
#define MP3_RX_PIN 10       // RX pin for DFPlayer Mini
#define MP3_TX_PIN 11       // TX pin for DFPlayer Mini

// MP3 player setup
SoftwareSerial mySerial(MP3_RX_PIN, MP3_TX_PIN); // RX (Pin 10), TX (Pin 11)
DFPlayerMini_Fast myMP3;

// Variable to store PIR sensor state
bool motionDetected = false;

// Flag to prevent retriggering during sequence
bool sequenceInProgress = false;

// Set a delay to avoid retriggering the PIR sensor immediately after motion is detected
unsigned long pirCooldown = 77000;  // 1 minute 17 seconds (77 seconds) delay after PIR is triggered
unsigned long lastMotionTime = 0;   // Timestamp of when the PIR was last triggered

void setup() {
  // Initialize serial communication for MP3 and debugging
  mySerial.begin(9600);
  Serial.begin(9600);

  // Initialize PIR sensor pin as input
  pinMode(PIR_PIN, INPUT);

  // Set up the LED strip pin as output and ensure it's off initially
  pinMode(LED_STRIP_PIN, OUTPUT);
  analogWrite(LED_STRIP_PIN, 0);    // Ensure the LED strip is off initially

  // Initialize DFPlayer Mini
  if (!myMP3.begin(mySerial)) {
    Serial.println("DFPlayer Mini not detected.");
    while (true); // Halt if MP3 player isn't detected
  } else {
    Serial.println("DFPlayer Mini detected and initialized.");
  }
  
  myMP3.volume(25);  // Set volume level (0 to 30)
  Serial.println("System ready. Waiting for PIR motion detection...");
}

void loop() {
  unsigned long currentTime = millis(); // Get the current time

  // If a sequence is in progress, check if cooldown time has passed
  if (sequenceInProgress) {
    // If 77 seconds have passed since the PIR was triggered, reset for next sequence
    if (currentTime - lastMotionTime >= pirCooldown) {
      sequenceInProgress = false;  // Reset sequence flag, allowing the PIR to trigger again
      Serial.println("Cooldown complete. System ready for next trigger.");
    }
    return;  // Do nothing else while the sequence is in progress
  }

  // Read PIR sensor input
  motionDetected = digitalRead(PIR_PIN);

  // Check if PIR detected motion and cooldown has passed
  if (motionDetected && (currentTime - lastMotionTime >= pirCooldown)) {
    lastMotionTime = currentTime;  // Record the time when the PIR was triggered
    sequenceInProgress = true;     // Set the sequence in progress flag
    Serial.println("Motion detected! Starting lightning and MP3 sequence...");

    // Play the MP3 file at the same time as the lightning
    playMP3WithLightning();

    Serial.println("Sequence complete. Waiting for cooldown.");
  }

  // Small delay to avoid excessive polling
  delay(100);
}

// Function to simulate a lightning effect using the LED strip while playing MP3
void playMP3WithLightning() {
  // Start playing the MP3 file (0001.mp3)
  myMP3.play(1);
  Serial.println("Playing MP3 track 0001.mp3");

  int numFlashes = random(3, 8); // Random number of flashes (3 to 7 flashes)
  
  for (int i = 0; i < numFlashes; i++) {
    // Bright flash with random intensity
    int flashBrightness = random(180, 255); // Random brightness between 180 and 255
    analogWrite(LED_STRIP_PIN, flashBrightness); // Set brightness
    int flashDuration = random(50, 150); // Random short flash duration (50ms to 150ms)
    delay(flashDuration); // Keep the brightness for the duration

    // Dim to simulate fading flash
    analogWrite(LED_STRIP_PIN, random(50, 120)); // Dim flash to random brightness (50 to 120)
    delay(random(100, 300)); // Dim flash duration (100ms to 300ms)

    // Small pause between flashes to add randomness
    delay(random(100, 500)); // Random delay between flashes (100ms to 500ms)

    // Check if MP3 is still playing, if not, break out of loop
    if (!myMP3.isPlaying()) {
      break;
    }
  }

  // Gradually fade out after the lightning effect
  for (int