Introduction: UCL - Embedded - Noise Meter

This is a school project in embedded programming.

I have chosen to to make a little box that will measure noise. The idea was to figure out if the working area is too loud to work in, and IF it is, you should take precautions.

Movie, in mp4, of the object you can find here belowe.

Step 1: Step 1: the Project

The box has a small microphone to measure the sound. It then shows the sound level with LED-lamps and on an LCD display how high it is. The display will show the current level and the previous level

Step 2: Step 2: Parts

1 - Arduino MEGA 2560

1 - Breadboard

11 - 220 ohm resistors

1 - potentiometer

1 - LCD screen

1 - Sound Module

11 - LEDs (blue, red, yellow and green)

jumpwires

9v battery

Step 3: Step 3: Wiring

Step 4: Step 4: IO List

Step 5: Step 5: Code

//Include LCD library
#include <liquidcrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int firstLED = 40;
const int lastLED = 50;

const int STATE_GREEN = 40;
const int STATE_YELLOW = 45;
const int STATE_RED = 48;
const int STATE_BLUE = 50;
const String STATE_GREEN_TEXT = "LOW";
const String STATE_YELLOW_TEXT = "MEDIUM";
const String STATE_RED_TEXT = "HIGH";
const String STATE_BLUE_TEXT = "WAY TOO HIGH!";

int previousState = 0;

unsigned long previousMillis = 0;     // will store last time display was updated
const long interval = 3000;           // interval at which to update display (milliseconds)

String line0 = "";
String line1 = "";

void setup() {
  Serial.begin(9600);

  //sound sensor
  pinMode(A0, INPUT);

  //led pins
  for(int i = firstLED; i <= lastLED; i++) { 
    pinMode(i, OUTPUT);
  }

  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
}

void loop() {
  //storing the volume value
  int volume = analogRead(A0);

  // mapping volume value to make it easier to turn LEDs on (830 instead of 1023)
  int volumeMapped = map(volume, 145, 160, firstLED, lastLED);

  // we are going through all pins where we have LEDs and checking if the volume
  // is bigger than pin number (that's why we are maping the volume)
  for(int i = firstLED; i <= lastLED; i++) {
    if (volumeMapped >= i) {
      digitalWrite(i, HIGH); //if it is bigger we can turn on the LED
    } else {
      digitalWrite(i, LOW); //if it is smaller we can turn the LED off
    }
  }

  // store the current time;
  unsigned long currentMillis = millis();

  // store the current state (GREEN, YELLOW, RED or BLUE)
  int currentState = mapVolumeToState(volumeMapped);

  // check to see if the current state is "higher" than the last state;
  // if so, update the display immediately
  if (currentState > previousState) {
    // save the last time you updated the display
    previousMillis = currentMillis;

    displayState(currentState);
    previousState = currentState;
  }

  // check to see if it's time to update the display; that is, if the difference
  // between the current time and last time you updated the display is bigger than
  // the interval at which you want to update the display.
  if (currentMillis - previousMillis >= interval) {
    // update only if the current state is "lower" than the last state
    if (currentState < previousState) {
      // save the last time you updated the display
      previousMillis = currentMillis;

      displayState(currentState);
      previousState = currentState;
    }
  }
}

void displayState(int state) {
  line1 = line0;
  if (state == STATE_GREEN) {
    line0 = STATE_GREEN_TEXT;
  }
  if (state == STATE_YELLOW) {
    line0 = STATE_YELLOW_TEXT;
  }
  if (state == STATE_RED) {
    line0 = STATE_RED_TEXT;
  }
  if (state == STATE_BLUE) {
    line0 = STATE_BLUE_TEXT;
  }
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(line0);
  lcd.setCursor(0, 1);
  lcd.print(line1);
}

int mapVolumeToState(int volume) {
  if (volume >= STATE_BLUE) {
    return STATE_BLUE;
  }
  if (volume >= STATE_RED) {
    return STATE_RED;
  }
  if (volume >= STATE_YELLOW) {
    return STATE_YELLOW;
  }
  return STATE_GREEN;
}

Step 6: Step 6: the BOX

Step 7: Step 7: Evaluation

The box was not as easy to make as I thought. The microphone is hard to calibrate and the led lights are easy to brake. The wires were far too long, so it was difficult to fit it all in the box in the right places. I have learned a lot, especially of my mistakes! ;-)