Bird
Raised Fist0
Arduinoprogramming~10 mins

tone() function for frequency generation in Arduino - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - tone() function for frequency generation
Call tone(pin, frequency)
↓
Arduino sets pin HIGH and LOW
↓
Pin outputs square wave at frequency
↓
Sound or signal generated
↓
tone() runs until noTone(pin) called or duration ends
The tone() function starts a square wave on a pin at a set frequency, producing sound or signals until stopped.
Execution Sample
Arduino
tone(8, 440);
// Play 440Hz tone on pin 8
delay(1000);
noTone(8);
This code plays a 440Hz tone on pin 8 for 1 second, then stops the tone.
Execution Table
StepFunction CallPin StateFrequency (Hz)ActionOutput
1tone(8, 440)Pin 8 outputs square wave440Start toneSound at 440Hz starts
2delay(1000)Pin 8 continues square wave440Wait 1 secondTone continues playing
3noTone(8)Pin 8 stops output0Stop toneSound stops
4-Pin 8 LOW0IdleNo sound
💡 Tone stops after noTone(8) is called, pin output returns to LOW
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Pin 8 StateLOWSquare wave HIGH/LOWSquare wave HIGH/LOWLOWLOW
Frequency044044000
Key Moments - 3 Insights
Why does the tone keep playing during delay()?
Because tone() sets the pin to output a square wave independently, delay() just pauses the program but the tone continues (see execution_table rows 1 and 2).
What happens if noTone() is not called?
The tone keeps playing indefinitely on the pin until stopped or the program ends (execution_table row 2 shows tone continuing).
Can tone() play multiple frequencies on different pins at the same time?
Yes, tone() can be called on different pins with different frequencies independently.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the pin 8 state after step 3?
ALOW
BSquare wave HIGH/LOW
CHIGH
DUndefined
💡 Hint
Check the 'Pin State' column in execution_table row 3
At which step does the tone stop playing?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' and 'Output' columns in execution_table
If delay(1000) was changed to delay(500), how would the execution_table change?
ATone would stop at step 2
BTone frequency would change
CTone would stop earlier at step 3 after 500ms
DPin state would be HIGH after step 3
💡 Hint
Delay controls how long tone plays before noTone stops it (see variable_tracker and execution_table)
Concept Snapshot
tone(pin, frequency) starts a square wave on the pin at the given frequency.
The tone continues until noTone(pin) is called or duration ends.
Use delay() to control how long the tone plays.
noTone(pin) stops the sound and sets pin LOW.
Multiple pins can play tones independently.
Full Transcript
The tone() function in Arduino generates a square wave on a specified pin at a given frequency. When you call tone(pin, frequency), the Arduino sets the pin to output a square wave that produces sound or signals at that frequency. This tone continues playing independently of the main program flow, so even if you use delay(), the tone keeps playing. To stop the tone, you call noTone(pin), which stops the square wave and sets the pin back to LOW. This allows you to control how long the tone plays by combining tone(), delay(), and noTone(). Multiple pins can have tones playing at different frequencies at the same time.

Practice

(1/5)
1. What does the tone() function do in Arduino?
easy
A. Generates a sound of a specific frequency on a pin
B. Reads the voltage from a sensor
C. Turns on an LED
D. Sends data over serial communication

Solution

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

    The tone() function is used to create sound by generating a square wave of a specific frequency on a pin.
  2. Step 2: Compare options with function behavior

    Only Generates a sound of a specific frequency on a pin describes generating sound with frequency, which matches tone() usage.
  3. Final Answer:

    Generates a sound of a specific frequency on a pin -> Option A
  4. Quick Check:

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

Solution

  1. Step 1: Recall tone() function parameters

    The correct order is tone(pin, frequency, duration). So pin 8, frequency 1000 Hz, duration 500 ms means tone(8, 1000, 500);.
  2. Step 2: Check each option

    tone(8, 1000, 500); matches the correct order. tone(1000, 8, 500); swaps pin and frequency. tone(8, 500, 1000); swaps frequency and duration. tone(8, 1000); delay(500); starts continuous tone but does not stop after 500 ms, unlike duration parameter.
  3. Final Answer:

    tone(8, 1000, 500); -> Option A
  4. Quick Check:

    tone(pin, freq, duration) = tone(8, 1000, 500) [OK]
Hint: Remember parameter order: pin, frequency, duration [OK]
Common Mistakes:
  • Swapping frequency and pin parameters
  • Confusing duration with frequency
  • Omitting duration when required
