{"id":3967,"date":"2011-11-03T18:54:02","date_gmt":"2011-11-03T22:54:02","guid":{"rendered":"http:\/\/embedded-lab.com\/blog\/?p=3967"},"modified":"2012-11-06T13:55:20","modified_gmt":"2012-11-06T18:55:20","slug":"lab-19-play-musical-notes","status":"publish","type":"post","link":"https:\/\/embedded-lab.com\/blog\/lab-19-play-musical-notes\/","title":{"rendered":"Lab 19: Play musical notes"},"content":{"rendered":"<p>We have discussed in the past experiments how to use a PIC microcontroller to do a variety of things from flashing an LED to driving a motor, etc. Today, we will see how to play notes of a song with a PIC microcontroller. Musical notes are simply sound waves of particular frequencies. If the frequency of a note is known correctly, a microcontroller can be programmed to play the note by generating a square wave (of the same frequency) signal at one of its I\/O pins. The signal must be fed to a speaker to listen to the sound. Here, we will discuss playing notes of the popular &#8220;Happy birthday to you&#8221; tune using a PIC16F628A microcontroller and a buzzer.<\/p>\n<div id=\"attachment_3968\" style=\"width: 570px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/11\/MusicNoteTitle.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3968\" class=\"size-full wp-image-3968\" title=\"MusicNoteTitle\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/11\/MusicNoteTitle.jpg\" alt=\"\" width=\"560\" height=\"361\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/11\/MusicNoteTitle.jpg 560w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/11\/MusicNoteTitle-550x354.jpg 550w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><\/a><p id=\"caption-attachment-3968\" class=\"wp-caption-text\">Musical notes using a PIC micro<\/p><\/div>\n<p><!--more--><\/p>\n<p><strong>Theory<\/strong><\/p>\n<p>In order to play the tune of a song, you need to know its musical notes. Each note is played for a certain duration and there is a certain time gap between two successive notes. The table below provides frequencies of musical notes starting from middle C. The middle C is designated as C4 because it is the fourth C key on a standard 88-key piano keyboard.<\/p>\n<div id=\"attachment_3981\" style=\"width: 585px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/11\/MiddleNotes.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3981\" class=\"size-full wp-image-3981\" title=\"MiddleNotes\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/11\/MiddleNotes.png\" alt=\"\" width=\"575\" height=\"80\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/11\/MiddleNotes.png 575w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/11\/MiddleNotes-550x76.png 550w\" sizes=\"auto, (max-width: 575px) 100vw, 575px\" \/><\/a><p id=\"caption-attachment-3981\" class=\"wp-caption-text\">Musical notes and their frequencies starting from Middle C<\/p><\/div>\n<p>The notes of the other octaves can be obtained by multiplying or dividing these frequencies by 2. For example, the next C note above the middle C would have a frequency of 524 Hz. You can find the frequencies of rest of the notes at the following link:\u00a0<a href=\"http:\/\/cs.nyu.edu\/courses\/fall03\/V22.0201-003\/notes.htm\" target=\"_blank\">http:\/\/cs.nyu.edu\/courses\/fall03\/V22.0201-003\/notes.htm<\/a><\/p>\n<p>Musical notes can be generated using square waves of the note frequencies. So, in order to play the tune of a song with a microcontroller, all you need to know are the musical notes and their timing information; rest is all programming. A square wave can be generated at an I\/O pin of PIC microcontroller by bit-banging the pin high and low. An alternative way of achieving this is using a hardware PWM module. Here, we will be using the former one.<\/p>\n<p><strong>Circuit diagram<\/strong><\/p>\n<p>The circuit diagram for this experiment is pretty simple. The RB0 pin of the PIC16F628A microcontroller is bit-banged to generate a square wave tone of desired frequency. An I\/O pin of PIC16F628A can source current up to 25 mA, which may not be sufficient to drive an electric buzzer directly. Therefore, a BC547 NPN transistor is used as a current amplifier for the buzzer. An RC filtering may be used to improve the quality of audio (square waves are not pure waves and so they don&#8217;t sound as good as sine waves), but for simplicity, it is not implemented here.<\/p>\n<div id=\"attachment_4085\" style=\"width: 523px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/11\/PlayNotesCkt.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4085\" class=\"size-full wp-image-4085\" title=\"PlayNotesCkt\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/11\/PlayNotesCkt.png\" alt=\"\" width=\"513\" height=\"330\" \/><\/a><p id=\"caption-attachment-4085\" class=\"wp-caption-text\">Electrical coil buzzer is driven through RB0 pin using a transistor<\/p><\/div>\n<div id=\"attachment_3969\" style=\"width: 570px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/11\/MusicNoteCktSetUp.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3969\" class=\"size-full wp-image-3969\" title=\"MusicNoteCktSetUp\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/11\/MusicNoteCktSetUp.jpg\" alt=\"\" width=\"560\" height=\"435\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/11\/MusicNoteCktSetUp.jpg 560w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/11\/MusicNoteCktSetUp-550x427.jpg 550w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><\/a><p id=\"caption-attachment-3969\" class=\"wp-caption-text\">Circuit setup on breadboard<\/p><\/div>\n<p>You can see I am using my <a href=\"http:\/\/embedded-lab.com\/blog\/?p=1034\" target=\"_blank\"><strong>PIC16F628A breadboard module<\/strong><\/a> in this experiment.<\/p>\n<p><strong>Software<\/strong><\/p>\n<p>Generating audio tones is very easy in mikroC Pro for PIC compiler. It has a built-in Sound Library for serving this purpose. The library has following two functions:<\/p>\n<p>Sound_Init(<strong>char<\/strong> *snd_port, <strong>char<\/strong> snd_pin): Configures the appropriate MCU pin for sound generation. For example, <em>Sound_Init(&amp;PORTB,0)<\/em> will configure RB0 pin for the sound output.<\/p>\n<p><strong><\/strong>Sound_Play(<strong>unsigned<\/strong> freq_in_hz, <strong>unsigned<\/strong> duration_ms): Generates a square wave signal on the appropriate pin.<\/p>\n<p>The note frequencies of a song can be either defined as a variable array or saved as a constant array in ROM (if RAM size is not sufficient) of the microcontroller. The notes of the song &#8220;Happy birthday to you&#8221; are not too big and therefore, can be defined in an integer-type array in mikroC, as:<\/p>\n\n<div class=\"wp_syntax\"><table><tr><td class=\"code\"><pre class=\"c\" style=\"font-family:monospace;\"><span style=\"color: #808080; font-style: italic;\">\/*\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Hap  py\u00a0 Birth Day\u00a0 to\u00a0 you,\u00a0 Hap py\u00a0  birth day\u00a0 to\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 C4\u00a0\u00a0 C4\u00a0\u00a0 D4\u00a0\u00a0 C4\u00a0\u00a0 F4\u00a0\u00a0 E4\u00a0\u00a0 C4\u00a0\u00a0 C4\u00a0\u00a0 D4\u00a0\u00a0 C4\u00a0\u00a0 G4 *\/<\/span>\n<span style=\"color: #993333;\">unsigned<\/span> <span style=\"color: #993333;\">int<\/span> notes<span style=\"color: #009900;\">&#91;<\/span><span style=\"color: #009900;\">&#93;<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #009900;\">&#123;<\/span> <span style=\"color: #0000dd;\">262<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">262<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">294<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">262<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">349<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">330<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">262<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">262<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">294<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">262<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">392<\/span><span style=\"color: #339933;\">,<\/span>\n&nbsp;\n<span style=\"color: #808080; font-style: italic;\">\/*\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 you, Hap py\u00a0 Birth Day\u00a0 dear\u00a0 xxxx\u00a0\u00a0\u00a0\u00a0\u00a0 Hap\u00a0 py\u00a0\u00a0 birth\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 F4\u00a0\u00a0 C4\u00a0\u00a0 C4\u00a0\u00a0 C5\u00a0\u00a0 A4\u00a0\u00a0 F4\u00a0\u00a0 E4\u00a0\u00a0 D4\u00a0\u00a0 B4b\u00a0 B4b\u00a0 A4 *\/<\/span>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0  <span style=\"color: #0000dd;\">349<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">262<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">262<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">523<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">440<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">349<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">330<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">294<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">466<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">466<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">440<\/span><span style=\"color: #339933;\">,<\/span>\n&nbsp;\n<span style=\"color: #808080; font-style: italic;\">\/*\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 day\u00a0 to\u00a0 you\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 F4\u00a0\u00a0 G4\u00a0\u00a0 F4\u00a0\u00a0 *\/<\/span>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0  <span style=\"color: #0000dd;\">349<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">392<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">349<\/span>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: #009900;\">&#125;<\/span><span style=\"color: #339933;\">;<\/span><\/pre><\/td><\/tr><\/table><\/div>\n\n<p>Similarly, the duration of each note frequency can also be defined in another integer-type array of the same size. The complete program (source + HEX files) can be downloaded from the following link.<\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/uploads\/HexFiles\/PIC16F628A\/MusicTune.zip\">Download mikroC source and HEX files<\/a><\/p>\n<p>Here&#8217;s a video showing the &#8220;Happy birthday to you&#8221; tune being played by the PIC16F628A microcontroller. You can tweak the time durations of notes little bit to sound it better.<\/p>\n<p><center><br \/>\n<iframe loading=\"lazy\" src=\"http:\/\/www.youtube.com\/embed\/RQvdkvQiscQ\" frameborder=\"0\" width=\"420\" height=\"315\"><\/iframe><\/center>&nbsp;<\/p>\n<p>Although bit-banging an I\/O pin seems to be a very easy way of generating sound, the microcontroller might be substantially occupied while the tone is active. Read this <a href=\"http:\/\/www.uchobby.com\/index.php\/2007\/11\/11\/arduino-sound-part-1\/\" target=\"_blank\"><strong>article<\/strong><\/a> to find out alternative ways of producing sound with a microcontroller.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We have discussed in the past experiments how to use a PIC microcontroller to do a variety of things from flashing an LED to driving a motor, etc. Today, we will see how to play notes of a song with a PIC microcontroller. Musical notes are simply sound waves of particular frequencies. If the frequency of a note is known correctly, a microcontroller can be programmed to play the note by generating a square wave (of the same frequency) signal at one of its I\/O pins. The signal must be fed to a speaker to listen to the sound. Here,<\/p>\n","protected":false},"author":1,"featured_media":3968,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,38],"tags":[369,9,368,96],"class_list":["post-3967","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded-labs","category-pic-tutorials","tag-creating-sound-with-a-microcontroller","tag-mikroc","tag-musical-notes","tag-pic16f628a"],"_links":{"self":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/3967","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/comments?post=3967"}],"version-history":[{"count":25,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/3967\/revisions"}],"predecessor-version":[{"id":4086,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/3967\/revisions\/4086"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media\/3968"}],"wp:attachment":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media?parent=3967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/categories?post=3967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/tags?post=3967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}