Microphone

The electret microphone 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;
}

Last updated