Bird
0
0
Arduinoprogramming~10 mins

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

Choose your learning style9 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.