Little electret mic preamp

Russ said he had trouble using a bare electret mic as an input device in his Arduino 201 sensors class.  I hope to help by making up a couple of simple mic preamp boards.  There was an initial prototype, and a short ‘production’ run with a better layout, allowing a smaller board and putting the header pins closer to the edge so they don’t obscure as much of the breadboard.  I stuck a couple of the mics pulled out of the Sansa recorders on to try them out.  I also played with code ’til I got a decent one-LED PWM “applause meter”.

It’s a very simple one transistor amplifier, and the output still rides on a DC offset, just like with a standalone electret mic.  That was mostly so the students would have to deal with the DC offset.  Overall gain is about 100.  With one sample board, the analogRead() DC offset is about 320 (of 1024) and a loud noise gives a peak around 650 higher than that, so it’s a pretty good match for the Arduino’s A/D range.

I tried a couple of approaches in the code to automatically remove the DC offset and “rectify” the signal.  Final result was running batches of 600 reads (about 70 ms) and keeping both the max reading and the average over that time.  With that many samples  effectively at random times within the audio waveform, statistically we’re pretty likely to hit close to at least one near-peak reading each run – and it seems to work pretty well in practice.  Students can start with a simple version of the code ( loop() is only 9 lines).  There’s also a fast attack/slow release feature in an independent, additional block of code.  The code is in the W88 shared dropbox under Arduino201ScratchSpace.

While the target use case is on a breadboard, since I’m lazy and wanted to try it out on a bare Arduino, I put the 3 header pins in A0,A1, A2.  I used one as the obvious analog input, and the others as digital outputs, one constantly putting out HIGH, the other LOW to provide approximately +5 and ground.  Worked fine.  It responds to normal conversation maybe 2 feet away from the mic with a visible brightening of the LED.  A handclap shows a bright attack/release.  I hope Russ finds the boards useful.

This entry was posted in Miscellaneous, Workshop 88 Stuff. Bookmark the permalink.

5 Responses to Little electret mic preamp

  1. Gabriel says:

    Hello! Cute post !!
    Have you tried decouple the CC with a capacitor in series and rectify the negative half cycle with a diode bridge? Is it a good idea?

    • Jim says:

      Hi Gabriel,

      Thanks for your comments! Yes, you could certainly decouple the output (although I wanted the students to have to deal with the DC offset).

      I think the problem with rectifying the output with a diode is that the forward voltage drop of the (silicon) diode would mean that signal swings between 0 and around -0.6V would be invisible. Using a Schottky diode would drop that by half, but the problem would still be there. Using what I think is called a “precision” rectifier – an opamp with a diode in the feedback path, essentially eliminating the forward voltage problem should work fine, but was more complex than I wanted for this quick and dirty board.

      Jim

  2. Nieva says:

    Hello, could you share the code? Thank you!

    • Jim says:

      Hi Juan,

      I found one of the old mics and tried it out. There are 2 bits of code below, but the second doesn’t seem to work very well. The first one seems OK.

      Good luck!

      Jim

      • Jim says:

        I can’t believe I’m writing this: I removed the code from the reply above BECAUSE !@#$%^& WORDPRESS CORRUPTED IT! It failed to properly URI-encode a “< " in the code, causing it (and a nearby ">“) to be interpreted as an unknown tag, which caused browsers to ignore a bunch of lines of code! WTF?!

        I played with the code some and made an improved version. It has to be tuned depending on the power supply voltage you’re using, and possibly the color of the LED. I had a lot of noise problems powering it from USB, but it worked great from a Li-ion cell. (Even though that’s marginally out of spec for a 328P @16 MHz.) Here are the 3 versions of code, safe from WordPress’ corruption: Original, Improved, Applause meter that doesn’t seem to work.

        Jim

Leave a Reply

Your email address will not be published. Required fields are marked *