聲音分貝計 sound level meter

[材料]

Arduino主板 x 1個

OLed x1個

聲音分貝計 Gravity SEN0232 x 1個 

[聲音分貝計 Gravity SEN0232腳位]

藍線 -->  arduino  pin A1

紅線  -->  arduino 5V

黑線  -->  arduino GND

[OLed 腳位]

VCC --> arduino 3.3V 或 5V

GND --> arduino GND

SCL --> arduino A5 

SDA  --> arduino A4

#include<Wire.h>

#include<Adafruit_GFX.h>

#include<Adafruit_SSD1306.h>

#define CREEN_WIDTH 128     //設定OLED螢幕的寬度像素

#define CREEN_HEIGHT 64     //設定OLED螢幕的寬度像素

#define OLED_RESET -1       //Reset pin如果OLED上沒有RESET腳位,將它設置為-1


Adafruit_SSD1306 display(CREEN_WIDTH, CREEN_HEIGHT, &Wire, OLED_RESET);     //OLED顯示器使用2C連線並宣告名為display物件


bool OLEDStatus = true;


#define SoundSensorPin A1  //this pin read the analog voltage from the sound level meter

#define VREF  5.0  //voltage on AREF pin,default:operating voltage

unsigned long begin_time = 0;


void setup()

{

  Serial.begin(115200);

  display.begin(SSD1306_SWITCHCAPVCC,0x3c);

}


void loop()

{

  float voltageValue, dbValue;

  voltageValue = analogRead(SoundSensorPin) / 1024.0 * VREF;

  dbValue = voltageValue * 50.0;  //convert voltage to decibel value

  Serial.print(dbValue, 1);

  Serial.println(" dBA");


  display.clearDisplay();   //清除緩衝區資料


  display.setCursor(0, 0);  //設定起始點位置(0,0)

  display.setTextSize(1);   //設定文字尺寸為1,1:6x8,2:12x16,3:18x24...etc

  display.setTextColor(WHITE);          //白字

  display.println("Sound level meter: ");

  display.println();

  display.print("Real time: ");

  display.print(dbValue, 1);

  display.println(" dBA");


  float max1, max2;

  max1 = dbValue;

  if (max1 >= max2) {

    max2 = max1;

    begin_time = millis();

  }

  if (millis() - begin_time > 5000) {

    max2 = max1;

  }

  Serial.println(max1);

  Serial.print(max2);

  Serial.println(begin_time);

  Serial.println(millis());


  display.println();

  display.print("Max  (5s): ");

  display.print(max2, 1);

  display.println(" dBA");


  display.fillRect(0, 50, max1-20, 6, WHITE);


  display.display();

  display.clearDisplay();

  //delay(125);

}