CxD Archive
Internet of Things Project
Internet of Things Project
  • IoT Project Background
  • IoT Project Challenge
  • IoT Project Outline
    • 1-1 Smartphone Research
    • 1-2 Smart Thermostat Research
    • 1-3 Smart Device Research
    • 1-4 Introduction to Photon Kit
    • 1-5 Build Smart Lightbulb
    • 1-6 Generate Project Ideas
    • 1-7 Build Smart Thermostat
    • 2-1 Evaluate Project Ideas
    • 2-2 Plan User Research
    • 2-3 Compile Research Data
    • 2-4 Construct Personas
    • 2-5 Construct Journey Map
    • 2-6 Define Value Proposition
    • 2-7 Define Design Requirements
    • 3-1 Generate Solution Ideas
    • 3-2 Evaluate and Refine Solution Ideas
    • 3-3 Create System Model of Solution
    • 3-4 Create Wireflow for Web App
    • 3-5 Create Storyboard for Solution
    • 3-6 Evaluate Solution Design
    • 3-7 Present Project Proposal
    • 4-1 Track Progress and Issues
    • 4-2 Build Smart Device
    • 4-3 Create Photon App
    • 4-4 Create Web App
    • 4-5 Conduct Integration Testing
    • 4-6 Create Product Demo Video
    • 4-7 Create Product Marketing Website
    • 5-1 Evaluate Smart Device and Web App
    • 5-2 Evaluate Product Marketing Website
    • 5-3 Improve Solution Based on Evaluations
    • 6-1 Create Project Poster
    • 6-2 Present Project to Public
    • 6-3 Write Personal Reflection
  • References for Wiring & Coding
    • Creating Circuits
    • Wiring Programming Language
    • Particle Cloud
    • Hello World App for Photon
    • LED Light
    • Push Button
    • Micro OLED Display
    • Humidity and Temp Sensor
    • Speaker
    • Servo Motor
    • Motion Sensor
    • Magnetic Switch
    • Potentiometer
    • Photocell
    • Moisture Sensor
    • Accelerometer
    • RFID Reader
    • Fingerprint Scanner
    • GPS Receiver
    • Camera
    • Microphone
    • Force Sensitive Resistor
    • Keypad
    • Continuous Rotation Servo Motor
  • Glossary
  • Updated Project Guidebook
  • Updated Code Guidebok
Powered by GitBook
On this page
  • Code for Microphone
  • Library
  • Global Variables
  • setup( ) function
  • Measuring Sound Level
Export as PDF
  1. References for Wiring & Coding

Microphone

PreviousCameraNextForce Sensitive Resistor

Last updated 6 years ago

The can be used to detect sound levels in the nearby environment from 100Hz to 10KHz (which is most of the human hearing range).

Microphone

Photon Pin

AUD - Data

any analog I/O pin

GND - Ground

GND

VCC - Power

3.3V

IMPORTANT: The data wire must connect to an analog I/O pin, such as: A0, A1, A2, A3, A4, A5.

Code for Microphone

Library

The microphone does not require any special code library.

Global Variables

In the global variables, you should declare which pin is being used as the microphone data pin. The example below declares a variable called "micPin" (but you could use a different variable name).

// if necessary, change data pin number to match your wiring
int micPin = A0;

You will also want to declare an integer variable to store the sound level read by the microphone. This will make it easier to do something based on the data. The example below declares a variable called "soundLevel" (but you could use a different variable name).

// global variable
int soundLevel;

setup( ) function

There isn't any code that you need to include in the setup() function for the microphone.

IMPORTANT: Do not set a pin mode for the microphone.

Measuring Sound Level

You can measure the amplitude of the sound in the environment using an analogRead() statement.

int sample;

sample = analogRead(micPin);

The measurement will be a value ranging from 0-4095. Sounds with a higher amplitude (louder) will have a higher measurement.

However, sound waves have peaks and valleys, so a single measurement is not necessarily an accurate measurement of the sound environment. So it is better to take multiple measurements over a brief period of time (fraction of second) to get the "peak-to-peak" amplitude of the sound level.

The code below shows a custom function called listenMic() that samples the sound over a window of 50 milliseconds. The custom function is called within the loop() function. You would need to add code within the loop() to do something based on the sound level.

void loop() {

    listenMic();

    // add code to do something based on value of soundLevel
}

// CUSTOM FUNCTION - samples sound over window to get peak-to-peak amplitude
void listenMic() {

    // set amount of time for sample window (in milliseconds)
    int sampleWindow = 50;
    int sample;

    // set start time to current time (in milliseconds)
    long startTime = millis();

    // analog input on Photon measures value from 0-4095
    int signalMax = 0;
    int signalMin = 4095;

    // while difference between current time and start time is within sample window
    while (millis() - startTime < sampleWindow) {

        sample = analogRead(micPin);
        if (sample < 4095) {  
            if (sample > signalMax) {
                // save max sound level detected
                signalMax = sample;
            }
            else if (sample < signalMin) {
                // save min sound level detected
                signalMin = sample;
            }
        }
    }
    // peak to peak amplitude of sound during sample window
    soundLevel = signalMax - signalMin;
}
electret microphone
Microphone