Contenido principal

playTone

R2026b

Play tone on piezo speaker using digital pin

Description

Add-On Required: This feature requires the MATLAB Support Package for Arduino Hardware add-on.

playTone(a,pin,frequency,duration) plays a tone on a piezo speaker attached to the digital pin on Arduino® hardware at the specified pin and frequency for the specified duration.

example

playTone(a,pin) plays a tone on a piezo speaker attached to the digital pin on Arduino hardware.

playTone(a,pin,frequency) plays a tone on a piezo speaker attached to the digital pin on Arduino hardware at the specified pin and frequency.

Examples

collapse all

Play a tone connected to digital pin 5 on the Arduino hardware for 10 seconds at 2400Hz.

a = arduino();
playTone(a,'D5',2400,10);

Input Arguments

collapse all

Arduino hardware connection created using arduino, specified as an object.

Digital pin number on the physical hardware, specified as a character vector.

Frequency of tone to be played, specified as a number between the frequency range 0 and 32767Hz.

Duration of tone to be played specified as a number, which represents the number of seconds. Valid values are between 0 and 60.

Limitations

This function is not supported on the Arduino Due and ESP32 boards.

Alternative Functionality

App

Use the Pin Explorer pane in Arduino Explorer to play a tone on a piezo speaker, for a particular frequency or duration. You can use the app in the installed version of MATLAB® and MATLAB Online™. The speaker can be attached to a digital pin on Arduino or other supported hardware. In the Pin Configuration panel, set the Mode as Tone, Tone Mode as Frequency or Duration, and if required specify a Label to uniquely identify the pin. For more information, see Configure Read and Write for GPIO Pins Using Arduino Explorer App.

Version History

Introduced in R2014b