Thursday, January 13, 2011

Microphone on Arduino

Today I connected a microphone mini board from sparkfun to the Arduino. After soldering on some connector pins you have three connections. Vcc, which works from 2.7V up to 5.5V, GND and the Signal pin. I used 5V for Vcc. I used an additional 4.7kΩ resistor to pull up the ADC value read from the analog port of the Arduino. You might not need it though. As an indicator for signal strength / loudness, I used three LEDs which were triggered if a certain threshold is passed. So it is kind of an equalizer.

Here is the basic setup:


The code is pretty easy. The only thing you might change are the threshold values, because your background noise might differ.
int audioPort = 0;
int val;
int greenLed = 7;
int yellowLed = 4;
int redLed = 2;
int lowThreshold = 780;
int mediumThreshold = 890;
int highThreshold = 1000;

void setup() {
  pinMode(greenLed, OUTPUT);
  pinMode(yellowLed, OUTPUT);
  pinMode(redLed, OUTPUT); 
}

void loop() {
  val = analogRead(audioPort);
  if(val > highThreshold) {
    digitalWrite(redLed, HIGH);
    delay(10);             
    digitalWrite(redLed, LOW);
  } else if(val > mediumThreshold) {
    digitalWrite(yellowLed, HIGH);
    delay(10);             
    digitalWrite(yellowLed, LOW);
  } else if(val > lowThreshold) {
    digitalWrite(greenLed, HIGH);
    delay(10);             
    digitalWrite(greenLed, LOW);
  }
}
I wanted to use one of my favorite songs to demonstrate this experiment, but since I don't want to get into any trouble with the RIAA or the GEMA, I used a recording of me playing my dobro.

video

7 comments:

  1. I just "Pinned" this blog entry on Pintrest.com and I'm wondering how you feel about it possibly violating your copyright etc. I don't see a copyright on your site - and I'm willing to bet you posted this in the hope of helping other arduino folks. But I recently heard that the copyright + pintrest debate is going on and me pinning your stuff might not be 100% legal.

    ReplyDelete
    Replies
    1. Hi M@,

      it's no problem at all. Everything I publish on this Blog is licensed under Creative Commons License 3.0. So you can reshare it with proper attribution. Just link to the blog post or the blog itself or mention my name that's all. I appreciate your concern and I am glad that there are people out there that do care about this stuff. I try my best to conform to copyright and attribution in my blog and it is good to see that others do this as well. If you scroll to the bottom of the blog you see the CC license remark ("CC Licensed by Mario Böhmer"). It's important that we share our work so that we all can benefit from it. Thanks for your appreciation...have fun making stuff!

      Regards,
      Mario

      Delete
  2. how do you set the sampling rate for analog input?

    ReplyDelete
    Replies
    1. I didn't care about a certain sampling rate. Due to my delays between LED flashes in the loop, the sampling rate is rather low.
      If you want to improve the sampling rate you might want to try using interrupts and timers (http://arduino.cc/playground/Code/Interrupts) or even the new dedicated concurrency language (http://concurrency.cc/). I haven't tried it though.

      Delete
  3. What sort of microphone did you use for this?

    ReplyDelete
  4. hello!! Very interesting discussion glad that I came across such informative post.
    yeti usb microphone

    ReplyDelete