Clap Detector

Learn how to build a clap detector

Introduction

This example reads audio data from an Invensense's ICS43432I2S microphone breakout board, and uses the input to detect clapping sounds. The built-in LED is toggled when a clap is detected.

Goals

  • How to use and read audio data.
  • How to use audio as an input.

Hardware & Software Needed

Circuit

Clap detector circuit
Clap detector circuit

Programming the Board

1. First, let's make sure we have correct the drivers installed. If we are using the Web Editor, we do not need to install anything. If we are using an offline editor, we need to install it manually. This can be done by navigating to Tools > Board > Board Manager.... Here we need to look for the Arduino SAMD boards (32-bits Arm® Cortex®-M0+) and install it.

2. Now, we need to install the libraries needed. If we are using the Web Editor, there is no need to install anything. If we are using an offline editor, simply go to Tools > Manage libraries... and search for ArduinoSound and install it.

The sketch can be found in the snippet below. Upload the sketch to the board.

Code

1/*
2
3 This example reads audio data from an Invensense's ICS43432 I2S microphone
4
5 breakout board, and uses the input to detect clapping sounds. An LED is
6
7 toggled when a clap is detected.
8
9 Circuit:
10
11 * Arduino Zero, MKRZero or MKR1000 board
12
13 * ICS43432:
14
15 * GND connected GND
16
17 * 3.3V connected 3.3V (Zero) or VCC (MKR1000, MKRZero)
18
19 * WS connected to pin 0 (Zero) or pin 3 (MKR1000, MKRZero)
20
21 * CLK connected to pin 1 (Zero) or pin 2 (MKR1000, MKRZero)
22
23 * SD connected to pin 9 (Zero) or pin A6 (MKR1000, MKRZero)
24
25 created 18 November 2016
26
27 by Sandeep Mistry
28
29 */
30
31#include <ArduinoSound.h>
32
33// the LED pin to use as output
34
35const int ledPin = LED_BUILTIN;
36
37// the amplitude threshold for a clap to be detected
38
39const int amplitudeDeltaThreshold = 100000000;
40
41// create an amplitude analyzer to be used with the I2S input
42
43AmplitudeAnalyzer amplitudeAnalyzer;
44
45// variable to keep track of last amplitude
46int lastAmplitude = 0;
47
48void setup() {
49
50 // setup the serial
51
52 Serial.begin(9600);
53
54 // configure the LED pin as an output
55
56 pinMode(ledPin, OUTPUT);
57
58 // setup the I2S audio input for 44.1 kHz with 32-bits per sample
59
60 if (!AudioInI2S.begin(44100, 32)) {
61
62 Serial.println("Failed to initialize I2S input!");
63
64 while (1); // do nothing
65
66 }
67
68 // configure the I2S input as the input for the amplitude analyzer
69
70 if (!amplitudeAnalyzer.input(AudioInI2S)) {
71
72 Serial.println("Failed to set amplitude analyzer input!");
73
74 while (1); // do nothing
75
76 }
77}
78
79void loop() {
80
81 // check if a new analysis is available
82
83 if (amplitudeAnalyzer.available()) {
84
85 // read the new amplitude
86
87 int amplitude = amplitudeAnalyzer.read();
88
89 // find the difference between the new amplitude and the last
90
91 int delta = amplitude - lastAmplitude;
92
93
94
95 // check if the difference is larger than the threshold
96
97 if (delta > amplitudeDeltaThreshold) {
98
99 // a clap was detected
100
101 Serial.println("clap detected");
102
103
104
105 // toggle the LED
106
107 digitalWrite(ledPin, !digitalRead(ledPin));
108
109
110
111 // delay a bit to debounce
112
113 delay(100);
114
115 }
116
117
118
119 // update the last amplitude with the new amplitude
120
121 lastAmplitude = amplitude;
122
123 }
124}

Testing It Out

After you have uploaded the code, start clapping. The in-built LED should now light up!

Troubleshoot

If the code is not working, there are some common issues we can troubleshoot:

  • The microphone is not wired correctly.
  • Make sure there are no missing curly brackets {}.
  • You have not installed the ArduinoSound Library.
  • You have not selected the right port and board.

Conclusion

In this example, we have learned how to create a clap detector using the ArduinoSound Library with a microphone to turn on the in-built LED. With this knowledge in the bag you can start to think about how sound can be used as an input to control various outputs!

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.