Basic Threshold Switch from a Mic

The Electret speaker breakout board from Sparkfun has been super popular in class so far.

The thing is that when you finally get it wired up, it outputs what appear to be random number pulses. In waves. These are the amplitude wave of the sounds near the microphone, not an error. They can themselves be used for many things, but will not tell you if, for example, someone is talking TO the critter, just the background levels. Terrible! We need a threshold to tell us when someone’s surpassed the normal background volume instead.

Much like “face recognition,” “hearing” speech is tough. This is simple. This creates a threshold switch from a microphone. It will tell you when the sound levels nearby change to a wider range than “normal.” It requires some calibration, and some simple math. If you are outputting to a nearby speaker, it may not work very well; it will hear the speaker, as well as yourself. Better math would fix that issue. This is a simpler switch than that.

electret breakout wiring
To wire it, hook 5v to a 10k pulldown resistor, ground to ground, and information to A1* as in the diagram. The datasheet says the thing needs 1.5v in, and most sources say a 5k will do you, but a 5k gives you a big fat wave with numbers between 200 and 400 – it looks super random. The 10K will compress your business like no nevermind.

*NB: I prefer A1 to A0 because A0 sometimes acts weird, and code occasionally interprets a “0” as a null value. This is important with analog inputs.

Once hooked up, here is your code:


Electret Mic Threshold Reader
Alex Leitch

Oct 31 2012

Appropriate for All Ages, 2006, uses a much-simplified version of this circuit.
That one ran on a similar mic circuit, picked up for $3 in a spare halloween “spooky robots” pile.
This is better because you can calibrate it, that was better because you didn’t have to. Also, it cost $3, not $30.


const int pinSpeaker = 3;

int valMic;
int valMicRange;
int valMicLast;

int iHearYou; //set threshold for “being talked to”

void setup() {

void loop() {

valMic = analogRead(pinMic); // every loop read the mic

valMicRange = abs(valMic – valMicLast);
// take an absolute value of current and last value of amplitude wave for threshold

// Find out what the absolute value of difference is – this will make a pretty visualizer out of your thresholds

if (valMicRange > iHearYou) { //do something if threshold is passed

else { // if not being talked to, what to do?

valMicLast=valMic; // hang onto that mic reading


Here is where the exhortation to do Amazing Things with this information comes in! But no. I am not going to lie to you: working with sound is tough and the sort of thing [redacted judgemental comment] (hi there, GearSlutz) work with. Sound is a wave, which is to say a four-dimensional transform, waves are a challenge if what you really want are robots. Sound becomes useful as a switch when, using thresholds maybe, you can transform it from a wave to a point, or at least a baseline, which is what this circuit does. Audio obsession knows no upward bounds. That message board is maybe the best review board for people who are really into analog inputs, though, and I think you may find something of use there.

Appropos of nothing, The Simpsons. Happy Hallowe’en.

Comments are closed.