Rayan kiwan
Published © GPL3+

Arduino Sound meter

In this video we make a sound level meter using an Ardiuino Una, a 1.3" OLED display and a microphone breakout board.

IntermediateFull instructions provided5,107
Arduino Sound meter

Things used in this project

Story

Read more

Code

arduino oled sound meter

Arduino
 //--------------------------------------------------------------------------------------------
 //                                          LIBRARIES
 //--------------------------------------------------------------------------------------------
 
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);

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

 //--------------------------------------------------------------------------------------------
 //                                            SETUP
 //--------------------------------------------------------------------------------------------
 
void setup() 
{
   Serial.begin(9600);                                    //Serial comms for debugging
   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);                    //OLED display start
   display.display();                                     //show buffer
   display.clearDisplay();                                //clear buffer
   display.setTextSize(1);                                //Set text size to 1 (1-6)
   display.setTextColor(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 = 1024;                         //maximum value
 
                                                          // collect data for 50 mS
   while (millis() - startMillis < sampleWindow)
   {
      sample = analogRead(0);                             //get reading from microphone
      if (sample < 1024)                                  // 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,900,49.5,90);             //calibrate for deciBels
   display.setCursor(0,0);                                //cursor to upper left
   display.setTextSize(2);                                //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, WHITE);
    }
   display.drawRoundRect(0, 32, 120, 20, 6, WHITE);       //draw outline of bar graph
   int r = map(db,0,120,1,120);                           //set bar graph for width of screen
   display.fillRoundRect(1, 33, r, 18, 6, WHITE);         //draw bar graph with a width of r
   display.display();                                     //show all that we just wrote & drew
   display.clearDisplay();                                //clear the display
}

Credits

Rayan kiwan

Rayan kiwan

36 projects • 1 follower

Comments