Making an Audio File Play With A Sensor

I have been working for weeks to solve this and I can't figure it out, though I am new to Arduino.

I have tried DFplayer and WTV020 and I can't figure out how to make them work with a temperature sensor. I have tried DHT11/22 and TMP36.

Using the example code for DFplayer is an absolute nightmare, I can't figure out how to simply play it through an if statement, I really need help before I tear my hair out. Thanks!

Well start off by saying what you want to do, the details you have given so far are vague to say the lease.

I can't figure out how to simply play it through an if statement,

Through?

Please read this:-
How to use this forum it will tell you how to ask a question and how to post stuff as attachments. Then you need to post the best attempt at the code you have made so far. Finally you need to post a schematic of what you have.

I must say I can't see much use for making a temperature measurement while playing a file so explaining your end goal is also useful.

I just did this yesterday, using the Catalex YX-5300 module. the DF player is apparently a YX-5200 module. the YX-5300 module is the same thing, with a Serial interface. I am assuming you have the chops to get Serial working. the whole sketch is 27K and growing; I am only including the code related to sound here. it isn’t for compiling, it’s for studying:

12 sensors, active low, 12 folders, containing sounds ranging from bird tweets to “lock and load, incoming!”. It works in the sense that pulling a pin low causes noise to come out, I don’t have it at right folders for individual sensors yet

no library required:

// YX5300 Sound Module
#define CMD_SEL_DEV 0X09
              #define DEV_TF 0X02
#define CMD_SET_VOLUME 0X06
//#define CMD_PLAY_W_VOL 0X22
//#define CMD_PLAY 0X0D
#define CMD_PLAY_W_INDEX 0X03 
#define CMD_PLAY_FOLDER_FILE 0X0F   // 0x7E 06 0F 00 01 02 EF;( play folder 01/002.wav )
//#define CMD_PAUSE 0X0E
//#define CMD_PREVIOUS 0X02
//#define CMD_NEXT 0X01

// YX-5300
static unsigned char Send_buf[8] = {0} ;

void setup()
{
delay(500);//Wait chip initialization is complete
      sendCommand(CMD_SEL_DEV, DEV_TF);                 //select the YX5300 TF card 
      delay(200);//wait for 200ms
      sendCommand(CMD_SET_VOLUME, 0x06001e);    // you have to set the sound volume first, to know if it's alive     
      //sendCommand(CMD_PLAY_W_INDEX, 0X030001);          // play the first sound file on the root directory to verify operation. plays on upload and on reset
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// YX5300 sound module functions //////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

void playWAV (int sw) // sw is the counter in a loop
{
 switch (sw)  // switch / case is a better choice than if / then for many purposes
   {
    case 0:  // Zone 0 Administrator Log In // press a concealed button to verify you are home, and to get past "Zone 1 is on pin 0, Zone two is on pin 1..."
      sendCommand(CMD_PLAY_FOLDER_FILE, 0X0F0102);  // Play the 2ndfile on folder 00
      break;  // you are done with Zone 0 action, return
    case 1:  // Zone 1 Outer Marker // 400 foot driveway
      sendCommand(CMD_PLAY_FOLDER_FILE, 0X0F0101);  // Play a single note file from folder 01
      break;
    case 2:  // Zone 2 Inner Marker // 50 foot driveway enters property
      sendCommand(CMD_PLAY_FOLDER_FILE, 0X0F0201);  // Play a double note file from folder 02      
      break;
    case 3:  // Zone 3 Curtilage
      sendCommand(CMD_PLAY_FOLDER_FILE, 0X0F0301);  // Play a triple note file from folder 03
      break; 
    case 4:  // Zone 4 DMZ
      sendCommand(CMD_PLAY_FOLDER_FILE, 0X0F0401);  // Play a 4 ships bell file from folder 04 // neighbor was in the Merchant Marine, his initials are DM...
      break; 
    case 5:  // Zone 5 North Side Entry
      sendCommand(CMD_PLAY_FOLDER_FILE, 0X0F0501);  // Play an alarm sound from folder 05
      break; 
    case 6:  // Zone 6 North Side No Pass
      sendCommand(CMD_PLAY_FOLDER_FILE, 0X0F0506);  // Play an alarm sound from folder 05
      break; 
    case 7:  // 
      sendCommand(CMD_PLAY_FOLDER_FILE, 0X0F0503);  // Play an alarm sound from folder 05
      break; 
    case 8:  //
      sendCommand(CMD_PLAY_FOLDER_FILE, 0X0F0601);  // Play an alarming sound from folder 06
      break;  
    case 9:  //
      sendCommand(CMD_PLAY_FOLDER_FILE, 0X0F0602);  // Play an alarming sound from folder 06
      break;  
    case 10: //
      sendCommand(CMD_PLAY_FOLDER_FILE, 0X0F0603);  // Play an alarming sound from folder 06
      break;
    case 11: //
      sendCommand(CMD_PLAY_FOLDER_FILE, 0X0F0609);  // Play an alarming sound from folder 06
      break;
    case 12: // Public Doorbell
      sendCommand(CMD_PLAY_FOLDER_FILE, 0X0F0610);  // Play an innocuous sound from folder 12
      break;
    case 13: // DBFH
      sendCommand(CMD_PLAY_FOLDER_FILE, 0X0F0304);  // Play an obnoxious sound from folder 13
      break;      
    default:
      // if nothing else matches, do the default
      // default is optional
      break;
  }
}

void sendCommand(uint8_t command, int16_t dat)

{
  delay(20);
  Send_buf[0] = 0x7e;    //starting byte
  Send_buf[1] = 0xff;    //version
  Send_buf[2] = 0x06   ; //the number of bytes of the command without starting byte and ending byte
  Send_buf[3] = command; //
  Send_buf[4] = 0x00;    //0x00 = no feedback, 0x01 = feedback
  Send_buf[5] = (int8_t)(dat >> 8);//datah
  Send_buf[6] = (int8_t)(dat); //datal
  Send_buf[7] = 0xef; //ending byte
  for(uint8_t i=0; i<8; i++)//

  {
    Serial2.write(Send_buf[i]); 
  }
    
}

this gets me one squeak, squawk, ding, gong, et cetera per pin pulled low. it assembles a command string and sends it through a serial port to the YX-5300.