Hardware components | ||||||
![]() |
| × | 1 | |||
![]() |
| × | 1 | |||
| × | 1 | ||||
![]() |
| × | 1 | |||
![]() |
| × | 1 | |||
Software apps and online services | ||||||
![]() |
| |||||
![]() |
|
Arduino Sound Level MeterIn this video we make a sound level meter using an Ardiuino Una, a 1.3" OLED display and a microphone breakout board. The sketch samples the AC waveform of the microphone for 50ms and records the minimum and maximum values. This gives us the peak-to-peak value for the sound.Then we convert this value to a calibrated decibel numeric value and then map that value to create a bar graph.
connections :
OLED:
connect VCC to 5V.
connect GND to GND.
connect SCL to A5.
connect SDA to A4.
sound sensor :
connect VCC to 5V.
connect GND to GND.
connect A0 pin to A0.
//--------------------------------------------------------------------------------------------
// 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
}
Comments