Remarks

PiezoSpeaker
Status Status badge: working
Source code GitHub
NuGet package NuGet 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

Characteristic Locus
Inheritance object > PiezoSpeaker
Implements IToneGenerator IDisposable
Inherited Members object.Equals(object) object.Equals(object, object) object.GetHashCode() object.GetType() object.MemberwiseClone() object.ReferenceEquals(object, object) object.ToString()
Namespace Meadow.Foundation.Audio
Assembly Meadow.Foundation.dll

Syntax

public class PiezoSpeaker : IToneGenerator, IDisposable

Constructors

PiezoSpeaker(IPin)

Create a new PiezoSpeaker instance

Declaration
public PiezoSpeaker(IPin pin)

Parameters

Type Name Description
IPin pin

PWM Pin connected to the PiezoSpeaker

Remarks

PiezoSpeaker
Status Status badge: working
Source code GitHub
NuGet package NuGet 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

PiezoSpeaker(IPin, Frequency, float)

Create a new PiezoSpeaker instance

Declaration
public PiezoSpeaker(IPin pin, Frequency frequency, float dutyCycle = 0)

Parameters

Type Name Description
IPin pin

PWM Pin connected to the PiezoSpeaker

Frequency frequency

PWM frequency

float dutyCycle

Duty cycle

Remarks

PiezoSpeaker
Status Status badge: working
Source code GitHub
NuGet package NuGet 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

PiezoSpeaker(IPwmPort)

Create a new PiezoSpeaker instance

Declaration
public PiezoSpeaker(IPwmPort port)

Parameters

Type Name Description
IPwmPort port

Remarks

PiezoSpeaker
Status Status badge: working
Source code GitHub
NuGet package NuGet 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

Properties

IsDisposed

Is the object disposed

Declaration
public bool IsDisposed { get; }

Property Value

Type Description
bool

Remarks

PiezoSpeaker
Status Status badge: working
Source code GitHub
NuGet package NuGet 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

Port

Gets the port that is driving the Piezo Speaker

Declaration
protected IPwmPort Port { get; set; }

Property Value

Type Description
IPwmPort

Remarks

PiezoSpeaker
Status Status badge: working
Source code GitHub
NuGet package NuGet 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

Volume

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

Declaration
public float Volume { get; protected set; }

Property Value

Type Description
float

Remarks

PiezoSpeaker
Status Status badge: working
Source code GitHub
NuGet package NuGet 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

Methods

Dispose()

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

Declaration
public void Dispose()

Remarks

PiezoSpeaker
Status Status badge: working
Source code GitHub
NuGet package NuGet 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

Dispose(bool)

Dispose of the object

Declaration
protected virtual void Dispose(bool disposing)

Parameters

Type Name Description
bool disposing

Is disposing

Remarks

PiezoSpeaker
Status Status badge: working
Source code GitHub
NuGet package NuGet 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

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

Remarks

PiezoSpeaker
Status Status badge: working
Source code GitHub
NuGet package NuGet 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

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 duration is 0, tone plays indefinitely

Returns

Type Description
Task

Remarks

PiezoSpeaker
Status Status badge: working
Source code GitHub
NuGet package NuGet 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

SetVolume(float)

Set the playback volume

Declaration
public void SetVolume(float volume)

Parameters

Type Name Description
float volume

The volume from 0 (off) to 1 (max volume)

Remarks

PiezoSpeaker
Status Status badge: working
Source code GitHub
NuGet package NuGet 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

StopTone()

Stops a tone playing

Declaration
public void StopTone()

Remarks

PiezoSpeaker
Status Status badge: working
Source code GitHub
NuGet package NuGet 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