nanoFramework.Iot.Device.Buzzer
1.2.673
Prefix Reserved
dotnet add package nanoFramework.Iot.Device.Buzzer --version 1.2.673
NuGet\Install-Package nanoFramework.Iot.Device.Buzzer -Version 1.2.673
<PackageReference Include="nanoFramework.Iot.Device.Buzzer" Version="1.2.673" />
paket add nanoFramework.Iot.Device.Buzzer --version 1.2.673
#r "nuget: nanoFramework.Iot.Device.Buzzer, 1.2.673"
// Install nanoFramework.Iot.Device.Buzzer as a Cake Addin #addin nuget:?package=nanoFramework.Iot.Device.Buzzer&version=1.2.673 // Install nanoFramework.Iot.Device.Buzzer as a Cake Tool #tool nuget:?package=nanoFramework.Iot.Device.Buzzer&version=1.2.673
Buzzer - Piezo Buzzer Controller
This device binding allows playing certain tones using piezo buzzer. It uses PWM with 50% duty cycle and various frequencies.
Piezo buzzers with three pins supported as well as piezo buzzers with two pins.
Device Family
This binding was tested on two types of piezo buzzers. First type of buzzer has two pins vcc and gnd. Second type of buzzers has addition signal pin.
Usage
The Buzzer
class can use either software or hardware PWM. This is done fully transparently by the initialization.
If you want to use the software PWM, you have to call the constructor that takes in one integer: public Buzzer(int pinNumber)
.
To use the hardware PWM, make sure you reference correctly the chip and channel you want to use, and call the constructor that takes two integers (chip and channel).
Also you could explicitly pass a PwmChannel if you want to construct that yourself.
Here's an example how you could use Buzzer
.
using (Buzzer buzzer = new Buzzer(21)); // Initialize buzzer with software PWM connected to pin 21.
{
buzzer.PlayTone(440, 1000); // Play tone with frequency 440 hertz for one second.
}
Important Depending on your platform, you may have to setup the pins properly. In case of ESP32, use the
nanoFramework.Hardware.Esp32
nuget then configure the pin properly likeConfiguration.SetPinFunction(21, DeviceFunction.PWM1);
. That should be done before creating the Buzzer.
Buzzer
allows to play tone for certain duration like in example above.
Or you could start tone playing, perform some operation and then stop tone playing like in a following example.
using (Buzzer buzzer = new Buzzer(21));
{
buzzer.StartPlaying(440);
Thread.Sleep(1000);
buzzer.StopPlaying();
}
The result will be the same as in previous example.
Buzzer
allows you to play only single tone at a single moment. If you will call SetFrequency
sequentially with a different frequencies then the last call will override previous calls. Following example explains it.
using (Buzzer buzzer = new Buzzer(21)); // Initialize buzzer with software PWM connected to pin 21.
{
buzzer.StartPlaying(440);
Thread.Sleep(1000);
buzzer.StartPlaying(880);
Thread.Sleep(1000);
buzzer.StopPlaying();
}
This example will play tone with frequency 440 for a second and then will play tone with a frequency 880 for a second.
Example of Alphabet song played using Buzzer
Schematic
You have 2 types of buzzers. Those with 2 pins only and those with 3 pins. For buzzer with 3 pins: simply connect signal pin of buzzer to commutation pin (any valid GPIO), vcc pin to +5v, gnd pin to ground. For buzzer with 2 pins: connect vcc pin of buzzer to commutation pin (any valid GPIO) and gnd to ground.
You could use any types of buzzers in any order. No changes to code are required.
Code
This sample contains a wrapper on a Buzzer called MelodyPlayer
.
MelodyPlayer and MelodyElement
To create an instance of a MelodyPlayer use following line:
MelodyPlayer player = new MelodyPlayer(new Buzzer(26));
Constructor takes a single parameter type of Buzzer
.
After initialization MelodyPlayer allows playing melody represented by sequence of MelodyElement
objects.
MelodyElement is a base class for two types of elements:
NoteElement
- It will be played. So in a constructor it acceptsNote
andOctave
to determine frequency of the sound andDuration
to determine duration of the sound.PauseElement
- It's supposed to make a pause between two NoteElements so it's only have duration of pause as constructor parameter.
How to use
Following example demonstrates how to create MelodyElement sequence and how to play it using MelodyPlayer:
IListMelodyElement sequence = new ListMelodyElement()
{
new NoteElement(Note.C, Octave.Fourth, Duration.Quarter),
new PauseElement(Duration.Quarter),
new NoteElement(Note.C, Octave.Fourth, Duration.Quarter)
};
using (var player = new MelodyPlayer(new Buzzer(21)))
{
player.Play(sequence, 100);
}
Play
method MelodyPlayer accepts a sequence of MelodyElements as the first parameter and a tempo as the second.
Tempo is an amount of quarter notes per minute. So the more tempo is the quicker melody will be played.
Also there is an overload of MelodyPlayer.Play
with 3 parameters: MelodyElement sequence, tempo and transposition value. Transposition increases or decreases every tone of melody sequence by desired amount of semitones. For example: following line will decrease every tone of sequence by one octave since octave consists of 12 semitones.
player.Play(sequence, 100, -12);
Parallel buzzer playing
As far as MelodyPlayer.Play
method is not asynchronous, calls of this method are wrapped by task like this:
var player1 = new MelodyPlayer(new Buzzer(21));
player1.Play(AlphabetSong, 100, -12);
player1.Play(AlphabetSong, 100);
This approach allows playing two melodies independently however example above plays a single melody in the same time using two different buzzers.
Alphabet song
Presented sample plays Alphabet song using two buzzers. The song is hardcoded in a Buzzer.Sample.cs
file as a sequence of MelodyElements. Read more about Alphabet song on Wikipedia
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net is compatible. |
-
- nanoFramework.CoreLibrary (>= 1.15.5)
- nanoFramework.System.Device.Model (>= 1.2.670)
- nanoFramework.System.Device.Pwm (>= 1.1.10)
- nanoFramework.System.Math (>= 1.5.43)
- UnitsNet.nanoFramework.Frequency (>= 5.60.0)
NuGet packages (6)
Showing the top 5 NuGet packages that depend on nanoFramework.Iot.Device.Buzzer:
Package | Downloads |
---|---|
nanoFramework.M5Core
This package includes the nanoFramework.M5Core assembly for .NET nanoFramework C# projects. |
|
nanoFramework.M5StickCPlus
This package includes the nanoFramework.M5StickCPlus assembly for .NET nanoFramework C# projects. |
|
nanoFramework.Fire
This package includes the nanoFramework.Fire assembly for .NET nanoFramework C# projects. |
|
nanoFramework.MagicBit
This package includes nanoFramework.MagicBit, a board package library for MagicBit in .NET nanoFramework C# projects. |
|
nanoFramework.M5Stack
This package includes the nanoFramework.M5Stack assembly for .NET nanoFramework C# projects. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.2.673 | 259 | 10/23/2024 |
1.2.656 | 668 | 10/3/2024 |
1.2.639 | 486 | 9/6/2024 |
1.2.631 | 300 | 8/28/2024 |
1.2.613 | 420 | 8/9/2024 |
1.2.601 | 215 | 7/26/2024 |
1.2.590 | 387 | 7/17/2024 |
1.2.573 | 587 | 6/19/2024 |
1.2.570 | 133 | 6/14/2024 |
1.2.536 | 800 | 4/15/2024 |
1.2.514 | 589 | 3/22/2024 |
1.2.494 | 385 | 2/28/2024 |
1.2.474 | 527 | 1/24/2024 |
1.2.462 | 177 | 1/5/2024 |
1.2.458 | 165 | 12/20/2023 |
1.2.436 | 466 | 11/10/2023 |
1.2.416 | 401 | 11/8/2023 |
1.2.403 | 404 | 10/6/2023 |
1.2.396 | 388 | 9/27/2023 |
1.2.384 | 411 | 9/6/2023 |
1.2.378 | 503 | 8/16/2023 |
1.2.369 | 446 | 8/2/2023 |
1.2.363 | 342 | 7/28/2023 |
1.2.357 | 446 | 7/19/2023 |
1.2.354 | 370 | 7/14/2023 |
1.2.345 | 459 | 6/21/2023 |
1.2.341 | 384 | 6/14/2023 |
1.2.337 | 519 | 6/7/2023 |
1.2.335 | 244 | 6/2/2023 |
1.2.329 | 395 | 5/26/2023 |
1.2.313 | 729 | 5/12/2023 |
1.2.302 | 519 | 5/10/2023 |
1.2.297 | 469 | 5/3/2023 |
1.2.273 | 1,882 | 3/17/2023 |
1.2.267 | 1,168 | 3/10/2023 |
1.2.263 | 858 | 3/8/2023 |
1.2.259 | 832 | 2/27/2023 |
1.2.256 | 868 | 2/24/2023 |
1.2.253 | 850 | 2/22/2023 |
1.2.222 | 2,156 | 1/9/2023 |
1.2.217 | 1,275 | 1/6/2023 |
1.2.208 | 1,319 | 1/3/2023 |
1.2.203 | 880 | 12/28/2022 |
1.2.159 | 1,553 | 11/14/2022 |
1.2.153 | 2,839 | 11/5/2022 |
1.2.141 | 4,399 | 10/25/2022 |
1.2.128 | 1,294 | 10/22/2022 |
1.2.87 | 10,108 | 9/15/2022 |
1.2.63 | 4,077 | 9/3/2022 |
1.2.47 | 1,731 | 8/15/2022 |
1.2.40 | 2,004 | 8/6/2022 |
1.2.38 | 1,639 | 8/5/2022 |
1.2.32 | 3,747 | 8/2/2022 |
1.2.28 | 1,585 | 8/1/2022 |
1.2.13 | 3,189 | 7/24/2022 |
1.2.10 | 1,594 | 7/23/2022 |
1.1.145.58726 | 3,168 | 7/7/2022 |
1.1.133.52556 | 5,045 | 6/30/2022 |
1.1.121.35854 | 3,912 | 6/26/2022 |
1.1.116.8772 | 2,822 | 6/24/2022 |
1.1.113.2032 | 780 | 6/23/2022 |
1.1.102.51394 | 2,211 | 6/15/2022 |
1.1.99.36719 | 1,654 | 6/14/2022 |
1.1.72.29765 | 7,824 | 5/31/2022 |
1.1.64.21380 | 3,318 | 5/26/2022 |
1.1.58.10097 | 2,186 | 5/23/2022 |
1.1.54.28879 | 1,065 | 5/23/2022 |
1.1.40 | 3,836 | 5/5/2022 |
1.1.3 | 12,282 | 4/15/2022 |
1.1.1 | 504 | 4/14/2022 |
1.0.302 | 5,162 | 3/31/2022 |
1.0.300 | 490 | 3/31/2022 |
1.0.288-preview.114 | 160 | 3/25/2022 |
1.0.288-preview.113 | 130 | 3/25/2022 |
1.0.288-preview.103 | 151 | 3/21/2022 |
1.0.288-preview.100 | 137 | 3/19/2022 |
1.0.288-preview.99 | 144 | 3/18/2022 |
1.0.288-preview.98 | 127 | 3/18/2022 |
1.0.288-preview.94 | 182 | 3/15/2022 |
1.0.288-preview.93 | 130 | 3/15/2022 |
1.0.288-preview.90 | 182 | 3/11/2022 |
1.0.288-preview.86 | 163 | 3/8/2022 |
1.0.288-preview.77 | 185 | 2/27/2022 |
1.0.288-preview.75 | 138 | 2/26/2022 |
1.0.288-preview.65 | 165 | 2/18/2022 |
1.0.288-preview.63 | 151 | 2/16/2022 |
1.0.288-preview.61 | 148 | 2/12/2022 |
1.0.288-preview.58 | 144 | 2/10/2022 |
1.0.288-preview.53 | 139 | 2/9/2022 |
1.0.288-preview.48 | 171 | 2/4/2022 |
1.0.288-preview.41 | 171 | 1/31/2022 |
1.0.288-preview.29 | 152 | 1/28/2022 |
1.0.288-preview.22 | 172 | 1/27/2022 |
1.0.288-preview.20 | 146 | 1/27/2022 |
1.0.288-preview.19 | 140 | 1/27/2022 |
1.0.288-preview.18 | 146 | 1/27/2022 |
1.0.288-preview.5 | 158 | 1/24/2022 |
1.0.288-preview.1 | 156 | 1/21/2022 |
1.0.272 | 583 | 1/10/2022 |
1.0.263 | 390 | 12/24/2021 |
1.0.260 | 524 | 12/10/2021 |
1.0.259 | 175 | 12/9/2021 |
1.0.258 | 186 | 12/7/2021 |
1.0.218 | 360 | 10/18/2021 |
1.0.191 | 184 | 9/29/2021 |
1.0.179 | 174 | 9/22/2021 |