Skip to main content

Meadow.Foundation.Audio.PiezoSpeaker

PiezoSpeaker
StatusStatus badge: working
Source codeGitHub
NuGet packageNuGet Gallery for Meadow.Foundation

The PiezoSpeaker class represents a piezoelectric speaker that can be used to generate tones across a range of frequencies. They typically perform well between 1-5kHz but may go as high as 100kHz. PiezoSpeaker Implements IToneGenerator.

The positive pin of the piezo speaker connects to a pulse width modulation (PWM) capable pin on the Meadow and the negative pin connects to common/ground. A resistor can be placed in-line to reduce volume.

Piezo Speakers

Code Example​

protected PiezoSpeaker piezoSpeaker;

public override Task Initialize()
{
Resolver.Log.Info("Initializing...");

piezoSpeaker = new PiezoSpeaker(Device.CreatePwmPort(Device.Pins.D05, new Frequency(100, Frequency.UnitType.Hertz)));

return Task.CompletedTask;
}

public override async Task Run()
{
for (int i = 0; i < 5; i++)
{
Resolver.Log.Info("Playing A major triad starting at A4");
await piezoSpeaker.PlayTone(new Frequency(440, Frequency.UnitType.Hertz), TimeSpan.FromMilliseconds(500)); //A
await piezoSpeaker.PlayTone(new Frequency(554.37f, Frequency.UnitType.Hertz), TimeSpan.FromMilliseconds(500)); //C#
await piezoSpeaker.PlayTone(new Frequency(659.25f, Frequency.UnitType.Hertz), TimeSpan.FromMilliseconds(500)); //E

await Task.Delay(2500);
}
}

Sample project(s) available on GitHub

Wiring Example​

Wiring a Piezo Speaker to a Meadow F7

API Reference​

Class PiezoSpeaker

Represents a 2 pin piezo-electric speaker capable of generating tones

Assembly: Meadow.Foundation.dll​
View Source​
Declaration
public class PiezoSpeaker : IToneGenerator, IDisposable

Implements:
Meadow.Peripherals.Speakers.IToneGenerator, System.IDisposable

Properties​

Volume​

The volume from 0-1 Defined by the PWM port duty cycle from 0 to 0.5

View Source​
Declaration
public float Volume { get; protected set; }

Port​

Gets the port that is driving the Piezo Speaker

View Source​
Declaration
protected IPwmPort Port { get; set; }

IsDisposed​

Is the object disposed

View Source​
Declaration
public bool IsDisposed { get; }

Methods​

PlayTone(Frequency)​

Play a frequency until stopped by StopTone

View Source​
Declaration
public Task PlayTone(Frequency frequency)
Returns​

System.Threading.Tasks.Task

Parameters​
TypeNameDescription
Meadow.Units.FrequencyfrequencyThe frequency in hertz of the tone to be played

PlayTone(Frequency, TimeSpan)​

Play a frequency for a specified duration

View Source​
Declaration
public Task PlayTone(Frequency frequency, TimeSpan duration)
Returns​

System.Threading.Tasks.Task

Parameters​
TypeNameDescription
Meadow.Units.FrequencyfrequencyThe frequency in hertz of the tone to be played
System.TimeSpandurationHow long the note is played in milliseconds, if duration is 0, tone plays indefinitely

StopTone()​

Stops a tone playing

View Source​
Declaration
public void StopTone()

SetVolume(float)​

Set the playback volume

View Source​
Declaration
public void SetVolume(float volume)
Parameters​
TypeNameDescription
System.SinglevolumeThe volume from 0 (off) to 1 (max volume)

Dispose()​

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

View Source​
Declaration
public void Dispose()

Dispose(bool)​

Dispose of the object

View Source​
Declaration
protected virtual void Dispose(bool disposing)
Parameters​
TypeNameDescription
System.BooleandisposingIs disposing

Implements​

  • Meadow.Peripherals.Speakers.IToneGenerator
  • System.IDisposable