Bird
Raised Fist0
Arduinoprogramming~10 mins

Playing melodies with tone() in Arduino - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Playing melodies with tone()
Start
↓
Define notes and durations
↓
Loop through each note
↓
Call tone() to play note
↓
Delay for note duration
↓
Stop tone()
↓
Next note or End
↓
Repeat or Stop
The program defines notes and durations, then plays each note using tone(), waits, stops the tone, and moves to the next note.
Execution Sample
Arduino
int melody[] = {262, 294, 330};
int duration[] = {500, 500, 500};
for (int i=0; i<3; i++) {
  tone(8, melody[i]);
  delay(duration[i]);
  noTone(8);
}
This code plays three notes (C4, D4, E4) on pin 8, each for 500 milliseconds.
Execution Table
Stepimelody[i]ActionOutput/Effect
10262tone(8, 262)Pin 8 plays note C4 (262 Hz)
20262delay(500)Wait 500 ms while note plays
30262noTone(8)Stop tone on pin 8
41294tone(8, 294)Pin 8 plays note D4 (294 Hz)
51294delay(500)Wait 500 ms while note plays
61294noTone(8)Stop tone on pin 8
72330tone(8, 330)Pin 8 plays note E4 (330 Hz)
82330delay(500)Wait 500 ms while note plays
92330noTone(8)Stop tone on pin 8
103N/ALoop endsAll notes played, melody finished
💡 i reaches 3, loop condition i<3 is false, melody playback ends
Variable Tracker
VariableStartAfter 1After 2After 3Final
iundefined0123
melody[i]N/A262294330N/A
Key Moments - 3 Insights
Why do we call noTone() after delay()?
Because tone() starts playing the note and keeps playing until stopped. delay() waits while the note plays, then noTone() stops the sound. See execution_table rows 2 and 3.
What happens if we forget noTone()?
The last note will keep playing continuously because tone() never stops. The program won't move on properly. This is why noTone() is important after each note.
Why do we use a loop to play the melody?
The loop lets us play each note in order without repeating code. It goes through melody[] and duration[] arrays step by step, as shown in execution_table steps 1 to 9.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the frequency played at step 4?
A262 Hz
B330 Hz
C294 Hz
DNo frequency played
💡 Hint
Check the 'melody[i]' value at step 4 in the execution_table.
At which step does the program stop playing the last note?
AStep 9
BStep 8
CStep 10
DStep 7
💡 Hint
Look for noTone(8) call for the last note in execution_table.
If we increase delay duration to 1000 ms, how does execution_table change?
ATone frequencies change
BDelay steps show 1000 ms instead of 500 ms
CLoop runs fewer times
DnoTone() calls are removed
💡 Hint
Check the 'Action' and 'Output/Effect' columns for delay() steps in execution_table.
Concept Snapshot
Use tone(pin, frequency) to play a note on a pin.
Use delay(duration) to wait while note plays.
Use noTone(pin) to stop the note.
Loop through arrays of notes and durations to play melodies.
Each note plays until noTone() stops it.
This creates simple melodies on Arduino.
Full Transcript
This example shows how to play a melody on Arduino using tone(). We define arrays for notes and durations. A loop goes through each note, calls tone() to start playing, waits with delay(), then calls noTone() to stop. This repeats for all notes. The execution table shows each step: playing a note, waiting, stopping, then moving to the next. Variables i and melody[i] change as the loop runs. Key points: noTone() stops the sound, delay() controls note length, and looping plays all notes in order. The quiz checks understanding of frequencies played, when notes stop, and effects of changing delay.

Practice

(1/5)
1. What does the tone() function do in Arduino?
easy
A. It reads the value from a sensor.
B. It controls the brightness of an LED.
C. It plays a sound at a specific frequency on a speaker.
D. It sends data over the serial port.

Solution

  1. Step 1: Understand the purpose of tone()

    The tone() function generates a square wave of the specified frequency on a pin, producing sound through a speaker.
  2. Step 2: Compare options with function behavior

    Only It plays a sound at a specific frequency on a speaker. describes playing sound at a frequency, which matches tone() usage.
  3. Final Answer:

    It plays a sound at a specific frequency on a speaker. -> Option C
  4. Quick Check:

    tone() = plays sound [OK]
Hint: Remember: tone() controls sound frequency, not LEDs or sensors [OK]
Common Mistakes:
  • Confusing tone() with analogWrite() for LEDs
  • Thinking tone() reads sensor data
  • Assuming tone() sends serial data
2. Which of the following is the correct syntax to play a 440 Hz tone on pin 8 for 500 milliseconds?
easy
A. tone(8, 440); delay(500);
B. tone(440, 8, 500);
C. tone(8, 500, 440);
D. tone(8, 440, 500);

Solution

  1. Step 1: Recall tone() function parameters

    The correct order is tone(pin, frequency, duration). So pin 8, frequency 440 Hz, duration 500 ms.
  2. Step 2: Check each option's parameter order

    tone(8, 440, 500); matches the correct order. tone(8, 440); delay(500); plays tone without duration, relying on delay, which is valid but not the asked syntax.
  3. Final Answer:

    tone(8, 440, 500); -> Option D
  4. Quick Check:

    tone(pin, freq, duration) = tone(8, 440, 500) [OK]
