I want to play mp3 when PIR is activated

I am using the following code. But mp3 doesn’t play
Who can help me?

#include "Arduino.h"
#include "Audio.h"
#include "SD.h"
#include "FS.h"
 
// Digital I/O used
#define SD_CS          5
#define SPI_MOSI      23    // SD Card
#define SPI_MISO      18
#define SPI_SCK       19
 
#define I2S_DOUT      25
#define I2S_BCLK      27    // I2S
#define I2S_LRC       26


const int PIN_TO_SENSOR = 2; //  pin connected to OUTPUT pin of sensor
int pinStateCurrent   = LOW;  // current state of pin
int pinStatePrevious  = LOW;  // previous state of pin

Audio audio;
 
void setup() {
    
    
   pinMode(PIN_TO_SENSOR, INPUT); // set ESP32 pin to input mode to read value from OUTPUT pin of sensor

    pinMode(5, OUTPUT);   
    digitalWrite(5, LOW);
    Serial.begin(115200);
   
     SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI);
    Serial.begin(115200);
    if(!SD.begin(SD_CS))
    {
      Serial.println("Error talking to SD card!");
      while(true);  // end program
    }  
    audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
    audio.setVolume(15); // 0...21
    audio.connecttoFS(SD,"/mysilverl.mp3"); 
    
}
 
void loop(){
  
  pinStatePrevious = pinStateCurrent; // store old state
  pinStateCurrent = digitalRead(PIN_TO_SENSOR);   // read new state

  if (pinStatePrevious == LOW && pinStateCurrent == HIGH) {   // pin state change: LOW -> HIGH
    Serial.println("Motion detected!");
    audio.loop();
    
  }
  else
  if (pinStatePrevious == HIGH && pinStateCurrent == LOW) {   // pin state change: HIGH -> LOW
    Serial.println("Motion stopped!");

    // TODO: turn off alarm, light or deactivate a device ... here
  }

   }

Tell us more

as you experienced from the other answer

the short answer just arises new questions:

  • what shall I tell more?

It is the same situation for your potential helpers
Your potential helpers need more information:

Where did you donwload the libraries
Audio.h ?
sd.h?
fs.h?

What exact type of microcontroller-board are you using?
What I2S-device are you using? please post a link to the data-sheet of this I2S-device
post a hand-drawn schematic of how

  • you have wired the I2S-device to the microcontroller
  • you have wired the SD-card-socket to the microcontroller

post a link to the datasheet of your I2S-device
post a link to the datasheet of your SD-card-socket

What file-format does the audio-library expect to read?

The microcontroller-world is not super-standardised like USB-devices.
You have to take care about more than just "Does the plug fit into the socket?"

best regards Stefan

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