Tone on Multiple Speakers
Play tones on multiple speakers sequentially using the tone() command.
This example shows how to use the tone() command to play different notes on multiple outputs.
The tone() command works by taking over one of the Atmega's internal timers, setting it to the frequency you want, and using the timer to pulse an output pin. Since it's only using one timer, you can only play one note at a time. You can, however, play notes on different pins, sequentially. To do this, you need to turn the timer off for one pin before moving on to the next.
Thanks to Greg Borenstein for clarifying this.
Hardware Required
Arduino Board
3 8 ohm speakers
3 100 ohm resistor
hook-up wires
breadboard
Circuit
Schematic
Code
The sketch below plays a tone on each of the speakers in sequence, turning off the previous speaker first. Note that the duration of each tone is the same as the delay that follows it.
Here's the main sketch:
1/*2
3 Multiple tone player4
5 Plays multiple tones on multiple pins in sequence6
7 circuit:8
9 - three 8 ohm speakers on digital pins 6, 7, and 810
11 created 8 Mar 201012
13 by Tom Igoe14
15 based on a snippet from Greg Borenstein16
17 This example code is in the public domain.18
19 https://www.arduino.cc/en/Tutorial/Tone420
21*/22
23void setup() {24
25}26
27void loop() {28
29 // turn off tone function for pin 8:30
31 noTone(8);32
33 // play a note on pin 6 for 200 ms:34
35 tone(6, 440, 200);36
37 delay(200);38
39 // turn off tone function for pin 6:40
41 noTone(6);42
43 // play a note on pin 7 for 500 ms:44
45 tone(7, 494, 500);46
47 delay(500);48
49 // turn off tone function for pin 7:50
51 noTone(7);52
53 // play a note on pin 8 for 300 ms:54
55 tone(8, 523, 300);56
57 delay(300);58}
Learn more
You can find more basic tutorials in the built-in examples section.
You can also explore the language reference, a detailed collection of the Arduino programming language.
Last revision 2015/08/11 by SM
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.