Bird
Raised Fist0
Arduinoprogramming~20 mins

Playing melodies with tone() in Arduino - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Tone Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Arduino sketch?
Consider the following Arduino code that plays a simple melody using tone(). What will be the behavior when this code runs on a board connected to a speaker on pin 8?
Arduino
const int speakerPin = 8;
int melody[] = {262, 294, 330, 349};
int noteDurations[] = {4, 4, 4, 4};

void setup() {
  for (int thisNote = 0; thisNote < 4; thisNote++) {
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(speakerPin, melody[thisNote], noteDuration);
    delay(noteDuration * 1.3);
  }
}

void loop() {
  // no repeat
}
AThe speaker plays a continuous tone at 262Hz without pauses.
BThe speaker plays four notes (C4, D4, E4, F4) each for 250ms with short pauses, then stops.
CThe code causes a compilation error due to missing semicolons.
DThe speaker plays the notes but all at the same time, creating a chord.
Attempts:
2 left
💡 Hint
Think about how tone() and delay() work together to play notes sequentially.
🧠 Conceptual
intermediate
1:30remaining
Which statement about tone() is true?
Select the correct statement about the Arduino tone() function.
Atone() can play multiple tones simultaneously on different pins without interference.
Btone() automatically stops after 1 second if no duration is specified.
Ctone() requires an external library to work.
Dtone() stops any previous tone on the same pin when called again.
Attempts:
2 left
💡 Hint
Think about what happens if you call tone() twice on the same pin.
🔧 Debug
advanced
2:00remaining
Why does this melody play only one note?
This code is intended to play a melody of three notes, but only the last note sounds. What is the cause?
Arduino
const int speakerPin = 9;
int melody[] = {440, 494, 523};
int noteDurations[] = {500, 500, 500};

void setup() {
  for (int i = 0; i < 3; i++) {
    tone(speakerPin, melody[i]);
    delay(noteDurations[i]);
  }
}

void loop() {}
AThe speakerPin is not set as output, so only the last note plays.
BThe delay() function is too short to hear the notes properly.
Ctone() without duration plays indefinitely until stopped, so only the last note remains audible.
DThe melody array has invalid frequencies causing the first notes to be skipped.
Attempts:
2 left
💡 Hint
Check how tone() behaves when called without a duration parameter.
📝 Syntax
advanced
1:30remaining
Which option causes a compilation error?
Identify the code snippet that will cause a compilation error when trying to play a tone on pin 7.
Atone(7, 440 500); delay(500);
Btone(7, 440); delay(500); noTone(7);
Ctone(7, 440, 500); delay(500); noTone(7);
Dtone(7, 440, 500); delay(500);
Attempts:
2 left
💡 Hint
Look carefully at the parameters passed to tone().
🚀 Application
expert
2:30remaining
How many notes are played by this code?
Given the code below, how many distinct notes will be played on the speaker connected to pin 10?
Arduino
const int speakerPin = 10;
int melody[] = {262, 294, 330, 349, 392};
int noteDurations[] = {4, 8, 4, 8, 4};

void setup() {
  int i = 0;
  while (i < 5) {
    int duration = 1000 / noteDurations[i];
    tone(speakerPin, melody[i], duration);
    delay(duration * 1.2);
    i++;
  }
}

void loop() {}
A5 notes are played sequentially with varying durations.
BOnly 4 notes are played because the last note is skipped.
CThe code plays 5 notes but all with the same duration.
DNo notes are played because tone() is called inside a while loop.
Attempts:
2 left
💡 Hint
Count how many times tone() is called and check the loop condition.

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