Sarah WorthingtonNoah RedfeairnEthan Croitoru
Published © GPL3+

Sound Detector

This project will teach you how to build a photon that collects sound levels using the Sparkfun Electronics Sound Detector.

BeginnerFull instructions provided3 hours2,850
Sound Detector

Things used in this project

Hardware components

SparkFun Sound Detector
×1
Photon
Particle Photon
×1
Jumper wires (generic)
Jumper wires (generic)
For this project you will only need 3 wires (ground, power, and envelope).
×1
Breadboard (generic)
Breadboard (generic)
×1

Hand tools and fabrication machines

Laser cutter (generic)
Laser cutter (generic)
Hot glue gun (generic)
Hot glue gun (generic)
Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Custom parts and enclosures

Photon Enclosure Adobe Illustrator Plan

This is a plan for the enclosure using Adobe Illustrator. The box uses T-slots so that it can be opened and closed when needed. The round hole on one of the sides is for the sound sensing microphone to stick out. The other rectangular hole is to allow the USB cable to reach the photon and the power source. We also engraved our contact information (removed in the photo) so that anyone can easily contact us with questions. The enclosure is 2x2.5x3.5 inches.

Sound Directing Cone

This is the laser cutting plan for the the cone that goes around the microphone. This is intended to block out the unwanted noise and focus sound waves onto the microphone

Schematics

Sketch of Enclosure

This drawing shows how the breadboard, photon, and sound sensor fit into the enclosure.

Fritzing Diagram

This photo shows how to wire the sound sensor, Fritzing did not have the sensor that we used so we just used a note as a reference. By soldering the wires to the sound sensors, the sensor can be moved outside of the enclosure. Connections are as follows: A0 on the photon connects to Gate on the sound sensor, 3V3 on the photon goes to VCC on the sensor, and the photon's GND to GND on the sensor.

Code

Sound Detetctor Code

C/C++
This code collects the average, minimum, and maximum sound level every minute. There are also additional lines that allow the data to be brought into google sheets using the Script Editor tool
const long sample_rate = 50; // time between samples (in miliseconds)
const int array_size = 1200; // 1000/50=20 * 60=1200
int snd_array[array_size] = {};
int snd_max, prev_max = 0;
int snd_min, prev_min = 4096;
float snd_avg = 2048;

const int blink_thresh = 2048;

unsigned long broadcast_interval = 3000;
unsigned long last_broadcast = 0;

void averageReading(int value) {

    // Shift all the values right by 1
    for(int i = array_size-1; i >= 1; i--) 
    {
        snd_array[i] = snd_array[i-1]; 
        if((snd_array[i] < snd_min) && (snd_array[i] != 0))
        {
            snd_min = snd_array[i];
            
        }
        if(snd_array[i] > snd_max)
        {
            snd_max = snd_array[i];
            
        }
    }

    snd_array[0] = value; 

    // Average array
    float avg_sum = 0; 
    int size = 0 ;
    for (int a=0; a <= array_size; a++) 
    {
        if(snd_array[a] > 0)
        {
            size++ ;
            avg_sum  = avg_sum + snd_array[a];
        }
    }
    snd_avg = avg_sum / size;
    
    Particle.variable("snd_avg",snd_avg); //pull to google sheets
    Particle.variable("snd_min",snd_min); 
    Particle.variable("snd_max",snd_max); 
}

void blinkMic(int reading) {
    if(reading > blink_thresh) {
        digitalWrite(D7, HIGH);
    } else {
        digitalWrite(D7, LOW);
    }
}


void checkBroadcast() {
    unsigned long now = millis();
    if((now - last_broadcast) > broadcast_interval) {
        Serial.print("Avg: "); Serial.println(snd_avg);
        Serial.print("Min: "); Serial.println(snd_min);
        Serial.print("Max: "); Serial.println(snd_max);
        snd_avg = 0;
        snd_min = 4096;
        snd_max = 0;
        snd_array[array_size] = {};
        last_broadcast = now;
    }
}

void setup() {
    Serial.begin(9600);
    pinMode(A0, INPUT); // mic AUD connected to Analog pin 0
    pinMode(D7, OUTPUT); // flash on-board LED
}

void loop() {
    int mic_reading = analogRead(0); //Serial.println(mic_reading);
    blinkMic(mic_reading);
    averageReading(mic_reading); 
    checkBroadcast();

    delay(sample_rate);
}

Google Script Editor Code

JavaScript
This code pulls the data from Particle into Google Sheets.
function collectCurrent() {
  "use strict";
  try
  {
    //Replace the device ID below with your Photon's unique device ID
    var deviceID = "2d0020001447353136383631"; //sound
    
    //Replace the access token below with your Photon's unique access token
    var accessToken = "86c90df22e2a316e11af127272bea47a793d905b"; //sound
    
    //Replace the value below with you group ID
    var groupID = 407;
    
    //Replace the room number below with location of the Photon
    var room = 345; //Ms.Brock
    
    var sheet = SpreadsheetApp.getActiveSheet();

    // Fetch the value of the testValue variable from the Spark Cloud API.
    // The name of your variable in Particle's cloud must match the variable in the URL below.
    var response = UrlFetchApp.fetch("https://api.spark.io/v1/devices/" + deviceID + "/snd_avg?access_token=" + accessToken);
    var jsonResponse = JSON.parse(response.getContentText());
    var avg = jsonResponse.result;

    var response2 = UrlFetchApp.fetch("https://api.spark.io/v1/devices/" + deviceID + "/snd_min?access_token=" + accessToken);
    var jsonResponse2 = JSON.parse(response2.getContentText());
    var min = jsonResponse2.result;    
    
    var response3 = UrlFetchApp.fetch("https://api.spark.io/v1/devices/" + deviceID + "/snd_max?access_token=" + accessToken);
    var jsonResponse3 = JSON.parse(response3.getContentText());
    var max = jsonResponse3.result;    


    // Create a timestamp.
    var timeStamp = new Date(); //sound

    // Append the timestamp and the temperature to the sheet.
    sheet.appendRow([timeStamp, groupID, room, min, max, avg]);
  } catch (e)
  {	
    // If something doesn't work, log it, then rethrow the error.
	//Logger.log(e.name + ": " + e.message);
	//throw (e);
  }
}

Credits

Sarah Worthington

Sarah Worthington

2 projects • 3 followers
Noah Redfeairn

Noah Redfeairn

-1 projects • 0 followers
Ethan Croitoru

Ethan Croitoru

-1 projects • 0 followers

Comments