SoundDemo Arduino Sketch

This plays tones on an speaker which has been wired an Arduino digital output pin. An unamplified speaker will make sound, but very quietly; it is recommended to wire it in series with a 220 ohm resistor to minimize chances of damaging the Arduino. Using a driver circuit is recommended for both higher volume and to reduce the load on the Arduino pin. Example circuits include a MOSFET transistor driver (e.g. IRF540), an integrated circuit driver (e.g. ULN2803), or an audio amplifier module.

Full Source Code

The full code is all in one file SoundDemo.ino.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/// \file SoundDemo.ino
/// \brief Arduino program demonstrating the generation of sound tones using an external speaker
//
/// \copyright No copyright, 2016-2019, Garth Zeglin.  This file is explicitly placed in the public domain.

// This program assumes that:
//
//  1. A speaker is connected to pin 5 via a suitable driver.

// ================================================================================
// Define constant values.

/// Digital I/O pin to which the speaker or speaker driver is attached.
const int SPEAKER_PIN = 5;

// ================================================================================
/// Configure the hardware once after booting up.  This runs once after pressing
/// reset or powering up the board.
void setup()
{
  // Initialize the outputs.
  pinMode(SPEAKER_PIN, OUTPUT);
  digitalWrite(SPEAKER_PIN, LOW);
}
// ================================================================================
/// Run one iteration of the main event loop.  The Arduino system will call this
/// function over and over forever.
void loop()
{
  // play a siren tone
  for(int i = 0; i < 4; i++) {
    tone(SPEAKER_PIN, 622);  // enable a tone on the specified I/O pin with given frequency
    delay(500);              // pause to allow it to be heard
    
    tone(SPEAKER_PIN, 440);  // and again with a different frequency
    delay(500);
  }

  // brief quiet
  noTone(SPEAKER_PIN);
  delay(1000);

  // Play a simple melody, using a pitch table for more concise code.  Each
  // value is a tone frequency in Hz (cycles/second).  The following line
  // declares an immutable table of integers.  The last entry must be a negative
  // number to indicate the end.
  const int pitches[] = { 262, 330, 392, 523, 494, 440, 392, 349, 330, 294, 262, -1 };

  // Loop through the table.  Note that the loop condition (second clause)
  // checks that the current entry in the pitch table is valid, so this loop
  // will exit once the final value is reached.
  for(int i = 0; pitches[i] > 0; i++) {

    // enable tone production on the specified I/O pin using a frequency value from the table
    tone(SPEAKER_PIN, pitches[i]);

    // brief pause to allow the sound to be heard
    delay(250);
  }

  // blissful silence
  noTone(SPEAKER_PIN);
  delay(2000);
}