Advertisement
atuline

ESP Sample

Jul 2nd, 2020
1,432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.36 KB | None | 0 0
  1. /* ESPSample
  2.  *
  3.  * By: Andrew Tuline
  4.  *
  5.  * Updated: Feb, 2019
  6.  *
  7.  * Basic code to read and calculate average from the Sparkfun INMP401/MAX4466/MAX9814 microphone
  8.  * on an ESP32 or ESP8266.
  9.  *
  10.  * Use the Arduino Serial plotter to view the output. Compare results to those found at:
  11.  *
  12.  * https://github.com/atuline/WLED/blob/assets/docs/Microphones.pdf
  13.  *
  14.  * Note that the ESP32 employs a 12 bit A/D, while the ESP8266 has a 10 bit A/D. Also note
  15.  * the anomalous spikes on the ESP8266.
  16.  *
  17.  * The micLev variable is the DC Offset value that you can use for zeroeing your samples.
  18.  *
  19.  */
  20.  
  21. #ifdef ESP8266
  22. #define MIC_PIN A0                              // ESP8266 pin A0
  23. #else
  24. #define MIC_PIN 36                              // ESP32 pin also known as 'VP'.
  25. #endif
  26.  
  27. void setup() {
  28.   delay(1000);
  29.   Serial.begin(115200);                         // Initialize serial port for debugging.
  30. } // setup()
  31.  
  32. void loop() {
  33.   analog_sample();
  34. } // loop()
  35.  
  36. void analog_sample() {
  37.   static float micLev;                          // Needs to be a float, or smoothing calculation below will be very inaccurate.
  38.   int micIn = analogRead(MIC_PIN);
  39.   micLev = ((micLev*31)+micIn)/32;              // Smooth out the data to get average value (used for zeroeing).
  40.   Serial.print(micIn); Serial.print(" ");
  41.   Serial.print(micLev); Serial.println(" ");
  42. } // analog_sample()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement