Spanish Italian
17454 Users    

Howto Make Music with a Microcontroller

  Download PDF version of the Article

Hey there. I am gonna show you how to use your Nerdkit microcontroller kit to make music. Beyond the base kit, we are gonna use a Piezo electric element which is a device that bends in response to voltage. By changing the voltage quickly, we can make the disk oscillate along with the air around it. These pressure waves in the air are sensed by our ears as sound.

First, a look at the Piezo element. It has two wires and behaves electrically like a capacitor. You can get one of these from Radio Shack for about 2 dollars or online for less. Just be sure it’s the kind that requires a driving circuit. You don’t want one with a full buzzer circuit which will not let you change sound.

If you already followed along with the Nerdkit guide, you’ve got most of the circuit in place. Now I put one wire from the Piezo element into the microcontroller and the other into ground. I am using the microcontrollers PA1 which is pin number 19 on the chip. This is on the right side on row number 12 on the bread board. Just to review, remember that the pin numbers go counter clockwise around the chip. Starts at one in the top left position, comes down to 10 along the left side, starts at eleven in the bottom right and counts up 20 along the right side.

Next, I wired the push button switch into the microcontroller’s PA7 which is pin number 11 on the chip. If you look carefully, you see that the switch has three terminals labeled C, NO and NC. These stand for common, normally open and normally closed. The button connects C to NO when it’s pressed and the rest of the time, connects C to NC. We’ll use the microcontroller’s internal pull up resistor, which we talked about in the Nerdkit guide. So, we just have to use the button to pull the voltage down to ground.

Every wave has an associated property called its frequency which measures how many cycles happen every second. This thing, cycles per second is also called Hertz. The middle C on a piano has a frequency of 262 Hertz which means the air oscillates back and forth 262 times every second. Now, another property of a wave is period which is just 1 divided by the frequency. And this measure is the length in time of one entire cycle of the wave. So, for that middle C on the piano, the cycle repeats every 3.8 milli seconds.

Now, while a normal play tone is a sine wave, we can much more easily make a square wave with the microcontroller by turning the pin on, waiting for certain amount of time and then turning the pin off and waiting again. A square wave sounds a bit different than the sine wave. And this is because of something engineers and musicians call harmonics or over turns.

I’ll demonstrate both using function generator which is a piece of wired equipment that generates a voltage signal. First, a sine wave. Next, a square wave. Could you hear the difference?
I have written a play tone function which takes two parameters, the half period of the tone and the duration of the note. It’s pretty clear that the length of the two delays should each be equal to half of the period. This function calculates the number of cycles to do and then switches the output pin to play the note. Then using a website which converts musical notes to frequencies, I made a series of ‘define’ statements which give the appropriate names to those half period values. Finally, in the main function, I transcribe the song into source code specifying the tone and duration of each note. I also made use of the LCD to let it help sing along.

And now here is the working system. That’s all for now. Hopefully you learned something about making square waves with delays and making sound from voltage. If you like to download the source code for this project and see more videos like this one, take a look at the videos page on

www.nerdkits.com.

Thanks, great article :-)

Thanks, great article :-)

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
2 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.

Who's new

  • fernand
  • Ligrock
  • paolo_0665
  • chanuei
  • JM
  • samsilva77
  • araghube
  • stoll
  • mt
  • orionkw

Who's online

There are currently 0 users and 61 guests online.