Summary of Arduino Sketch Sound Demo
This article describes an Arduino sketch named "SoundDemo" that generates audio and vibration patterns. It utilizes a speaker to produce tones like sirens and melodies, while a vibratory motor creates rhythmic buzzes. The code is structured with setup functions for pin initialization and a main loop that sequentially executes these sound effects using the `tone()` function and digital writes.
Parts used in the SoundDemo:
- Arduino Board
- Speaker
- Vibratory Motor (Pager Motor)
- Speaker Driver
- Motor Driver
This sketch is used by Exercise: Multichannel Bipolar Transistor Driver.

Full Source Code
The full code is all in one file SoundDemo.ino.
// SoundDemo - generate sound using a speaker and pager motor
//
// Copyright (c) 2016, Garth Zeglin. All rights reserved. Licensed under the
// terms of the BSD 3-clause license as included in LICENSE.
//
// This program assumes that:
//
// 1. A speaker is connected to pin 5 via a suitable driver.
// 2. A vibratory motor is connected to pin 6 via a suitable driver.
// 3. The serial console on the Arduino IDE is set to 9600 baud communications speed.
// ================================================================================
// Define constant values.
// The wiring assignment.
const int SPEAKER_PIN = 5;
const int MOTOR_PIN = 6;
// ================================================================================
// Configure the hardware once after booting up. This runs once after pressing
// reset or powering up the board.
void setup()
{
// Initialize the serial UART at 9600 bits per second.
Serial.begin(9600);
// Initialize the outputs.
pinMode(SPEAKER_PIN, OUTPUT);
digitalWrite(SPEAKER_PIN, LOW);
pinMode(MOTOR_PIN, OUTPUT);
digitalWrite(MOTOR_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);
delay(500);
tone(SPEAKER_PIN, 440);
delay(500);
}
noTone(SPEAKER_PIN);
// buzz a simple rhythm
for(int i = 0; i < 4; i++) {
digitalWrite(MOTOR_PIN, HIGH);
delay(450);
digitalWrite(MOTOR_PIN, LOW);
delay(50);
digitalWrite(MOTOR_PIN, HIGH);
delay(450);
digitalWrite(MOTOR_PIN, LOW);
delay(50);
digitalWrite(MOTOR_PIN, HIGH);
delay(200);
digitalWrite(MOTOR_PIN, LOW);
delay(50);
digitalWrite(MOTOR_PIN, HIGH);
delay(200);
digitalWrite(MOTOR_PIN, LOW);
delay(50);
}
// Play a simple melody, using a pitch table for more concise code.
// The following line declares an immutable table of integers.
const int pitches[] = { 262, 330, 392, 523, 494, 440, 392, 349, 330, 294, 262, -1 };
// Loop through the table. The break statement will exit this loop, so the
// continuation test is empty, which will be treated as 'true'.
for(int i = 0; /* note: empty */ ; i++) {
int pitch = pitches[i];
// Once the end marker is seen (an invalid pitch), exit the loop.
if (pitch < 0) break;
// Otherwise, play the tone
tone(SPEAKER_PIN, pitch);
delay(250);
}
noTone(SPEAKER_PIN);
// blissful silence
delay(2000);
}
Source: Arduino Sketch Sound Demo
- How do I set up the serial console for this project?
The serial console on the Arduino IDE must be set to 9600 baud communications speed. - Which pins are assigned to the speaker and motor?
The speaker connects to pin 5 and the vibratory motor connects to pin 6 via suitable drivers. - What happens if I press reset or power up the board?
The hardware configuration runs once after booting up to initialize the serial UART and output pins. - Can the code play different types of sounds?
Yes, it plays a siren tone, a simple rhythm on the motor, and a melody defined by a pitch table. - How does the program know when to stop playing the melody?
The loop exits when it encounters an end marker in the pitch table, which is indicated by a value less than zero. - Does the program run continuously?
Yes, the Arduino system calls the main event loop function over and over forever. - What is the duration of the silence between sound sequences?
The program includes a blissful silence delay of 2000 milliseconds after playing the melody. - Can I modify the frequencies used in the siren?
The code currently uses fixed frequencies of 622 Hz and 440 Hz for the siren tone within the loop.
