Netduino – 3. Making sounds

(A post I drafted a while ago. Thought I’d better update it!)

A piezo speaker or buzzer of the sort found in small toys and birthday cards can be powered and controlled by the netduino. They can be bought separately very cheaply. You can rip one out and connect it to an output port in series with a resistor.  A variable resistor (potentiometer) is ideal as you can use it as a volume control but for this example a 1k resistor is used.

Piezo speakers run with a small digital input and so are ideal for taking power from the netduino digital ports. By switching the current on and off very fast, the speaker vibrates. The sound produced by the speaker depends on the frequency the output is switched on and off. This is controlled with something called Pulse Width Modulation (PWM).  In our case, the value of the resistor determines the volume.

You will need to connect the speaker to the netduino. In order to create the circuit without soldering, you can use a small breadboard for this along with some leads. You can buy the leads themselves or make your own from an old cat5 cable sliced up. For this project, the negative lead (black) will run to ground terminal on the netduino and the positive lead (red)  runs in series with the resistor to a digital port with Pulse Width Modulation (PWM) enabled. These are ports x,x,x. I used digital port 5.


Now some code to make a small sound. The code for playing frequencies is contained within the speaker class. The main program just plays a scale in a loop until we press the button to stop it.

On the netduino forums there is an example of this setup playing a variety of tunes through a piezo speaker. There’s some nicely written classes there for defining notes and frequencies and also a few recognisable tunes. You would need to piece together some of the code in the thread to make the example work.

Video tutorial here:


I have pushed the code for this on github and implemented the missing classes:

Also changed the class below so it calculates the frequencies for the notes instead of hardcoding them into an Dictionary as in the original post

    using System;
    using Microsoft.SPOT;

    public static class NoteFrequencies
        /// <summary>
        /// Static method to calculate the frequency of a note from its name and octave.
        /// </summary>
        /// <param name="octave">Octave number (0-7)</param>
        /// <param name="note">Note to be found: eg. F#, A, Bb</param>
        /// <returns></returns>
        public static float CalculateFrequency(int octave, string note)
            string noteLC = note.ToLower();
            string[] notes = "c,c#,d,d#,e,f,f#,g,g#,a,a#,b".Split(',');

            // loop through each note until we find the index of the one we want        
            for (int n = 0; n < notes.Length; n++)
                if (notes[n] == noteLC // frequency found for major and sharp notes
                    || (note.Length > 1 && noteLC[1] == 'b' && notes[n + 1][0] == noteLC[0])) // or flat of next note
                    // Multiply initial note by 2 to the power (n / 12) to get correct frequency, 
                    //  (where n is the number of notes above the first note). 
                    //  Then mutiply that value by 2 to go up each octave
                    return (16.35f * (float)System.Math.Pow(2, (n / 12)))
                        * (float)System.Math.Pow(2, octave);
            throw new ArgumentException("No frequency found for note : " + note, note);

About octoberclub

.Net developer based in Brighton, UK. Interested in software, science, local politics and environment
This entry was posted in Netduino. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s