Hint: Remember tone(pin, frequency, duration) order [OK]
Common Mistakes:
  • Swapping frequency and pin parameters
  • Confusing duration with frequency
  • Omitting duration but expecting timed tone
3. What will the following Arduino code do?
void setup() {
  tone(9, 262, 250);
  delay(300);
  tone(9, 294, 250);
  delay(300);
  noTone(9);
}
void loop() {}
medium
A. Play two notes (C4 and D4) on pin 9 with pauses, then stop sound.
B. Play one continuous note at 262 Hz for 800 ms.
C. Play no sound because noTone() stops all tones immediately.
D. Cause a compile error due to missing loop content.

Solution

  1. Step 1: Analyze tone() and delay() calls

    The code plays 262 Hz tone for 250 ms, waits 300 ms, then plays 294 Hz tone for 250 ms, waits 300 ms.
  2. Step 2: Understand noTone() effect

    noTone(9) stops any tone on pin 9 after the notes play. The loop is empty, so no repetition.
  3. Final Answer:

    Play two notes (C4 and D4) on pin 9 with pauses, then stop sound. -> Option A
  4. Quick Check:

    tone + delay + noTone = two notes then stop [OK]
Hint: Check tone frequency and delays to predict sound timing [OK]
Common Mistakes:
  • Assuming tones play simultaneously
  • Thinking noTone() stops before notes play
  • Believing empty loop causes error
4. Which statement correctly describes the behavior of this code snippet:
void setup() {
  tone(7, 330);
  delay(500);
  tone(7, 349);
  delay(500);
  noTone(7);
}
void loop() {}
medium
A. The delay() calls are too short to hear the notes.
B. The second tone replaces the first (which continues until replaced), resulting in sequential notes without overlap.
C. noTone() is called too early, stopping the melody immediately.
D. tone() requires a duration parameter to work correctly.

Solution

  1. Step 1: Understand tone() without duration

    Calling tone(pin, freq) plays the tone continuously until stopped or replaced by another tone() on the same pin.
  2. Step 2: Analyze timing of tones and delays

    The first tone starts and plays for 500 ms during delay(). The second tone replaces it immediately, playing the new frequency for the next 500 ms. No overlap occurs.
  3. Final Answer:

    The second tone replaces the first (which continues until replaced), resulting in sequential notes without overlap. -> Option B
  4. Quick Check:

    tone without duration replaced by next tone() = sequential notes [OK]
Hint: tone() without duration continues until next tone() or noTone() [OK]
Common Mistakes:
  • Assuming tone() stops automatically after delay
  • Thinking tone() calls on same pin cause overlap
  • Believing delay controls tone length without duration
5. You want to play a melody of 3 notes (262 Hz, 294 Hz, 330 Hz) each lasting 300 ms on pin 10 with 100 ms pause between notes. Which code snippet correctly achieves this?
hard
A. for (int i = 0; i < 3; i++) { int notes[] = {262, 294, 330}; tone(10, notes[i], 300); delay(400); } noTone(10);
B. int notes[] = {262, 294, 330}; for (int i = 0; i < 3; i++) { tone(10, notes[i]); delay(250); noTone(10); delay(100); }
C. int notes[] = {262, 294, 330}; for (int i = 0; i < 3; i++) { tone(10, notes[i], 300); delay(250); noTone(10); delay(100); }
D. int notes[] = {262, 294, 330}; for (int i = 0; i < 3; i++) { tone(10, notes[i]); delay(400); } noTone(10);

Solution

  1. Step 1: Understand timing requirements

    Each note plays 300 ms, plus 100 ms pause, total 400 ms delay between notes.
  2. Step 2: Check code for correct tone duration and delay

    for (int i = 0; i < 3; i++) { int notes[] = {262, 294, 330}; tone(10, notes[i], 300); delay(400); } noTone(10); uses tone with duration 300 ms and delay 400 ms, matching timing. noTone(10) after loop stops sound.
  3. Step 3: Verify other options

    int notes[] = {262, 294, 330}; for (int i = 0; i < 3; i++) { tone(10, notes[i]); delay(250); noTone(10); delay(100); } calls noTone inside loop, cutting notes short. int notes[] = {262, 294, 330}; for (int i = 0; i < 3; i++) { tone(10, notes[i], 300); delay(250); noTone(10); delay(100); } delays only 250 ms, missing pause. int notes[] = {262, 294, 330}; for (int i = 0; i < 3; i++) { tone(10, notes[i]); delay(400); } noTone(10); plays tone without duration, causing overlap.
  4. Final Answer:

    for (int i = 0; i < 3; i++) { int notes[] = {262, 294, 330}; tone(10, notes[i], 300); delay(400); } noTone(10); -> Option A
  5. Quick Check:

    tone(duration) + delay(total note+pause) = for (int i = 0; i < 3; i++) { int notes[] = {262, 294, 330}; tone(10, notes[i], 300); delay(400); } noTone(10); [OK]
Hint: Use tone(duration) plus delay(total note+pause) for melody timing [OK]
Common Mistakes:
  • Calling noTone() inside loop too early
  • Using delay shorter than note+pause
  • Omitting duration causing overlapping notes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard