Skip to main content

MP3 sound control

This simple project will use a mp3 player, a simple, recycled speaker, and arduino and a HLK-LD2420 human motion sensor to play sounds when a human walks past the device. 

sketch code:

/*
 * Arduino Sketch for Motion-Activated MP3 Playback, Servo Control, and Display
 * 
 * This code interfaces with an LD2420 MMWave Sensor to detect motion. Upon
 * detecting motion, it commands a DF Player Mini to play an MP3 file, controls
 * an MG996R servo motor to rotate 45 degrees, and updates an SSD1306 I2C
 * display. The system uses an Arduino Nano as the main controller.
 */

#include <SoftwareSerial.h>
//#include define<DFRobotDFPlayerMini.h>
the#include digital<Servo.h>
pins#include to<Wire.h>
use#include as RX and TX for two<Adafruit_SSD1306.h>

// softwarePin serialdefinitions
connections#define constSERVO_PIN intA7
RX1#define =DFPLAYER_RX_PIN 8;A2
const#define intDFPLAYER_TX_PIN TX1A3
=#define 9;SENSOR_PIN constA6
int#define RX2OLED_RESET =-1
10;#define constSCREEN_WIDTH int128
TX2#define =SCREEN_HEIGHT 11;64

// create SoftwareSerialCreate objects
SoftwareSerial SoftSerialOne(RX1,TX1)mySoftwareSerial(DFPLAYER_RX_PIN, DFPLAYER_TX_PIN);
SoftwareSerialDFRobotDFPlayerMini SoftSerialTwo(RX2,TX2)myDFPlayer;
Servo myServo;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup(void)) {
  // setup the softwareInitialize serial pinscommunication
  mySoftwareSerial.begin(9600);
  Serial.begin(9600);

  // Initialize DFPlayer
  if (!myDFPlayer.begin(mySoftwareSerial)) {
    Serial.println("Unable to begin DFPlayer Mini");
    while (true);
  }
  myDFPlayer.volume(20);  // Set volume level (0-30)

  // Initialize Servo
  myServo.attach(SERVO_PIN);
  myServo.write(0);  // Set initial position

  // Initialize sensor pin
  pinMode(RX1,SENSOR_PIN, INPUT);

  pinMode(RX2,// INPUT)Initialize display
  if (!display.begin(SSD1306_I2C_ADDRESS, 0x3C)) {
    Serial.println("SSD1306 allocation failed");
    pinMode(TX1,for OUTPUT)(;;);
  pinMode(TX2,}
  OUTPUT)display.display();
  delay(2000);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.println("System Ready");
  display.display();
}

void loop(void)) {
  SoftSerialOne.begin(9600)// Check for motion detection
  if (digitalRead(SENSOR_PIN) == HIGH) {
    // Play MP3 file
    myDFPlayer.play(1);  // begin communication onPlay the first track

    // softwareRotate serialservo channel45 SoftSerialOne.print("Hellodegrees
    World")myServo.write(45);
    delay(1000);  // sendWait somethingfor SoftSerialOne.end(); // end communication on the first software
// serial channel
SoftSerialTwo.begin(9600); // begin communication on the1 second

    // softwareReturn serialservo channelto SoftSerialTwo.print("Helloinitial World")position
    myServo.write(0);
    delay(1000);  // sendWait somethingfor SoftSerialTwo.end();1 second

    // endUpdate communicationdisplay
    ondisplay.clearDisplay();
    thedisplay.setCursor(0, second0);
    softwaredisplay.println("Motion //Detected!");
    serialdisplay.display();
    channeldelay(2000);
    display.clearDisplay();
    display.setCursor(0, 0);
    display.println("System Ready");
    display.display();
  }
}

Basic Wiring

motion_motor_sound.png