Arduino Light Clapper use Sound Detector

MechatronicsLAB
4 min readJun 16, 2021

--

a sound detector senses sound. The type of sound detector used in thethistutorial is a digital sound detector that is activated when a certain level of sound is detected in the environment. The sensor board is connected to a small microphone to pick up sounds

This tutorial allows you to learn how to create a system with a sound detector that allows you to switch a lead on or off twice in quick succession. It is similar to a commercially available product called the Clapper, a sound electric switch that can activate or disable lights that are connected to it when the user claps.

Schematic Diagram

  • Connect the VCC pin on the sound detector to the 3.3-V pin of the Arduino.
  • Connect the GND pin on the sound detector to a GND pin of the Arduino.
  • Connect the output pin on the sound detector to digital pin 8 of the Arduino.
  • Connect the negative terminal of the LED to a GND pin of the Arduino.
  • Connect the positive terminal of the LED to digital pin 7 of the Arduino

Program analysis

The number of sounds detected by the sound detector since the program starts and is initialized to 0 is the variable NumberSounds:

The output pin on the sound detector is assigned to digital pin 8 of the Arduino:

The RawValue variable holds the status of the sound detector and is initialized to 0:

The NumberClaps variable stores the current count of claps toward the two required to toggle the LED light. This is starting to 0 claps:

The LightOn variable is nonzero if the LED is turned on and 0 otherwise. Here it is initialized to 0:

The SoundDetectedTime variable stores the time of the last detected sound and is used to calculate the number of distinct claps, as a single clap can generate multiple positive sound detector readings. The initial value of this variable is 0:

unsigned long SoundDetectedTime = 0;

The PreviousSoundDetectedTime variable holds the time of the previous sound detected and is used to determine the number of unique claps:

unsigned long PreviousSoundDetectedTime = 0;

The UniqueClapMinTime variable stores the minimum time interval in milliseconds between detected sounds that the program must recognize as a unique clap:

int UniqueClapMinTime = 100;

The LEDPin variable represents digital pin 7, which is connected to the positive terminal of the LED:

The PreviousClapTime variable holds the time in milliseconds that the previous clap occurred and is initialized to 0:

unsigned long PreviousClapTime = 0;

The CurrentClapTime variable holds the time in milliseconds that the current clap has occurred and is initialized to 0:

unsigned long CurrentClapTime = 0;

The MaxTimeBetweenClaps variable specifies the maximum amount of time between two consecutive claps that can be used to toggle the LED light. By default, the timeout value is set to 2, 000 milliseconds, or two seconds.

unsigned long MaxTimeBetweenClaps = 2000;

The setup() function initializes the program and:

1. Sets the Arduino pin that is connected to the sound detector’s output pin to be an input pin so that voltages can be measured.

2. Sets the Arduino pin that is connected to the LED’s positive terminal to be an output pin that it can deliver voltage to and drive the LED.

3. Initializes the Serial Monitor and sets the communication speed to 9, 600 baud.

4. Prints a text message to the Serial Monitor indicating that the program has started.

The IsSoundPartOfUniqueClap() function determines whether the sound detected by the sound detector is the beginning of a new unique clap or a continuation of the current clap. This is accomplished through the following:

Calculating the elapsed time since the previous sound was detected.

If this time is greater than or equal to the minimum time required for a unique clap to be recognized, then the function returns 1.

Otherwise, the function returns 0.

The CheckTurnOnOffLight() function determines whether or not the LED should be toggled by detecting two consecutive claps within two seconds. This is accomplished through the following:

Calculating the elapsed time between the current detected clap and the previous detected clap.

If the elapsed time is less than or equal to the maximum allowed time between claps, then if the number of claps detected is two, sets the return value to 1, which means that the LED on/off status should be toggled, and resets the number of claps detected to 0.

If the elapsed time is greater than the maximum allowed time between claps, sets the number of claps detected to one to indicate that the currently detected clap is the only valid clap for the two-clap turn on/off sequence.

Returns 1 if the LED should be toggled and 0 otherwise.

The loop() function reads the output of the sound detector, determines whether the sound detected is a unique clap or a continuation of a previous clap, determines whether a two-clap sequence was detected, and toggles the LED light if it was. This is accomplished through the following:

Reading the status of the sound detector.

If the value read is equal to 0, which means that a sound has been detected, then the function:

Prints out some debug information to the Serial Monitor.

Updates the variables that keep track of the times for the current sound detection event and the previous sound detection event.

If this sound detection event is part of a new unique clap, then the function:

Increases the number of claps that have been detected by 1.

Updates the times for the previous clap and the current clap.

If two consecutive claps have occurred within 2 seconds, then the function:

Toggles the LED status variable by performing a bitwise NOT operation on the LightOn variable.

Turns on the LED if the LightOn variable evaluates to true (nonzero).

Turns off the LED if the LightOn variable evaluates to false (zero).

Originally published at https://www.hackster.io.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

MechatronicsLAB
MechatronicsLAB

Written by MechatronicsLAB

0 Followers

Our main focus will be on Mechatronics Technology where we will work in #Industrial_Automation, #PLC, #IoT, #Raspberry_Pi, #Arduino, #Microcontroller

No responses yet

Write a response