Play multiple piezos simultaneously

Hi there:

For my project, I’m trying to have an infrared sensor trigger the sounding of 5 piezos. The piezos would be playing at the same frequency (4000 Hz), and at the same time.

I’ve succeeded in having the sensor trigger 1 piezo, but for some reason my code for 5 piezos does not work.

I’ve attached a diagram of the setup below, as well as the code:

IRSong is my working code for 1 piezo
IRSong_5piezos is my attempted code for 5 piezos
IR Song sketch_bb.pdf is a diagram of my setup

Thanks so much guys!

IR Song sketch_bb.pdf (599 KB)

IRSong.ino (1.1 KB)

IRSong_5piezos.ino (1.19 KB)

rkduino:
The piezos would be playing at the same frequency (4000 Hz), and at the same time.

In that case, why not just put all the piezos in parallel and drive them off the same signal?

From the tone() webpage:

Only one tone can be generated at a time. If a tone is already playing on a different pin, the call to tone() will have no effect. If the tone is playing on the same pin, the call will set its frequency.

https://www.arduino.cc/en/Reference/Tone

My suspicion is that this code

    tone(9, 4000, 100);
    tone(10, 2000, 100);
    tone(11, 2000, 100);
    tone(12, 1000, 100);
    tone(13, 500, 100);

may have violated the limitation above.

I’d try putting a delay of 105ms after each call to tone()

tone(9, 4000, 100);
delay(105);
tone(10, 2000, 100);
delay(105);
tone(11, 2000, 100);
delay(105);
tone(12, 1000, 100);
delay(105);
tone(13, 500, 100);
delay(105);

rkduino:
The piezos would be playing at the same frequency (4000 Hz), and at the same time.

Your code won't do that. You've got a mix of frequencies in the code I quoted above. And as I pointed out previously, issuing a succession of calls to tone(), without any delays in between, may cause problems.