TIMOTHY MWALA
Published © LGPL

Connect Carenuity C3-Mini (ESP32-C3) With Max4466 via I2C

Build your Own Sound-Monitoring device using Max4466, C3-Mini, and an OLED display

BeginnerProtip1 hour86
Connect Carenuity C3-Mini (ESP32-C3) With Max4466 via I2C

Story

Read more

Code

Sound monitoring system

C/C++
#include <Wire.h>                                         // I2C Library for OLED
#include <Adafruit_GFX.h>                                 // Graphics library for OLED
#include <Adafruit_SSD1306.h>                             // OLED Driver

//--------------------------------------------------------------------------------------------
//                                           DEFINES
//--------------------------------------------------------------------------------------------

#define OLED_RESET 1                                     // OLED Reset in 1

//--------------------------------------------------------------------------------------------
//                                        LIBRARY CALLS
//--------------------------------------------------------------------------------------------

#define SCREEN_WIDTH 128                                  // OLED display width, in pixels
#define SCREEN_HEIGHT 64                                  // OLED display height, in pixels

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

//--------------------------------------------------------------------------------------------
//                                       GLOBAL VARIABLES
//--------------------------------------------------------------------------------------------

const int sampleWindow = 50;                              // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;

//--------------------------------------------------------------------------------------------
//                                            SETUP
//--------------------------------------------------------------------------------------------
const int soundSensorPin = 0;
void setup() 
{
   Serial.begin(9600);                                    // Serial comms for debugging
   if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {       // OLED display start with I2C address 0x3C
     Serial.println(F("SSD1306 allocation failed"));
     for(;;);
   }
   display.display();                                     // Show buffer
   display.clearDisplay();                                // Clear buffer
   display.setTextSize(1);                                // Set text size to 1 (1-6)
   display.setTextColor(SSD1306_WHITE);                  // Set text color to WHITE (no choice lol)
   display.setCursor(0,0);                                // Cursor to upper left corner
   display.println(" Arduino Sound Meter");               // Write title
   display.display();                                     // Show title
   delay(2000);                                           // Wait 2 seconds
}

//--------------------------------------------------------------------------------------------
//                                         MAIN LOOP
//--------------------------------------------------------------------------------------------
  
void loop() 
{
   unsigned long startMillis= millis();                   // Start of sample window
   float peakToPeak = 0;                                  // Peak-to-peak level
 
   unsigned int signalMax = 0;                            // Minimum value
   unsigned int signalMin = 4095;                         // Maximum value (for 12-bit ADC)

   // Collect data for 50 mS
   while (millis() - startMillis < sampleWindow)
   {
      sample = analogRead(0);                // Get reading from sound sensor pin
      if (sample < 4095)                                  // Toss out spurious readings
      {
         if (sample > signalMax)
         {
            signalMax = sample;                           // Save just the max levels
         }
         else if (sample < signalMin)
         {
            signalMin = sample;                           // Save just the min levels
         }
      }
   }

   peakToPeak = signalMax - signalMin;                    // Max - min = peak-peak amplitude
   float db = map(peakToPeak,20,4095,49.5,90);            // Calibrate for decibels
   display.setCursor(0,0);                                // Cursor to upper left
   display.setTextSize(1);                                // Set text size to 2
   display.print(db);                                     // Write calibrated decibels
   display.print(" dB");                                  // Write units

   for(int x =5;x<114;x=x+6){                            // Draw scale
      display.drawLine(x, 32, x, 27, SSD1306_WHITE);
   }
   display.drawRect(0, 32, 120, 20, SSD1306_WHITE);       // Draw outline of bar graph
   int r = map(db,0,120,1,120);                           // Set bar graph for width of screen
   display.fillRect(1, 33, r, 18, SSD1306_WHITE);         // Draw bar graph with a width of r
   display.display();                                     // Show all that we just wrote & drew
   display.clearDisplay();                                // Clear the display
}

Credits

TIMOTHY MWALA

TIMOTHY MWALA

15 projects • 11 followers
SmartHome Nerd

Comments