tone() and noTone() in Arduino

ArduinoHardwareSoftware & Coding

The tone function can be used to generate a square wave (50% duty cycle) of a specific frequency on a pin.

Syntax

The syntax is −

tone(pin, frequency)

pin is the pin number on which to generate the tone. The frequency is specified in Hz.

This function can also take in a third optional argument − the millisecond duration for which the tone should be generated on the pin.

tone(pin,frequency,duration)

If you don’t specify the duration, the tone will continue till the noTone() function is called on the same pin. The syntax of the noTone() function is −

noTone(pin)

where pin is the pin number on which you wish to stop the tone. noTone() will have no effect if there is no tone being generated on the specified pin.

Points to Note

  • Only one tone can be called at a time, across all pins. Thus, if there is a tone running on pin 6, then trying to generate another tone on pin 5 will have no effect

  • The minimum frequency for generating tones is 31 Hz. You can’t generate tones of frequencies below that.

  • The tone() function interferes with the PWM output on pins 3 and 11 (except on the Mega board). It is recommended to not use the two together.

raja
Published on 30-Jul-2021 12:43:48
Advertisements