---
title: "Noise Pollution Monitor"
url: "https://maker.wiznet.io/irina/projects/Noise-Pollution-Monitor/"
markdown_url: "https://maker.wiznet.io/irina/projects/Noise-Pollution-Monitor/md"
type: "UCC: User Created Content"
author: "irina"
author_url: "https://maker.wiznet.io/irina/"
original_author: "irina"
original_url: "https://www.hackster.io/thomasabeyta/noise-pollution-monitor-b14e80"
published: "2022-03-29"
language: "en"
hardware: ["Teensy 3.1", "Bitcraze Micro SD card deck", "Itead W5100 Ethernet Network Module", "Solderless Breadboard Half Size", "SparkFun Electret Microphone Breakout", "Rotary Encoder with Push-Button"]
likes: 0
views: 142
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# Noise Pollution Monitor

> Noise Pollution Monitor

Original author: irina (source: https://www.hackster.io/thomasabeyta/noise-pollution-monitor-b14e80)

## Components

- **Teensy 3.1** x 1 ([docs](https://www.pjrc.com/teensy/teensy31.html))
- **Bitcraze Micro SD card deck** x 1
- **Itead W5100 Ethernet Network Module** x 1
- **Solderless Breadboard Half Size** x 1
- **SparkFun Electret Microphone Breakout** x 1 ([docs](https://learn.sparkfun.com/tutorials/electret-mic-breakout-board-hookup-guide?_ga=2.191480553.278798874.1657068782-1842012612.1651468433))
- **Rotary Encoder with Push-Button** x 1
- Software: **Arduino IDE** ([docs](https://www.arduino.cc/en/software))

## Article

## 3D Printing

<https://hacksterio.s3.amazonaws.com/uploads/attachments/1419011/audio_pollution_box-2_MQ3UrRR08V.stl>

## Fritzing

![](https://maker.wiznet.io/uploads/2022/03/screen_shot_2022-03-11_at_2_34_16_pm_abg5h6ZgSK-245x300.png)

## Code

`include #include #include #include #include #include #include #include #include #include ` #define TIME_HEADER "T" // Header tag for serial time sync message #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels #define OLED_RESET 4// reset pin # #define SCREEN_ADDRESS 0x3C /// See datasheet for Address; for 128x64 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); File dataFile; const int PIN15 = 15; const int PIN17 = 17; const int chipSelect = 4; const int ANALOGPIN = 20; bool status; int micVolume; int dbLevel; int currentTime; int lastSecond; int position; int n ; int i; int oldP; int thresHold; char filename[13]; int incident; int hr; int mn; int sc; Encoder myEnc(PIN15, PIN17); void setup() { myEnc.write(700); setSyncProvider(getTeensy3Time); // set the Time library to use Teensy 3.0's RTC to keep time Serial.begin(9600); while (!Serial); // Wait for Arduino Serial Monitor to open delay(100); pinMode(10, OUTPUT); digitalWrite(10, HIGH); pinMode(4, OUTPUT); digitalWrite(4, HIGH); Ethernet.begin(mac); //printIP(); Serial.printf("LinkStatus: %i n", Ethernet.linkStatus()); if (timeStatus() != timeSet) { Serial.println("Unable to sync with the RTC"); } else { Serial.println("RTC has set the system time"); //initializing time } Serial.printf("Initializing SD card..."); //monitor feedback/initializing SD pinMode(chipSelect, OUTPUT); //sets pin ready to write to SD pinMode(ANALOGPIN, INPUT); //gets pin ready to read MIC digitalWrite(chipSelect, HIGH); //sets the pin to high as on status = SD.begin(chipSelect); if (!status) { // if status is false Serial.printf("Card failed, or not presentn"); //while (true); // pause the code indefinately } else { Serial.printf("card initialized.n"); //checks the SD status and initialize } if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { } display.display(); delay(4000); //pause for 4 seconds //clear the buffer display.clearDisplay(); display.setRotation(0); } void loop() { micVolume = analogRead(ANALOGPIN); //interger for audio thresHold = myEnc.read(); if (Serial.available()) { time_t t = processSyncMessage(); if (t != 0) { Teensy3Clock.set(t); // set the RTC setTime(t); } } if (micVolume >= thresHold) { setHue(5, true, HueBlue, 200, 255); if ((currentTime - lastSecond) > 250) { setHue(5, true, HueOrange, 200, 255); lastSecond = millis(); } } if (micVolume >= thresHold) { oled(); incident++; sprintf(filename, "data%04i.csv", incident); hr = hour(); mn = minute(); sc = second(); int startTime; sprintf(filename,"TM%02i%02i%02i.csv",hr,mn,sc); while (micVolume >= thresHold) { writeToSD(); Serial.printf(" above 45db:%in", micVolume); micVolume = analogRead(ANALOGPIN); switchON(1); } } oled(); } //void printIP() { // Serial.printf("My IP address: "); // for (byte thisByte = 0; thisByte &lt; 3; thisByte++) { // Serial.printf("%i.",Ethernet.localIP()[thisByte]); // } // Serial.printf("%in",Ethernet.localIP()[3]); //} void oled(void) { display.clearDisplay(); if ((currentTime - lastSecond) > 1000) { display.setTextSize(1); //draws 2x scale text display.setTextColor(SSD1306_WHITE); display.setCursor(10, 0); display.printf("DB level: %in Thresold: %in", micVolume, thresHold); display.display(); //shows the initial text lastSecond = millis(); display.clearDisplay(); } } void writeToSD() { dataFile = SD.open(filename, FILE_WRITE); currentTime = hr; currentTime = millis(); if (dataFile) { dataFile.printf("%i n", micVolume); dataFile.close(); Serial.printf("%in", micVolume); } else { Serial.printf("loud.csv n"); // if the file is available, write to it: } return; } void readFromSD() { dataFile = SD.open("loud.csv"); if (dataFile) { Serial.printf("loud.csv: n"); // re-open the file for reading: // read from the file until there's nothing else in it: while (dataFile.available()) { Serial.write(dataFile.read()); } dataFile.close(); } else { Serial.printf("error opening micVolume.csvm n"); } return; } void digitalClockDisplay() { // digital clock display of the time Serial.print(hour()); printDigits(minute()); printDigits(second()); Serial.print(" "); Serial.print(day()); Serial.print(" "); Serial.print(month()); Serial.print(" "); Serial.print(year()); Serial.println(); } time_t getTeensy3Time() { return Teensy3Clock.get(); } /* code to process time sync messages from the serial port */ unsigned long processSyncMessage() { unsigned long pctime = 0L; const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013 if (Serial.find(TIME_HEADER)) { pctime = Serial.parseInt(); return pctime; if ( pctime &lt; DEFAULT_TIME) { // check the value is a valid time (greater than Jan 1 2013) pctime = 0L; // return 0 to indicate that the time is not valid } } return pctime; } void printDigits(int digits) { // utility function for digital clock display: prints preceding colon and leading 0 Serial.print(":"); if (digits &lt; 10) Serial.print('0'); Serial.print(digits); }

---

Source: https://maker.wiznet.io/irina/projects/Noise-Pollution-Monitor/
