Remarks
PiezoSpeaker | |
---|---|
Status | |
Source code | GitHub |
NuGet package |
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.
Code Example
protected PiezoSpeaker piezoSpeaker;
public override Task Initialize()
{
Console.WriteLine("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++)
{
Console.WriteLine("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
Characteristic | Locus |
---|---|
Inheritance | System.Object > PiezoSpeaker |
Namespace | Meadow.Foundation.Audio |
Assembly | Meadow.Foundation.dll |
Syntax
public class PiezoSpeaker : IToneGenerator
Constructors
PiezoSpeaker(IPwmOutputController, IPin)
Create a new PiezoSpeaker instance
Declaration
public PiezoSpeaker(IPwmOutputController device, IPin pin)
Parameters
Type | Name | Description |
---|---|---|
IPwmOutputController | device | IPwmOutputController to create PWM port |
IPin | pin | PWM Pin connected to the PiezoSpeaker |
PiezoSpeaker(IPwmOutputController, IPin, Frequency, Single)
Create a new PiezoSpeaker instance
Declaration
public PiezoSpeaker(IPwmOutputController device, IPin pin, Frequency frequency, float dutyCycle = null)
Parameters
Type | Name | Description |
---|---|---|
IPwmOutputController | device | IPwmOutputController to create PWM port |
IPin | pin | PWM Pin connected to the PiezoSpeaker |
Frequency | frequency | PWM frequency |
System.Single | dutyCycle | Duty cycle |
PiezoSpeaker(IPwmPort)
Create a new PiezoSpeaker instance
Declaration
public PiezoSpeaker(IPwmPort port)
Parameters
Type | Name | Description |
---|---|---|
IPwmPort | port |
Properties
Port
Gets the port that is driving the Piezo Speaker
Declaration
protected IPwmPort Port { get; set; }
Property Value
Type | Description |
---|---|
IPwmPort |
Methods
PlayTone(Frequency)
Play a frequency until stopped by StopTone
Declaration
public Task PlayTone(Frequency frequency)
Parameters
Type | Name | Description |
---|---|---|
Frequency | frequency | The frequency in hertz of the tone to be played |
Returns
Type | Description |
---|---|
Task |
PlayTone(Frequency, TimeSpan)
Play a frequency for a specified duration
Declaration
public Task PlayTone(Frequency frequency, TimeSpan duration)
Parameters
Type | Name | Description |
---|---|---|
Frequency | frequency | The frequency in hertz of the tone to be played |
TimeSpan | duration | How long the note is played in milliseconds, if durration is 0, tone plays indefinitely |
Returns
Type | Description |
---|---|
Task |
StopTone()
Stops a tone playing
Declaration
public void StopTone()