How to play individual sounds in multiple speakers using PCM library

Hello, I am currently working on a school project. Everything is going smoothly up until to the arduino part. The arduino handles the speaker using the PCM library. What happens is, ESP32 sends a string "alarm" to play the audio once the arduino receives this string through serial connection. I have read the library of the PCM and have found that it uses pin 11 as the default pin. My question is, is it possible for me to play audio in three or four different speakers? For example
speaker1 - 11
speaker2 - 10
speaker3 - 9

if it is, how can i do it?

#include <PCM.h>

const int speakerPin2 = 3;
const int speakerPin3 = 10;
const int speakerPin4 = 9;

const unsigned char sample[] PROGMEM = { (audio bits, have not included since it is too long)
void setup() {
  Serial.begin(9600);
  while(!Serial);
}

void loop() {
  if (Serial.available()){
    String ESP = Serial.readString();
    if (ESP == "ALARM"){
      startPlayback(sample, sizeof(sample));
      Serial.println(ESP);
    } else {
      Serial.println("Good");
    }
  }

}

You can't do it with snippets of code! Are you REALLY connecting speakers directly to Arduino pins?

1 Like

Not without additional circuitry. The PCM library requires an output pin connected to an internal timer to produce audio, which then could be directed by your custom circuitry to different audio amplifiers and speakers.

no, i am following this guide on how to play audio with arduino

i have used this setup to play audio, i am wondering if it is possible to play a single audio played by 3-4 speakers individually
for example

if (string == "alarm1"){
//speaker1 plays
} else if (string =="alarm2"){
//speaker2 plays
} else if (string == "alarm3"){
//speaker3 plays
}

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