Meadow.Tip – Playing Jingle Bells with a Piezo Speaker Using C#

I was lucky enough to be one of the early backers of the new Meadow board series from Wilderness labs and have since received my board as well as Hack Kit Pro! In this series, I explore and share some of the things I built for fun or for real. Check out wilderness labs here: https://www.wildernesslabs.co/

Playing Jingle bells

One of the basic samples of using the Meadow.Foundation SDK was playing a single note on repeat using a Piezo speaker. Peep that here: https://github.com/WildernessLabs/Meadow.Foundation/tree/master/Source/Samples/Speakers/PiezoSpeaker_Sample

Where the main class that implements the communication to the speaker is:

public class PiezoSpeakerApp : App<F7Micro, PiezoSpeakerApp>
{
    readonly IDigitalOutputPort port;
    readonly IPwmPort pwm; 
    readonly PiezoSpeaker piezoSpeaker;

    public PiezoSpeakerApp()
    {
        pwm = Device.CreatePwmPort(Device.Pins.D05);
        piezoSpeaker = new PiezoSpeaker(pwm);

        TestPiezoSpeaker();
    }

    protected void TestPiezoSpeaker()
    {
        while (true)
        {
            Console.WriteLine("Playing A4 note!");
            piezoSpeaker.PlayTone(440, 1000);
            piezoSpeaker.StopTone();
            Thread.Sleep(500);
        }
    }
}

The application builds a reference to the Piezo speaker through the Digital 05 port and ground, then proceeds to blast the A4 note on repeat until your ears bleed. This is a nice tool to use to make sure your circuit is setup properly, but I wasn’t satisfied…

As a musician, I’m always on the hunt for pleasant notes, little riffs, chords, etc. So naturally, I started looking up some note frequencies I didn’t know off the top of my head and stringing them together.

One of the first songs guitarists learn when first picking at strings and learning the fretboard is Jingle Bells! So what a great way to teach myself how to develop basic melodies using a single output speaker and my brand new Meadow F7 Micro! With that in mind, you can find the entire source for the app here: https://github.com/SuavePirate/Meadow-Piezo-JingleBells

And here’s the guts of the main jingle bells class for the meadow app:

JingleBellsApp.cs

public class JingleBellsApp : App<F7Micro, JingleBellsApp>
{
    readonly PiezoSpeaker piezoSpeaker;

    const int LENGTH = 26;
    const string NOTES = "eeeeeeegcde fffffeeeeddedg";
    readonly int[] BEATS = { 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 };
    const int TEMPO = 300;

    public JingleBellsApp()
    {
        piezoSpeaker = new PiezoSpeaker(Device.CreatePwmPort(Device.Pins.D05));

        PlayJingleBells();
    }

    protected void PlayJingleBells()
    {
        while (true)
        {
            Console.WriteLine("Playing jingle bells!");
            for (int i = 0; i < LENGTH; i++)
            {
                if (NOTES[i] == ' ')
                {
                    Thread.Sleep(BEATS[i] * TEMPO); // rest
                }
                else
                {
                    PlayNote(NOTES[i], BEATS[i] * TEMPO);
                }

                // pause between notes
                Thread.Sleep(TEMPO / 2);
            }
            Console.WriteLine("From the top!");
        }
    }
    void PlayNote(char note, int duration)
    {
        char[] names = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
        int[] tones = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

        // play the tone corresponding to the note name
        for (int i = 0; i < names.Length; i++)
        {
            if (names[i] == note)
            {
                piezoSpeaker.PlayTone(tones[i], duration);
            }
        }
    }
}

I wanted to make the song more configurable, so I broke the melody out into the notes as a string, and the beats for the song to go along with it. You can also play with the TEMPO to speed things up or slow them down. I also built a private PlayNote method that takes in the major note character and the duration and executes the MeadowFoundation piezoSpeaker.PlayTone to execute the tone on the speaker that was initialized earlier in the contstructor.

So with all these things together, the PlayJingleBellsMethod() takes those constant values and the initialized piezoSpeaker and infinitely loops the entire song. Within the while loop, we use a for loop to play the number of beats that we registered in the LENGTH const and proceed to pull out the beats and notes from the arrays above to string the song together.

When setting up the circuit for your Piezo speaker, just make sure you have the black wire to ground on your board, and red to the port you registered in your constructor (in this case D05). Here’s a fritzing diagram of a simple setup for it:

jinglebells_spiezo_bb

That’s all there is to it! Stay tuned for more fun projects and how to setup them up as I keep working through how to build some different fun things with my new Meadow board!


If you like what you see, don’t forget to follow me on twitter @Suave_Pirate, check out my GitHub, and subscribe to my blog to learn more mobile and AI developer tips and tricks!

Interested in sponsoring developer content? Message @Suave_Pirate on twitter for details.


voicify_logo
I’m the Director and Principal Architect over at Voicify. Learn how you can use the Voice Experience Platform to bring your brand into the world of voice on Alexa, Google Assistant, Cortana, chat bots, and more: https://voicify.com/


Leave a comment