HDTV Antenna
& Tuner Box
EPROM, MCU
GPS, Receivers
GPRS/GSM/LTE Modules
LCD
RF Modules
Robotics
Voice Modules
Accessories

Credit Card Payment

Follow us on Twitter Facebook

Happy Birthday

Level: beginner

How many times you have heard that beeping sound from your computer, PDA, microwave, and of course every morning you are awaken by the beep from the alarm o'clock. Sometimes it is annonying, most of the time it is a very helpful alert to tell you something is wrong. Most of these beeping sounds are generated by a small device called buzzer, also known as piezo.

A buzzer is an electrical device that is used to make a buzzing sound to attract someone's attention. We will not get into the technical aspect of the buzzer here. You can refer to the InnoBot Instruction Manual for a brief explanation on how Buzzer works.

Simple circuit

The circuit is very simple, only 2 items. First connect the long pin (positive) of the buzzer to pin 2 and the short pin to ground. There are different kind of buzzers with different operating voltage. The one we use, which is included in the InnoBot Kit, has 3v-7.5v. Therefore, we also need to add a resistor to the circuit as shown in the diagram below.

Buzzer Circuit

PART LIST
QtyDescription
1Education Board with Basic Commander
1Buzzer
1Jump wire

All parts are included in the Innobot Kit.

How does it work?

Many people use a buzzer to generate certain sound in a certain frequency. If you combine the sound in its corresponding frequency, you will get a simple melody, like the tone you hear when you dial a phone number. Refer to InnoBot Instruction Manual on how to calculate the frequency of the music note. For illustration purpose, we only list the frequency of one octave on a music scale.


NoteFrequency
(Hz)
Period
(ms)
Half Period
(ms)
Do5231.910.96
Do#5541.80.9
Re5871.70.85
Re#6221.60.8
Mi6591.520.76
Fa6981.430.72
Fa#7401.350.68
Sol7401.350.68
Sol#8311.20.6
La8801.140.57
La#9321.070.54
Ti9881.100.50
Do#10470.960.48

Compose your melody

In order to play a song, you need to know the notes. If you play any musical instrument, you would write your music note on the staff on the music sheet. Here we will use the numeric notation presented by 1-7 to denote the corresponding seven music notes. In our example, our melody is the well known Happy Birthday Song. Using the numeric notation, the first verse would be written as:

     
          1,  1,  2,  1,  4,  3
          C,  C,  D,  C,  F,  E
          DO  DO  RE  DO  FA  ME
     

To generate the sound of each note of the first note. we will use the Sound command. This InnoBasic's built in command takes 3 paramters: Pin#, duration (ms), frequency (Hz), refer to the frequency table described above. To generate the first three mucic notes, the commands would be as follows:

         ' buzzer is on pin 12
         Sound 12,250,523     'Do    1  C
         Sound 12,250,587     'Re    2  D
         Sound 12,250,659     'Mi    3  E
     

Transtating the first verse of the birthday song into Sound command, we would get the following:

     
      Sub Main
         Sound 12,250,523     'Do    1  C
         Sound 12,250,523     'Do    1  C
         Sound 12,250,587     'Re    2  D
         Sound 12,250,523     'Do    1  C
         Sound 12,250,698     'Fa    4  F
         Sound 12,250,659     'Mi    3  E
      End Sub
     
Happy Birthday to you

Let's compile and download the code to the Basic Command while you get the candle ready to play the first verse.

To complete the entire song, you would add one Sound command for each music note. However, if you want to play a long song, it would be a very tedius to have to check which frequency corresponds to which note. To avoid this repetitive task, you can write a subroutine PlayNote() which can be called for each note as it plays. Each note of the melody will be passed as an argument to the subroutine. It checks what is the note and runs the Sound command with the corresponding frequency.

      
      Sub PlayNote(note As Byte)
        Dim freq As Word
        Dim bz As Byte  'Buzzer pin
        Dim dur As Word 'duration of the note
      
        bz = 2
        dur = 300
      
        Select Note
          Case 1
            freq = 523 'Do    1  C 
          Case 2 
            freq = 587 'Re    2  D
          Case 3
            freq = 659 'Mi    3  E
          Case 4
            freq = 698 'Fa    4  F
          Case 5
            freq = 785 'Sol   5  G
          Case 6
            freq = 880 'La    6  A
          Case 7
            freq = 988 'Si    7  B
          Case 8
            freq = 1047 'Do High
          Case 9
            Pause dur 
        End Select
      
        Sound bz, 250, freq  'default duration to 250
     End Sub
     

To make your code simple, you would write the entire song in an Array. For more example on how to use Array, see the Example code from the innoBasi Workshop. At the end of each verse, add the number 9 to generate a pause or silence. You can also change the tempo of a note by modifying the duration (the second argument) of the Sound command.

      
      'Happy Birthday Song
      Dim Song(27) As Byte 
      Song(27) = {1,1,2,1,4,3,9,1,1,2,1,5,4,9,1,1,8,6,4,3,2,9,8,8,6,4,5,4} 
     

Once you have the routine, you would call PlayNote() from the Main() for each note as you would read the music sheet while playing the melody.

      Sub Main()
        Dim x as Byte = 0
        
        Do 
          PlayNote Song(x)
          x += 1       ' increment the index to read each note of the song 
        Loop x > 27 ' length of the song
      End Sub
     
Testing your circuit and code

As always, in case of error, test your code with Debug command. The result will be displayed on the Terminal Window of InnoBasic Workshop. Although this is a simple application, make sure all components are connected correctly before running the program. Also check the pin assignment matches with the one you have set in the program.

What is next?

Can you add some LED and make them blink according to the rhythm of the melody? Share your ideas and comments with us or post your questions on our Forum.

Download: bday.inb


Products
  • Robot Kits
  • Commander Module
  • Commander Boards
  • Development Boards
  • Smart Modules
  • General Modules
  • Accessories
  • InnoBasic
  • catalogue


    Resources
  • DIY Projects
  • ID MAP
  • Identifier List
  • InnoBasic Workshop


  • FAQ | Terms & Conditions | Contact Us

    Copyright © 2017 Kowatec Corporation. All rights reserved.