|
| 1 | +/* |
| 2 | + This example reads audio data from an Invensense's ICS43432 I2S microphone |
| 3 | + breakout board, and uses the input to detect whistling sounds at a particular |
| 4 | + frequency. When a whistle is detected, it's level is used to control the |
| 5 | + brightness of an LED |
| 6 | +
|
| 7 | + Circuit: |
| 8 | + * Arduino/Genuino Zero or MKR1000 board |
| 9 | + * ICS43432: |
| 10 | + * GND connected GND |
| 11 | + * 3.3V connected 3.3V |
| 12 | + * WS connected to pin 0 (Zero) or pin 3 (MKR1000) |
| 13 | + * CLK connected to pin 1 (Zero) or pin 2 (MKR1000) |
| 14 | + * SD connected to pin 9 (Zero) or pin A6 (MKR1000) |
| 15 | +
|
| 16 | + created 30 November 2016 |
| 17 | + by Sandeep Mistry |
| 18 | + */ |
| 19 | + |
| 20 | +#include <ArduinoSound.h> |
| 21 | + |
| 22 | +// the LED pin to use as output |
| 23 | +const int ledPin = LED_BUILTIN; |
| 24 | + |
| 25 | +// sample rate for the input |
| 26 | +const int sampleRate = 8000; |
| 27 | + |
| 28 | +// size of the FFT to compute |
| 29 | +const int fftSize = 128; |
| 30 | + |
| 31 | +// size of the spectrum output, half of FFT size |
| 32 | +const int spectrumSize = fftSize / 2; |
| 33 | + |
| 34 | +// frequency of whistle to detect |
| 35 | +const int whistleFrequency = 1250; |
| 36 | + |
| 37 | +// map whistle frequency to FFT bin |
| 38 | +const int whistleBin = (whistleFrequency * fftSize / sampleRate); |
| 39 | + |
| 40 | +// array to store spectrum output |
| 41 | +int spectrum[spectrumSize]; |
| 42 | + |
| 43 | +// create an FFT analyzer to be used with the I2S input |
| 44 | +FFTAnalyzer fftAnalyzer(fftSize); |
| 45 | + |
| 46 | +void setup() { |
| 47 | + // setup the serial |
| 48 | + Serial.begin(9600); |
| 49 | + |
| 50 | + // configure the pin for output mode |
| 51 | + pinMode(ledPin, OUTPUT); |
| 52 | + |
| 53 | + // setup the I2S audio input for the sample rate with 32-bits per sample |
| 54 | + if (AudioInI2S.begin(sampleRate, 32)) { |
| 55 | + Serial.println("Failed to initialize I2S input!"); |
| 56 | + while (1); // do nothing |
| 57 | + } |
| 58 | + |
| 59 | + // configure the I2S input as the input for the FFT analyzer |
| 60 | + if (fftAnalyzer.input(AudioInI2S)) { |
| 61 | + Serial.println("Failed to set FFT analyzer input!"); |
| 62 | + while (1); // do nothing |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +void loop() { |
| 67 | + if (fftAnalyzer.available()) { |
| 68 | + // analysis available, read in the spectrum |
| 69 | + fftAnalyzer.read(spectrum, spectrumSize); |
| 70 | + |
| 71 | + // map the value of the whistle bin magnitude between 0 and 255 |
| 72 | + int ledValue = map(spectrum[whistleBin], 50000, 60000, 0, 255); |
| 73 | + |
| 74 | + // cap the values |
| 75 | + if (ledValue < 0) { |
| 76 | + ledValue = 0; |
| 77 | + } else if (ledValue > 255) { |
| 78 | + ledValue = 255; |
| 79 | + } |
| 80 | + |
| 81 | + // set LED brightness based on whistle bin magnitude |
| 82 | + analogWrite(ledPin, ledValue); |
| 83 | + } |
| 84 | +} |
0 commit comments