3. What will be the output behavior of this Arduino code snippet?
void setup() {
  tone(9, 440, 1000);
  delay(1500);
  noTone(9);
}
void loop() {}
medium
A. Pin 9 outputs a 440 Hz tone for 1.5 seconds, then stops
B. Pin 9 outputs a 440 Hz tone continuously without stopping
C. Pin 9 outputs a 440 Hz tone for 1 second, then stops
D. No sound is produced because noTone() is called immediately

Solution

  1. Step 1: Analyze tone() with duration

    The tone(9, 440, 1000); plays a 440 Hz tone on pin 9 for 1000 milliseconds (1 second).
  2. Step 2: Understand delay and noTone()

    The program waits 1500 ms, which is longer than the tone duration, so the tone stops automatically after 1 second. Then noTone(9); stops any tone if still playing (safe call).
  3. Final Answer:

    Pin 9 outputs a 440 Hz tone for 1 second, then stops -> Option C
  4. Quick Check:

    tone(9, 440, 1000) plays 1s tone [OK]
Hint: Duration in tone() controls length; noTone() stops early [OK]
Common Mistakes:
  • Thinking tone plays for delay length
  • Assuming noTone() stops tone immediately before playing
  • Confusing tone duration with delay time
4. Identify the error in this code snippet that tries to play a 500 Hz tone on pin 7:
void setup() {
  tone(7, 500);
  delay(3000);
  noTone(7);
}
void loop() {
  tone(7, 500, 1000);
}
medium
A. tone() requires frequency before pin number
B. Calling tone() inside loop() with duration causes overlapping tones
C. noTone() should be called before tone() in setup()
D. delay() cannot be used with tone()

Solution

  1. Step 1: Review tone() usage in loop()

    Calling tone(7, 500, 1000); repeatedly in loop() will restart the tone every cycle, causing overlapping or interrupted sounds.
  2. Step 2: Check other options

    noTone() can be called after tone(), tone() parameters order is correct, and delay() can be used with tone(). So only Calling tone() inside loop() with duration causes overlapping tones describes the real problem.
  3. Final Answer:

    Calling tone() inside loop() with duration causes overlapping tones -> Option B
  4. Quick Check:

    Repeated tone() with duration in loop causes overlap [OK]
Hint: Avoid tone() with duration inside loop() to prevent overlap [OK]
Common Mistakes:
  • Calling noTone() before tone() unnecessarily
  • Mixing parameter order in tone()
  • Thinking delay() blocks tone() sound
5. You want to play a melody using tone() on pin 6 with these notes and durations: 262 Hz for 500 ms, 294 Hz for 500 ms, and 330 Hz for 1000 ms. Which code correctly plays this sequence without overlapping tones?
hard
A. void setup() { tone(6, 262, 500); delay(1000); tone(6, 294, 500); delay(1000); tone(6, 330, 1000); delay(1000); } void loop() {}
B. void setup() { tone(6, 262); delay(500); tone(6, 294); delay(500); tone(6, 330); delay(1000); } void loop() {}
C. void setup() { tone(6, 262, 500); tone(6, 294, 500); tone(6, 330, 1000); } void loop() {}
D. void setup() { tone(6, 262, 500); delay(500); tone(6, 294, 500); delay(500); tone(6, 330, 1000); delay(1000); } void loop() {}

Solution

  1. Step 1: Understand tone() with duration and delay

    Using tone(pin, freq, duration) plays the note for the duration. To avoid overlap, delay should match the tone duration before playing the next note.
  2. Step 2: Evaluate each option

    void setup() { tone(6, 262, 500); delay(500); tone(6, 294, 500); delay(500); tone(6, 330, 1000); delay(1000); } void loop() {} plays each tone with correct duration and delays matching durations, so notes play sequentially without overlap. void setup() { tone(6, 262); delay(500); tone(6, 294); delay(500); tone(6, 330); delay(1000); } void loop() {} omits duration in tone(), causing continuous tones. void setup() { tone(6, 262, 500); tone(6, 294, 500); tone(6, 330, 1000); } void loop() {} calls tone() three times without delay, overlapping sounds. void setup() { tone(6, 262, 500); delay(1000); tone(6, 294, 500); delay(1000); tone(6, 330, 1000); delay(1000); } void loop() {} uses delays longer than tone durations, causing unwanted silence.
  3. Final Answer:

    tone(6, 262, 500); delay(500); tone(6, 294, 500); delay(500); tone(6, 330, 1000); delay(1000); -> Option D
  4. Quick Check:

    Match delay to tone duration for clean melody [OK]
Hint: Use delay equal to tone duration to separate notes [OK]
Common Mistakes:
  • Omitting delay after tone() with duration
  • Calling multiple tone() without delay causing overlap
  • Using longer delays causing silence gaps
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