Arduino

How to Tone on Multiple Speakers in Arduino

The Arduino boards are very useful for using microcontrollers to work on multiple projects and interfacing multiple sensors. The most commonly used Arduino board is the Arduino Uno which is the best option for doing small projects. To interface speakers with Arduino Uno and giving each speaker a different note is performed in this discourse.

To tone the multiple speakers using Arduino the tone function can be used. The function tone has three arguments: one is the pin number on which the speaker is connected to Arduino, the second argument is the frequency of the tone and the third is the duration for which the tone is to be played. The function generates a square wave of the given frequency. Similarly, if multiple speakers are to be toned a noTone function is used to stop the respective speaker so that the next speaker can run. There is only one argument for the noTone function that is the pin number of the respective speaker whose tone is to be stopped. The syntax for the tone and no tone function is given as:

tone(pin-number, frequency, duration);
noTone(pin-number);

There are three speakers used in the circuit; each speaker is assigned to a separate pin of Arduino and each speaker runs with a delay of 2 seconds. The circuit diagram for giving tones to different speakers is given as:

The code for the toning the three speakers using Arduino is given as:

void setup() {

}

void loop() {

  // playing a tone on pin 5 for 100 ms
  tone(5, 400, 100);
  delay(200);
  // turn off the tone of pin 5
  noTone(5);
  // play a note on pin 4 for 300 ms
  tone(4, 500, 300);
  delay(200);
  // turn off the tone of pin 4
  noTone(4);
  // play a note on pin 3 for 100 ms
  tone(3, 500, 100);
  delay(200);
  // turn off the tone of pin 3
  noTone(3);
}

In the circuit the tone is generated on the digital pins 3,4,5 of Arduino and after each tone() function a noTone() function is used so that the other pins can also generate the tone.

Simulation output:

Conclusion

To generate melodies using Arduino the tone function can be used. This function uses the frequency given by the user on which the tone is generated. Similarly, this function can be used for sounding an alarm for different levels of warnings giving different frequencies  for each type of warning. This discourse briefly explains how to generate three different tones on three speakers with a delay of 2 milliseconds.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.