Skip to content
FREE CA & US Shipping for CAD$89+ Orders | FREE CA Shipping on Applicable Products

Country/region

Cart

Tutorial #6: Making “The Clapper” with the digital sound sensor

Introduction

 

If you’ve been around in the 90’s then you likely remember “The Clapper”. They had a very catchy commercial … “Clap ON, Clap OFF. The Clapper!”. I still have a working Clapper since then and up until recently used it regularly! In this tutorial we will be making a simpler version of the Clapper by using the digital sound sensor. This tutorial heavily relies on details from the previous tutorial. So if you haven’t yet, I encourage you to look at the getting started with the digital sound sensor tutorial. The physical setup (hardware and connections) will be identical. In this tutorial we will only be tweaking the script a bit to recognize when two successive claps have been detected, which will turn on an LED.

Required hardware

The hardware is identical to the previous tutorial, but we list the BOM here again.

Setting up the hardware

Same as previous tutorial!

  • VCC pin on the sensor to the 3.3V pin on the Nano.
  • GND pin on the sensor to GND pin on the Nano.
  • Digital pin (labelled “out”) on the sensor to analog pin A3 on the Nano.

Setting up the software

In the previous tutorial we chose not to use the only digital pin available, D13, that was on the same side as the 3.3V and GND pins. Instead we decided to use an analog pin so to keep D13 available. Here it will become apparent why we did so. Without having to redo any of the hardware and wiring, as it is identical to the previous tutorial, here we will alter the script a bit to be able to detect two successive claps. Once detected this will turn ON the Nano onboard LED, which is controlled by the digital pin D13!

The script is modified to detect two claps. There are some timers that set a flag to make sure the sequence works properly. The time between the two claps has to be a minimum of 100ms, but less than 4000ms. If the time exceeds reset the sequence. That was a single detection and the timer expired. If two claps are too close together (less than 100ms) then the timer simply reset. I found that sometimes when a sound is detected the sensor outputs a trigger cluster. I.e. not just a single trigger, but a bunch within a few timesteps. To avoid any potential problems we have the 100ms min timer requirement.

Simply copy the following script into the IDE software.

// Project title: "Tutorial: Making "The Clapper" with the digital sound sensor"
// Date of last edit: Dec 6 2021, PTSolns
// For bugs or questions please send us an email at contact@PTSolns.com
// Overview: This is the script for the digital sound sensor. Set up the hardware as outlined in the tutorial.

int flag_clap    = 0;
int LED_status   = 0;  // Keep track of the LED status
int statusSensor = 1;  // Digital out of sensor
unsigned long timer;
long timer_min =  100;  // Min time between first and second clap. If second clap happens before this time, it will not 
register as a second clap, but rather just reset the timer of the first clap. long timer_max = 4000; // Max time between first and second clap. If time passed, reset flag_claps void setup() { pinMode(A3, INPUT); // Recall, we are using the analog pin A3 as an digital input pin. No problem! pinMode(LED_BUILTIN, OUTPUT); // Onboard LED } void loop() { statusSensor = digitalRead(A3); if (statusSensor == 0 && (flag_clap == 0 || flag_clap == 1)) { // Enter IF loop only when first clap is detected. flag_clap = 1; // First clap detected timer = millis(); // Start the timer } if (flag_clap == 1 && ((millis() - timer) > timer_min)) { // If first clap is detected and min time has passed. flag_clap = 2; // Ready to accept the second clap. } if (flag_clap == 2 && ((millis() - timer) > timer_max)) { // Waited too long! Reset flag_clap = 0; } if ((statusSensor == 0) && (flag_clap == 2)) { // If clap is deteced and flag_clap is ready flag_clap = 0; // Reset the flag for the next round if (LED_status == 0) { // Means LED is currently OFF, so turn ON LED_status = 1; digitalWrite(LED_BUILTIN, HIGH); } else if (LED_status == 1) { // Means LED is currenly ON, so turn OFF LED_status = 0; digitalWrite(LED_BUILTIN, LOW); } delay(500); // Need a small delay between turning ON and OFF the LED, otherwise clapping sequence is confused. } }

Results and conclusion

The video shows a couple of things. First the sound sensor green trigger LED turns ON momentarily when the clap is detected. In total throughout this video this trigger LED turns ON four times. Furthermore the red onboard LED (controlled by D13) turns ON after the first two claps, and then OFF after the next two claps. It is a bit hard to see as the onboard power LED, which is also red, is right next to the D13 controlled LED that we are switching. But if you look closely you can make out which LED is which.

This tutorial made only a simple version of the original clapper. The original clapper didn’t just turn ON an LED. It turned ON a relay, which powered an outlet. This is much more practical! But here we present only the proof-of-concept. You could add the relay and have some fun with it. But take proper precautions when dealing with any electronics that deals with household voltages and current!

Furthermore, the clapper has two outlets. The first is turned ON with two claps, and the second with three. You can easily expand on the above script to add the third clap detection. Finally, the clapper also has an “Away” feature. This is basically the same as the potentiometer on the sound sensor. Turning the “Away” dial on the clapper shifts the threshold for detection, making the device very sensitive to noise. So there you have it!