Microphone
Last updated
Last updated
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.
The microphone does not require any special code library.
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).
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).
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.
You can measure the amplitude of the sound in the environment using an analogRead()
statement.
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.