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.

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

Characteristic Locus
Inheritance object > PiezoSpeaker
Implements IToneGenerator
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

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.

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

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.

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

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.

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

Properties

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.

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

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.

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

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

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.

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

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

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.

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

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.

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

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.

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