Spectrum Serial Plotter

This example reads audio data from an Invensense ICS43432I2S microphone breakout board, and prints out the spectrum to the Serial console.

This example reads audio data from an Invensense ICS43432I2S microphone breakout board, and prints out the spectrum to the Serial console. The Serial Plotter built into the Arduino IDE can be used to plot the audio amplitude data (Serial Plotter Tools).

Hardware Required

Circuit

The circuit for this example.
The circuit for this example.

Code

1/*
2
3 This example reads audio data from an Invensense's ICS43432 I2S microphone
4
5 breakout board, and prints out the spectrum to the Serial console. The
6
7 Serial Plotter built into the Arduino IDE can be used to plot the audio
8
9 amplitude data (Tools -> Serial Plotter)
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 21 November 2016
28
29 by Sandeep Mistry
30
31 */
32
33#include <ArduinoSound.h>
34
35// sample rate for the input
36
37const int sampleRate = 8000;
38
39// size of the FFT to compute
40
41const int fftSize = 128;
42
43// size of the spectrum output, half of FFT size
44
45const int spectrumSize = fftSize / 2;
46
47// array to store spectrum output
48int spectrum[spectrumSize];
49
50// create an FFT analyzer to be used with the I2S input
51
52FFTAnalyzer fftAnalyzer(fftSize);
53
54void setup() {
55// Open serial communications and wait for port to open:
56
57 // A baud rate of 115200 is used instead of 9600 for a faster data rate
58
59 // on non-native USB ports
60
61 Serial.begin(115200);
62
63 while (!Serial) {
64
65 ; // wait for serial port to connect. Needed for native USB port only
66
67 }
68
69 // setup the I2S audio input for the sample rate with 32-bits per sample
70
71 if (!AudioInI2S.begin(sampleRate, 32)) {
72
73 Serial.println("Failed to initialize I2S input!");
74
75 while (1); // do nothing
76
77 }
78
79 // configure the I2S input as the input for the FFT analyzer
80
81 if (!fftAnalyzer.input(AudioInI2S)) {
82
83 Serial.println("Failed to set FFT analyzer input!");
84
85 while (1); // do nothing
86
87 }
88}
89
90void loop() {
91
92 // check if a new analysis is available
93
94 if (fftAnalyzer.available()) {
95
96 // read the new spectrum
97
98 fftAnalyzer.read(spectrum, spectrumSize);
99
100 // print out the spectrum
101
102 for (int i = 0; i < spectrumSize; i++) {
103
104 Serial.print((i * sampleRate) / fftSize); // the starting frequency
105
106 Serial.print("\t"); //
107
108 Serial.println(spectrum[i]); // the spectrum value
109
110 }
111
112 }
113}

Last revision 2016/12/06 by AG

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.