Electret Mic Breakout Board Hookup Guide a learn.sparkfun.com tutorial
Available online at: http://sfe.io/t485
Introduction
Ready to add audio to your next project? The SparkFun Electret Microphone Breakout couples an Electret microphone (100Hz - 10kHz) with a 60x mic preamplifier to amplify the sounds of voice, claps, door knocks or any sounds loud enough to be picked up by a microcontroller’s analog to digital converter.
Image may be NSFW.
Clik here to view.
All in one tiny package!
The Electret Mic Breakout translates amplitude (not volume) by capturing sound waves between two conducting plates (one a vibrating diaphragm and the other fixed) in the microphone and converting them into electrical waves. These electrical signals are then amplified and picked up by your microcontroller’s ADC. In this tutorial, we will present two different projects to get you up and running with your next sound reactive project in a snap or a clap.
Materials Required
For this tutorial you will only need a few tools and components.
Recommended Reading
If you are not familiar or comfortable with the following concepts, we recommend reading through these before continuing on with the Electret Mic BOB Hookup Guide.
Hardware Overview
Hardware
The Electret Mic Breakout Board only has three pins: VCC, GND and AUD. You can power this device from 3.3V to 5V, so it is a great compliment to most microcontroller units. For the amplification, we used Texas Instruments OPA344 rail-to-rail precision amplifier to give you maximum output swing.
Image may be NSFW.
Clik here to view.
Schematic
The gain of the amplifier is set by R5/R4 which is approximately 82V/V. Simulation and testing puts the gain closer to 60V/V.
Image may be NSFW.
Clik here to view.
Click the image for a closer look.
Frequency Response
Image may be NSFW.
Clik here to view.
Click the image for a closer look.
What this tells you is that you will see the same gain across the frequency spectrum that is picked up by the mic (100Hz-10KHz). The input of the amplifier is biased at ½ VCC. The very small AC voltage output by the mic rides on the DC offset and gets amplified through the OPA344. The output from the “AUD” pin is also at ½ the supply voltage, so it can be connected directly to the ADC of microcontroller. In quiet conditions, the ADC will ideally read ½ the full scale or 512 on a 10-bit converter.
Example Circuit and Code
We are going to run through a very simple project to get you started lighting an LED on the RedBoard based on the ADC values picked up by your microcontroller.
This is the Knocker. An LED will light up when a knock is detected.
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
Make the Following Connections
Electret Mic BOB → RedBoard
- VCC → 5V
- GND → GND
- AUD → A0 (or any analog pin)
- LED+ → 330 ohm resistor → digital Pin 9
- LED- → GND
Image may be NSFW.
Clik here to view.
Open the Arduino IDE and create a new project.
language:c
/*
* The Circuit:
* Connect AUD to analog input 0
* Connect GND to GND
* Connect VCC to 3.3V (3.3V yields the best results)
*
* To adjust when the LED turns on based on audio input:
* Open up the serial com port (Top right hand corner of the Arduino IDE)
* It looks like a magnifying glass. Perform several experiments
* clapping, snapping, blowing, door slamming, knocking etc and see where the
* resting noise level is and where the loud noises are. Adjust the if statement
* according to your findings.
*
* You can also adjust how long you take samples for by updating the "SampleWindow"
*
* This code has been adapted from the
* Example Sound Level Sketch for the
* Adafruit Microphone Amplifier
*
*/
const int sampleWindow = 250; // Sample window width in mS (250 mS = 4Hz)
unsigned int knock;
int ledPin = 9;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop()
{
unsigned long start= millis(); // Start of sample window
unsigned int peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
// collect data for 250 miliseconds
while (millis() - start < sampleWindow)
{
knock = analogRead(0);
if (knock < 1024) //This is the max of the 10-bit ADC so this loop will include all readings
{
if (knock > signalMax)
{
signalMax = knock; // save just the max levels
}
else if (knock < signalMin)
{
signalMin = knock; // save just the min levels
}
}
}
peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
double volts = (peakToPeak * 3.3) / 1024; // convert to volts
Serial.println(volts);
if (volts >=1.0)
{
//turn on LED
digitalWrite(ledPin, HIGH);
delay(500);
Serial.println("Knock Knock");
}
else
{
//turn LED off
digitalWrite(ledPin, LOW);
}
}
Once you have loaded and run the program with your hardware hooked up, you should the LED attached to pin 9 on the RedBoard light up when you knock. Check out the WindBag Alert that uses the same code with just a few additional lines for an additional practice and project inspiration.
The Windbag Alert
Do you have a co-worker that talks too much? Do you want to alert that long-talker in your life of their offenses with the least unassuming desk ornament? Do you like literal translations of bad expressions? Then make yourself a Windbag Alert. This rude little piece of hardware will inflate a bag (one of my doggy doop-doop bags) with the air of your stolen silence.
Image may be NSFW.
Clik here to view.
Here the bag is deflated but the Electret Mic is listening. If ADC values are above a certain threshold for too long the bag will begin to inflate.
Image may be NSFW.
Clik here to view.
Here, someone has been talking for about 30 seconds. They have just a few seconds before the bag fully inflates. The fan kicking on usually stops them dead in their tracks.
Image may be NSFW.
Clik here to view.
But, some people, those people, don’t care. And the bag completely inflates. Alerting that person they are indeed the office Windbag.
Let’s break this thing down. This project is simply a continuation for the LED Blinking project from the previous section.
Hardware
I used a 12V computer fan, because that’s what I had laying around. You can use any fan, even disassemble a hand held cooling fan found at dollar stores. That should eliminate the need for a higher power supply and the fan (+) would be attached to 5V and the source (Emitter) of the transistor would be connected to GND on the Arduino.
Image may be NSFW.
Clik here to view.
I used an old SparkFun box as my project enclosure, which sits on my power supply on my workbench. I wrapped a plastic bag around the computer fan using electrical tape. There is about ½" of space between the box top and fan. I used standoffs there. Make sure there is enough air flow.
Software
language:c
const int sampleWindow = 250; // Sample window width in mS (250 mS = 4Hz)
unsigned int sample;
int Wind = 9;
void setup()
{
Serial.begin(9600);
pinMode(Wind, OUTPUT);
}
void loop()
{
unsigned long startMillis= millis(); // Start of sample window
unsigned int peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
// collect data for 1 second
while (millis() - startMillis < sampleWindow)
{
sample = analogRead(0);
if (sample < 1024) //This is the max of the 10-bit ADC so this loop will include all readings
{
if (sample > signalMax)
{
signalMax = sample; // save just the max levels
}
else if (sample < signalMin)
{
signalMin = sample; // save just the min levels
}
}
}
peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
double volts = (peakToPeak * 3.3) / 1024; // convert to volts
Serial.println(volts);
if (volts >=0.5)
{
//turn on FAN
digitalWrite(Wind, HIGH);
delay(1000);
digitalWrite(Wind, LOW);
delay(75);
digitalWrite(Wind, HIGH);
delay(75);
digitalWrite(Wind, LOW);
delay(75);
digitalWrite(Wind, HIGH);
delay(75);
digitalWrite(Wind, LOW);
delay(75);
digitalWrite(Wind, HIGH);
delay(75);
}
else
{
//turn FAN off
digitalWrite(Wind, LOW);
}
}
I suggest playing with the delays so you can finely tune your machine to suit your needs.
Resources and Going Further
A quick Google search for “Electret Mic Breakout” will get you going for any type of project you may want to create. Here are a few of my favorites from Instructables.
If you need to add sound to your next project but need something a little more sensitive, robust or a little more useful, I recommend the SparkFun Sound Detector.
Documents
For more audio fun, check out these other great SparkFun tutorials:
Build an Auduino Step Sequencer
WAV Trigger Hookup Guide V11
learn.sparkfun.com |CC BY-SA 3.0 | SparkFun Electronics | Niwot, Colorado