{"id":6439,"date":"2012-11-25T14:34:55","date_gmt":"2012-11-25T19:34:55","guid":{"rendered":"http:\/\/embedded-lab.com\/blog\/?p=6439"},"modified":"2013-02-03T16:47:57","modified_gmt":"2013-02-03T21:47:57","slug":"making-a-simple-clap-switch","status":"publish","type":"post","link":"https:\/\/embedded-lab.com\/blog\/making-a-simple-clap-switch\/","title":{"rendered":"Making a simple clap switch"},"content":{"rendered":"<p>A clap switch is a fun project for beginners. It switches on and off electrical appliances with a sound of clapping hands. Today we will discuss about making a simple clap switch that operates when it detects two clapping sounds in a row. It uses an electret microphone as a transducer for converting a clapping sound into an electrical signal. The microcphone output is amplified by a transistor and is then sent to the PIC12F683 microcontroller which performs an ON\/OFF switching action when valid claps are detected.<\/p>\n<div id=\"attachment_6471\" style=\"width: 575px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/11\/ClapSwitchTitle.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6471\" class=\"size-full wp-image-6471\" title=\"ClapSwitchTitle\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/11\/ClapSwitchTitle.jpg\" alt=\"\" width=\"565\" height=\"404\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/11\/ClapSwitchTitle.jpg 565w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/11\/ClapSwitchTitle-550x393.jpg 550w\" sizes=\"auto, (max-width: 565px) 100vw, 565px\" \/><\/a><p id=\"caption-attachment-6471\" class=\"wp-caption-text\">Simple clap switch using a condenser mic and PIC microcontroller<\/p><\/div>\n<p><!--more--><strong>Theory<\/strong><\/p>\n<p>The clap switch requires a transducer at the input to convert sound vibrations from clapping hands into electrical energy. An electret microphone or simply mic (OBO-04FN-0B) is used for this purpose. The output from the mic is very low in magnitide and so we need an amplifier circuit to boost the detected sound signal. The following circuit diagram shows the mic along with a single transistor amplifier. When there&#8217;s no sound, the collector voltage (Vout) of the transistor, which is saturated, is approximately 0.2V. When the mic detects a clap sound, the voltage across it drops suddenly. Since the condenser mic&#8217;s output voltage is coupled to the base of the transistor through capacitor C1, the base-emitter voltage is also lowered and as such the base-emitter junction is less forward biased or cutoff (in case of high clap sound). Therefore, every time a clapping sound is detected, there is a sudden peak arising at the collector voltage.<\/p>\n<div id=\"attachment_6441\" style=\"width: 558px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/11\/ClapSensorCircuit.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6441\" class=\"size-full wp-image-6441\" title=\"ClapSensorCircuit\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/11\/ClapSensorCircuit.png\" alt=\"\" width=\"548\" height=\"476\" \/><\/a><p id=\"caption-attachment-6441\" class=\"wp-caption-text\">Sensor part<\/p><\/div>\n<div id=\"attachment_6444\" style=\"width: 580px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/11\/ClapSensorSetup.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6444\" class=\"size-full wp-image-6444\" title=\"ClapSensorSetup\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/11\/ClapSensorSetup.jpg\" alt=\"\" width=\"570\" height=\"435\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/11\/ClapSensorSetup.jpg 570w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/11\/ClapSensorSetup-550x419.jpg 550w\" sizes=\"auto, (max-width: 570px) 100vw, 570px\" \/><\/a><p id=\"caption-attachment-6444\" class=\"wp-caption-text\">Clap sensor setup on breadboard<\/p><\/div>\n<p>The following picture shows the peaks in the collector voltage due to multiple clap sounds. You can see the peak voltage can go as high as 4.0V depending on the loudness of the clap. This waveform of the collector voltage is captured using Digilent&#8217;s <a href=\"http:\/\/http:\/\/www.digilentinc.com\/Products\/Detail.cfm?Prod=ANALOG-DISCOVERY\" target=\"_blank\">Analog Discovery<\/a> device.<\/p>\n<div id=\"attachment_6440\" style=\"width: 575px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/11\/Clap_Waveform.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6440\" class=\"size-full wp-image-6440\" title=\"Clap_Waveform\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/11\/Clap_Waveform.png\" alt=\"\" width=\"565\" height=\"404\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/11\/Clap_Waveform.png 565w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/11\/Clap_Waveform-550x393.png 550w\" sizes=\"auto, (max-width: 565px) 100vw, 565px\" \/><\/a><p id=\"caption-attachment-6440\" class=\"wp-caption-text\">Peaks in Vout due to clapping sound<\/p><\/div>\n<p>Now we know how to convert a clap sound into an electrical signal. The next stage is to feed this signal to PIC12F683 microcontroller for switching actions. The PIC12F683 microcontroller has got a built-in comparator module that can be used to compare two analog voltages and obtain a digital indication of their relative magnitudes. \u00a0The comparator module can operate in eight different modes based on CM2-CM0 bit settings in the CMCON0 register. For our purpose, we will configure it as:<\/p>\n<p>CIN- pin is configured as analog,\u00a0CIN+ pin is configured as I\/O, COUT pin is configured as I\/O, Comparator output\u00a0available internally, CVREF is non-inverting input (see picture below).<\/p>\n<div id=\"attachment_6469\" style=\"width: 468px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/11\/CMCON0_Config.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6469\" class=\"size-full wp-image-6469\" title=\"CMCON0_Config\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/11\/CMCON0_Config.png\" alt=\"\" width=\"458\" height=\"200\" \/><\/a><p id=\"caption-attachment-6469\" class=\"wp-caption-text\">Configuring the internal comparator module of PIC12F683<\/p><\/div>\n<p>We will compare the collector output voltage (Vout) against an internally generated reference voltage. The reference voltage is internally connected to the positive input (CIN+) of the comparator module, while the output voltage from the transducer is fed to the negative input (CIN-) of the comparator. The CIN- pin is multiplexed with GP1 I\/O pin of PIC12F683.\u00a0The magnitude of reference voltage is programmable and controlled through VRCON register. We will set the reference voltage to 0.625V (assuming the supply voltage is 5.0V). So, under normal condition the reference voltage (0.625V) is greater than Vout (=0.2V) and the comparator output (COUT) is high. COUT is accessible internally as well as externally through GP2 I\/O pin. When there is a clap sound, COUT will go low. The comparator output logic can be inverted by setting the comparator output inversion (CIN) bit in the CMCON0 register. The PIC12F683 microcontroller can be programmed to take switching actions based on the comparator output.<\/p>\n<p><strong>Circuit diagram<\/strong><\/p>\n<p>The complete circuit diagram of this simple clap switch project is shown below. The positive and negative inputs of the internal comparator module are externally accessible through GP0\/CIN+ and GP1\/CIN- pins, respectively. Since, the positive input is connected to the internal reference voltage source, GP1 pin can be used as a I\/O pin. The output from the transducer amplifier goes to GP0\/CIN+ pin. An LED is connected to GP5 pin to indicate the switching action of the microcontroller. When the microcontroller detects two clapping sound in a row, it toggles the logic output at GP5 pin. The LED is used here for illustrative purpose and it can be replaced with an electromechanical relay if you want to control an electrical appliance with it.<\/p>\n<div id=\"attachment_6738\" style=\"width: 579px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/11\/MicrocontrollerCircuit2.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6738\" class=\"size-full wp-image-6738\" title=\"MicrocontrollerCircuit2\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/11\/MicrocontrollerCircuit2.png\" alt=\"\" width=\"569\" height=\"402\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/11\/MicrocontrollerCircuit2.png 569w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/11\/MicrocontrollerCircuit2-550x388.png 550w\" sizes=\"auto, (max-width: 569px) 100vw, 569px\" \/><\/a><p id=\"caption-attachment-6738\" class=\"wp-caption-text\">Complete circuit diagram of the clap switch<\/p><\/div>\n<div id=\"attachment_6472\" style=\"width: 575px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/11\/CompleteSetupClapSwitch2.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6472\" class=\"size-full wp-image-6472\" title=\"CompleteSetupClapSwitch2\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/11\/CompleteSetupClapSwitch2.jpg\" alt=\"\" width=\"565\" height=\"439\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/11\/CompleteSetupClapSwitch2.jpg 565w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/11\/CompleteSetupClapSwitch2-550x427.jpg 550w\" sizes=\"auto, (max-width: 565px) 100vw, 565px\" \/><\/a><p id=\"caption-attachment-6472\" class=\"wp-caption-text\">Circuit setup on breadboard<\/p><\/div>\n<p><strong>Software<\/strong><\/p>\n<p>The program is developed in C and compiled with MikroC Pro for PIC compiler. The program continuously looks for two clapping sounds in a row withing 1.5 sec interval. If that happens then GP5 pin is toggled. When a clap is detected, Timer1 module is turned on to keep record of time. It generates a <strong><em>Time Out<\/em><\/strong>\u00a0signal after 1.5 seconds. If there&#8217;s no second clap before that, the microcontroller ignores the first clap, and returns to the main program.<\/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;\">\/*\n Project: Making a simple clap switch\n Description : Clap switch using built-in comparator module\n               of PIC12F683\n MCU: PIC12F683\n Oscillator: Internal 4.0000 MHz, MCLR Disabled, PWRT ON enabled\n Written by: Rajendra Bhatt (www.embedded-lab.com)\n Date:       Nov 21, 2012\n*\/<\/span>\n&nbsp;\nsbit Output_LED at GP5_bit<span style=\"color: #339933;\">;<\/span>\n<span style=\"color: #993333;\">unsigned<\/span> <span style=\"color: #993333;\">short<\/span> i<span style=\"color: #339933;\">,<\/span> TIME_UP<span style=\"color: #339933;\">;<\/span>\n&nbsp;\n<span style=\"color: #993333;\">void<\/span> interrupt<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #993333;\">void<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#123;<\/span>\n  <span style=\"color: #b1b100;\">if<\/span><span style=\"color: #009900;\">&#40;<\/span>PIR1.<span style=\"color: #202020;\">TMR1IF<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>\n    i <span style=\"color: #339933;\">++;<\/span>\n    <span style=\"color: #b1b100;\">if<\/span><span style=\"color: #009900;\">&#40;<\/span>i <span style=\"color: #339933;\">==<\/span> <span style=\"color: #0000dd;\">3<\/span><span style=\"color: #009900;\">&#41;<\/span> TIME_UP <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>  <span style=\"color: #666666; font-style: italic;\">\/\/ Time Up in 1.5 sec<\/span>\n    PIR1.<span style=\"color: #202020;\">TMR1IF<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n <span style=\"color: #009900;\">&#125;<\/span>\n<span style=\"color: #009900;\">&#125;<\/span>\n&nbsp;\n<span style=\"color: #993333;\">void<\/span> main<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>\n  TRISIO <span style=\"color: #339933;\">=<\/span> <span style=\"color: #208080;\">0b00000011<\/span> <span style=\"color: #339933;\">;<\/span>\n  ANSEL <span style=\"color: #339933;\">=<\/span> <span style=\"color: #208080;\">0x00<\/span><span style=\"color: #339933;\">;<\/span>\n  INTCON <span style=\"color: #339933;\">=<\/span> <span style=\"color: #208080;\">0b11000000<\/span> <span style=\"color: #339933;\">;<\/span> <span style=\"color: #666666; font-style: italic;\">\/\/ Enable GIE and PEIE for Timer1 overflow interrpt<\/span>\n  PIE1 <span style=\"color: #339933;\">=<\/span> <span style=\"color: #208080;\">0b00000001<\/span> <span style=\"color: #339933;\">;<\/span>   <span style=\"color: #666666; font-style: italic;\">\/\/ Enable TMR1IE<\/span>\n&nbsp;\n  <span style=\"color: #666666; font-style: italic;\">\/\/ Configure Comparator module<\/span>\n  <span style=\"color: #666666; font-style: italic;\">\/\/ CIN- pin is configured as analog,<\/span>\n  <span style=\"color: #666666; font-style: italic;\">\/\/ CIN+ pin is configured as I\/O,<\/span>\n  <span style=\"color: #666666; font-style: italic;\">\/\/ COUT pin is configured as I\/O,<\/span>\n  <span style=\"color: #666666; font-style: italic;\">\/\/ Comparator output available internally,<\/span>\n  <span style=\"color: #666666; font-style: italic;\">\/\/ CVREF is non-inverting input<\/span>\n  <span style=\"color: #666666; font-style: italic;\">\/\/ CINV is set to 1<\/span>\n  CMCON0 <span style=\"color: #339933;\">=<\/span> <span style=\"color: #208080;\">0b00010100<\/span><span style=\"color: #339933;\">;<\/span>\n  VRCON  <span style=\"color: #339933;\">=<\/span> <span style=\"color: #208080;\">0b10100011<\/span><span style=\"color: #339933;\">;<\/span>  <span style=\"color: #666666; font-style: italic;\">\/\/ Vref is set to VDD\/8<\/span>\n  Output_LED <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n  <span style=\"color: #b1b100;\">do<\/span><span style=\"color: #009900;\">&#123;<\/span>\n     TMR1H <span style=\"color: #339933;\">=<\/span> <span style=\"color: #208080;\">0x00<\/span><span style=\"color: #339933;\">;<\/span>\n     TMR1L <span style=\"color: #339933;\">=<\/span> <span style=\"color: #208080;\">0x00<\/span><span style=\"color: #339933;\">;<\/span>\n     TIME_UP <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n     i <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n     T1CON <span style=\"color: #339933;\">=<\/span> <span style=\"color: #208080;\">0b00110000<\/span><span style=\"color: #339933;\">;<\/span>   <span style=\"color: #666666; font-style: italic;\">\/\/ Configure Timer 1<\/span>\n     <span style=\"color: #b1b100;\">if<\/span><span style=\"color: #009900;\">&#40;<\/span>CMCON0.<span style=\"color: #202020;\">COUT<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#123;<\/span>      <span style=\"color: #666666; font-style: italic;\">\/\/ First clap detected<\/span>\n       Delay_ms<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000dd;\">100<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n       T1CON.<span style=\"color: #202020;\">TMR1ON<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>   <span style=\"color: #666666; font-style: italic;\">\/\/ Start Timer1<\/span>\n       <span style=\"color: #b1b100;\">while<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #339933;\">!<\/span>CMCON0.<span style=\"color: #202020;\">COUT<\/span> <span style=\"color: #339933;\">&amp;&amp;<\/span> <span style=\"color: #339933;\">!<\/span>TIME_UP<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span> <span style=\"color: #666666; font-style: italic;\">\/\/ Wait until second clap is<\/span>\n       T1CON.<span style=\"color: #202020;\">TMR1ON<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>                      <span style=\"color: #666666; font-style: italic;\">\/\/ detected or Timer1 overflows<\/span>\n       <span style=\"color: #b1b100;\">if<\/span><span style=\"color: #009900;\">&#40;<\/span>CMCON0.<span style=\"color: #202020;\">COUT<\/span> <span style=\"color: #339933;\">&amp;&amp;<\/span> <span style=\"color: #339933;\">!<\/span>TIME_UP<span style=\"color: #009900;\">&#41;<\/span> Output_LED <span style=\"color: #339933;\">=<\/span> ~Output_LED<span style=\"color: #339933;\">;<\/span>\n       Delay_ms<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000dd;\">100<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n     <span style=\"color: #009900;\">&#125;<\/span>\n  <span style=\"color: #009900;\">&#125;<\/span>  <span style=\"color: #b1b100;\">while<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000dd;\">1<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n<span style=\"color: #009900;\">&#125;<\/span><\/pre><\/td><\/tr><\/table><\/div>\n\n<p><a href=\"http:\/\/embedded-lab.com\/uploads\/HexFiles\/PIC12F\/ClapSwitch.zip\" target=\"_blank\"><strong>Download mikroC source and HEX files<\/strong><\/a><\/p>\n<p><strong>Output<\/strong><\/p>\n<p>Watch the following video to see the clap switch in action. It has been tested with clapping made at 10 feet away and found operating well.<\/p>\n<p><iframe loading=\"lazy\" src=\"http:\/\/www.youtube.com\/embed\/5d3468IZKtk\" frameborder=\"0\" width=\"480\" height=\"360\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A clap switch is a fun project for beginners. It switches on and off electrical appliances with a sound of clapping hands. Today we will discuss about making a simple clap switch that operates when it detects two clapping sounds in a row. It uses an electret microphone as a transducer for converting a clapping sound into an electrical signal. The microcphone output is amplified by a transistor and is then sent to the PIC12F683 microcontroller which performs an ON\/OFF switching action when valid claps are detected.<\/p>\n","protected":false},"author":1,"featured_media":6440,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[245,4,183],"tags":[511,512,6],"class_list":["post-6439","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded-lab-projects","category-pic-projects","category-tips-and-tricks","tag-clap-switch","tag-microphone","tag-pic12f683"],"_links":{"self":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/6439","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=6439"}],"version-history":[{"count":23,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/6439\/revisions"}],"predecessor-version":[{"id":6539,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/6439\/revisions\/6539"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media\/6440"}],"wp:attachment":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media?parent=6439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/categories?post=6439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/tags?post=6439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}