SpectrumSerialPlotter.ino 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. This example reads audio data from an Invensense's ICS43432 I2S microphone
  3. breakout board, and prints out the spectrum to the Serial console. The
  4. Serial Plotter built into the Arduino IDE can be used to plot the audio
  5. amplitude data (Tools -> Serial Plotter)
  6. Circuit:
  7. * Arduino/Genuino Zero, MKRZero or MKR1000 board
  8. * ICS43432:
  9. * GND connected GND
  10. * 3.3V connected 3.3V (Zero) or VCC (MKR1000, MKRZero)
  11. * WS connected to pin 0 (Zero) or pin 3 (MKR1000, MKRZero)
  12. * CLK connected to pin 1 (Zero) or pin 2 (MKR1000, MKRZero)
  13. * SD connected to pin 9 (Zero) or pin A6 (MKR1000, MKRZero)
  14. created 21 November 2016
  15. by Sandeep Mistry
  16. */
  17. #include <ArduinoSound.h>
  18. // sample rate for the input
  19. const int sampleRate = 8000;
  20. // size of the FFT to compute
  21. const int fftSize = 128;
  22. // size of the spectrum output, half of FFT size
  23. const int spectrumSize = fftSize / 2;
  24. // array to store spectrum output
  25. int spectrum[spectrumSize];
  26. // create an FFT analyzer to be used with the I2S input
  27. FFTAnalyzer fftAnalyzer(fftSize);
  28. void setup() {
  29. // Open serial communications and wait for port to open:
  30. // A baud rate of 115200 is used instead of 9600 for a faster data rate
  31. // on non-native USB ports
  32. Serial.begin(115200);
  33. while (!Serial) {
  34. ; // wait for serial port to connect. Needed for native USB port only
  35. }
  36. // setup the I2S audio input for the sample rate with 32-bits per sample
  37. if (!AudioInI2S.begin(sampleRate, 32)) {
  38. Serial.println("Failed to initialize I2S input!");
  39. while (1); // do nothing
  40. }
  41. // configure the I2S input as the input for the FFT analyzer
  42. if (!fftAnalyzer.input(AudioInI2S)) {
  43. Serial.println("Failed to set FFT analyzer input!");
  44. while (1); // do nothing
  45. }
  46. }
  47. void loop() {
  48. // check if a new analysis is available
  49. if (fftAnalyzer.available()) {
  50. // read the new spectrum
  51. fftAnalyzer.read(spectrum, spectrumSize);
  52. // print out the spectrum
  53. for (int i = 0; i < spectrumSize; i++) {
  54. Serial.print((i * sampleRate) / fftSize); // the starting frequency
  55. Serial.print("\t"); //
  56. Serial.println(spectrum[i]); // the spectrum value
  57. }
  58. }
  59. }