Loading...
Searching...
No Matches

Notes on a piezo buzzer: tone() and noTone(). More...

Functions

 tone (pin, frequency, duration=None)
 Plays a square wave (a note) on a pin, for example for a piezo buzzer.
 noTone (pin)
 Stops a tone() on a pin.

Detailed Description

Notes on a piezo buzzer: tone() and noTone().

Function Documentation

◆ tone()

tone ( pin,
frequency,
duration = None )

Plays a square wave (a note) on a pin, for example for a piezo buzzer.

Returns straight away (well under 1 ms); the note plays on while your code runs. The note is a square wave, high half the time, on the pin's PWM output, so the PWM pin pairs apply; it also sets the frequency of the pin next to it on the slice (see pwm() in phyduino.py).

Only one note at a time has a stop time: a new tone(), on any pin, cancels the last one's duration, so play notes one after another, each followed by a delay().

Parameters
pinPin number (8 is D8) or name ("D8").
frequencyFrequency in Hz, 8 Hz and up; 0 is silence.
durationMilliseconds to play for; None plays until noTone().
Exceptions
ValueErrorUnknown pin, an analogue-only pin (A3-A5), or the pin shares its PWM output with one already in use.
Example
tone(8, 440, 500) # the note A4 (440 Hz) on D8 for half a second
delay(650) # wait for the note, plus a short gap

◆ noTone()

noTone ( pin)

Stops a tone() on a pin.

Sets the pin's duty to 0, so it stays low. It does nothing to a pin that has had no tone() or analogWrite() since the program started.

Parameters
pinPin number (8 is D8) or name ("D8").