Whistle Detector

Control the brightness of an LED with whistling!

Introduction

This example reads audio data from an Invensense's ICS43432I2S microphone breakout board, and uses the input to detect whistling sounds at a particular frequency. When a whistle is detected, it's level is used to control the brightness of the built-in LED.

Goals

  • How to use and read audio data.
  • How to use audio as an input to control the brightness of an LED.

Hardware & Software Needed

Circuit

Whistle detector circuit
Whistle 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 whistling sounds at a particular
6
7 frequency. When a whistle is detected, it's level is used to control the
8
9 brightness of an LED
10
11 Circuit:
12
13 * Arduino Zero, MKRZero or MKR1000 board
14
15 * ICS43432:
16
17 * GND connected GND
18
19 * 3.3V connected 3.3V (Zero) or VCC (MKR1000, MKRZero)
20
21 * WS connected to pin 0 (Zero) or pin 3 (MKR1000, MKRZero)
22
23 * CLK connected to pin 1 (Zero) or pin 2 (MKR1000, MKRZero)
24
25 * SD connected to pin 9 (Zero) or pin A6 (MKR1000, MKRZero)
26
27 created 30 November 2016
28
29 by Sandeep Mistry
30
31 */
32
33#include <ArduinoSound.h>
34
35// the LED pin to use as output
36
37const int ledPin = LED_BUILTIN;
38
39// sample rate for the input
40
41const int sampleRate = 8000;
42
43// size of the FFT to compute
44
45const int fftSize = 128;
46
47// size of the spectrum output, half of FFT size
48
49const int spectrumSize = fftSize / 2;
50
51// frequency of whistle to detect
52
53const int whistleFrequency = 1250;
54
55// map whistle frequency to FFT bin
56
57const int whistleBin = (whistleFrequency * fftSize / sampleRate);
58
59// array to store spectrum output
60int spectrum[spectrumSize];
61
62// create an FFT analyzer to be used with the I2S input
63
64FFTAnalyzer fftAnalyzer(fftSize);
65
66void setup() {
67
68 // setup the serial
69
70 Serial.begin(9600);
71
72 // configure the pin for output mode
73
74 pinMode(ledPin, OUTPUT);
75
76 // setup the I2S audio input for the sample rate with 32-bits per sample
77
78 if (!AudioInI2S.begin(sampleRate, 32)) {
79
80 Serial.println("Failed to initialize I2S input!");
81
82 while (1); // do nothing
83
84 }
85
86 // configure the I2S input as the input for the FFT analyzer
87
88 if (!fftAnalyzer.input(AudioInI2S)) {
89
90 Serial.println("Failed to set FFT analyzer input!");
91
92 while (1); // do nothing
93
94 }
95}
96
97void loop() {
98
99 if (fftAnalyzer.available()) {
100
101 // analysis available, read in the spectrum
102
103 fftAnalyzer.read(spectrum, spectrumSize);
104
105 // map the value of the whistle bin magnitude between 0 and 255
106
107 int ledValue = map(spectrum[whistleBin], 50000, 60000, 0, 255);
108
109 // cap the values
110
111 if (ledValue < 0) {
112
113 ledValue = 0;
114
115 } else if (ledValue > 255) {
116
117 ledValue = 255;
118
119 }
120
121 // set LED brightness based on whistle bin magnitude
122
123 analogWrite(ledPin, ledValue);
124
125 }
126}

Testing It Out

After you have uploaded the code, start whisling. You should now be able to control the brightness of the in-built LED depending on the level of the whistle.

Troubleshoot

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

  • The microphone is not wired correctly.
  • You have not uploaded the sketch to the board.
  • You have not installed the correct drivers.
  • You have not installed the ArduinoSound Library.

Conclusion

In this example, we have learned how to use whistling to control the brightness of the in-built LED, using the ArduinoSound Library and a microphone. Now that you have learned how to use whistling as an input, you can start thinking about how to apply this input for other projects!

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.