REVERSE VALUES IN ELECTRET MICROPHONE

Well, I only started using Arduino a month ago so I am still pretty new to this.

We are trying to get some relays on/off using an electret mic as a sensor. It seems to be working perfectly if we do not involve arduino in the circuit but once connected to Arduino the values that we are getting they are reversed. I suppose I should get mid values when there's silence and very high and very low when there is sound. However I do get random low and high values(100-700) in silence and only high ones (>500) when there is sound. Therefore my lights flicker the whole time and stay on when there's sound. I am using this mic : http://www.freetronics.com/products/microphone-sound-input-module and a 12 V relay using external batteries. The lights are standard christmas lights.

The code used is the following: //SOUND Sensor

int sensorPin = 4; //Microphone Sensor Pin on analog 4 int Relay = 4; int sensorValue = 0;

void setup() { pinMode(Relay, OUTPUT); Serial.begin(9600); }

void loop() { // read the value from the sensor: sensorValue = analogRead(sensorPin); Serial.println(sensorValue);

if (sensorValue>500){ digitalWrite(Relay,HIGH); delay(150 ); } else { digitalWrite(Relay,LOW); } }

Any help will be greatly appreciated. We followed all relevant tutorials and all seem to have opposite results.

Which output are you using? There is a raw sound output and a SPL output. You probably want the SPL output. Most of those mics are pretty sensitive with a really good frequency range. It may be hearing things you don't.

If the mike is outputting ac waves, you probably want to rectify and filter it with a capacitor. Use a diode and a capacitor, and probably a high resistor parallel to the cap to bleed it off.

A circuit diagram would help us to help you to solve your problems.
Tom… :slight_smile:

To reverse the values simply swap the HIGH and LOW in the digital write statement. To understand why the post the schematic of how you drive your 12V relay from a 5V arduino pin and we can tell you.

SurferTim:
Which output are you using? There is a raw sound output and a SPL output.

I am using the SPL outout.

I will try this tonight thanks.

Grumpy_Mike:
To reverse the values simply swap the HIGH and LOW in the digital write statement.
To understand why the post the schematic of how you drive your 12V relay from a 5V arduino pin and we can tell you.

Reversing the values only drive the relay off/on instead of on/off doesn’t change my values.

Hi , if your schematic is correct you have the output of the arduino connected to the source of the FET, and the gate to GND.

It should be output of arduino 4 to gate of FET via a 1K resistor. The source of the FET connected to GND.

The wiring around the relay is not clear, it looks like you have moved the relay and not the associated wiring. The drain of the FET should be connected to one end of the relay coil, the other end of the coil to positive of your battery. You show 3V as your battery, yet say you are using 12V. I will assume that the 3V is a mistake and it is 12V. The motor should have one side connected to GND the other to the Normally Open contact of the relay, the COMmon of the relay needs to be connected to your 12V supply. Your diagram sort of shows the motor connected to the Normally Closed contact, this will explain why your output appears back to front. Hope this helps. Tom... :)

Thanks Tom! It’s the first time I am using Fritzing and I did a lot of mistakes. I attached some pictures that show where everything goes more clearly.

IMG_20130927_202335[1].jpg

Fine, thanks for the pics, I'm almost positived you have gate and source connections in the wrong positions. You have not said what the mosfet is but in general with the mosfet turned up so you can see the plasic pack and the tab is out the top, the pins are G D S, left to right. You have S D G left to right. So can you please check the specs and also tell me what the mosfet number is. Thanks Tom.... :)

It's the first time I am using Fritzing

Please make it your last time, Fritzing is rubbish for telling what you did to people who know anything.

Your photographs are way too big, and you don't post your code correctly, please read the how to use the forum sticky.

superlila:

Grumpy_Mike: To reverse the values simply swap the HIGH and LOW in the digital write statement.

Reversing the values only drive the relay off/on instead of on/off doesn't change my values.

That is what you asked for, I don't know what you mean by reversing your values if it is not what you meant. Try again please to explain what you want. The business with the FET is odd because you say the relay is working.

Thanks for your comments! My transistor number is TIP122G. I started from the basics again to understand better what I am doing. On the first image I have a normal led and works fine with the electret mic. On the second one I have a relay that also works quite ok although it goes on/off way too fast. I guess this is because the microphone is so sensitive. My goal is to make fairy lights blink the way one led light does. If there is a way to do it without using a relay that would be much easier. However, since they require 220 V I guess I need the relay anyways.

Problems start when I connect to Arduino. First of all, the light on my mic is always on even if there’s silence. Secondly, even when I use the basic code from Arduino Cookbook on detecting sound (Inkling) I get completely different values. I was supposed to get something between 0 and 1023 and I get 52000 to 120000. Also, even if I use the code from the freetronics website (http://www.freetronics.com/pages/microphone-sound-input-module-quickstart-guide#.Uke8xYanobg) in silence I get values between 40 and 700 and when I make any constant noise I get values around 500. These readings are all using the spl output.

To summarize, my questions are if there is a safe way to use fairy lights without using the relay and why does my microphone seem to be always on when I connect it to Arduino.

Hi TIP122G is a transistor, please look at the data sheet, google it, the pins are base , collector, emitter , left to right, you need a resistor in the base circuit. Both the fritzing diags you have supplied mean very little, there are no labels for any of the components. PLEASE give up fritzing, draw your circuit on paper and photograph or scan it, or use a cad program. Remember labels on circuit components. Do you have any knowledge of how to drive a transistor and relay? Tom... :) Please look at:

http://playground.arduino.cc/uploads/Learning/relays.pdf

I attached a project which is the closest thing I did in using transistors and relays. This uses timer to turn relays on/off. I want to use the exact same circuit but instead of a timer I want my microphone to control the relay. As I said before I can do it when the whole circuit is not connected to Arduino and is powered by 2 6V batteries for the relay and one battery for the mic, but once I connect it to the 5V, Ground, Analog input for the mic and Digital input for the relay/transistor of the Arduino, the relay turns on/off even without any sound. It might be the code, but since the microphone behaves strangely when connected to Arduino I think it is the microphone. The microphone is always on even when it is the only thing connected to Arduino.

Okay, I can see from that diag that you have the basics, but you are driving a BJT, NPN transistor not a MOSFET as in this diagram. So you need to put a 10K resistor between the BASE of the TIP122 and the output pin of the arduino.

Please tell me if you understand this and have done it, also checked the pin outs of the TIP122.

Also for the response you need you need to connect the motor wire to the Normally Open terminal of the relay and the motor supply wire to the Common terminal of the relay.

Did you look at the link I put in my previous Post?

Tom.. :)

I get completely different values. I was supposed to get something between 0 and 1023 and I get 52000 to 120000

That is quite simply impossible unless you have screwed up with your code. That is why we ask you to post the code you are using not a link to the code you think you are using. Normally the reading you will get with silence is around 512, so setting a threshold at this point is useless. It sounds like you are using the raw output of your sound sensor not the correct one.

TomGeorge:
So you need to put a 10K resistor between the BASE of the TIP122 and the output pin of the arduino.

Please tell me if you understand this and have done it, also checked the pin outs of the TIP122.

Also for the response you need you need to connect the motor wire to the Normally Open terminal of the relay and the motor supply wire to the Common terminal of the relay.

Did you look at the link I put in my previous Post?

Tom… :slight_smile:

Yes thanks Tom I figured out how the pins work and I put a resistor before plugging it in the arduino. However, the values I get are still pretty random.

Grumpy_Mike:
That is quite simply impossible unless you have screwed up with your code. That is why we ask you to post the code you are using not a link to the code you think you are using.
Normally the reading you will get with silence is around 512, so setting a threshold at this point is useless.
It sounds like you are using the raw output of your sound sensor not the correct one.

First code I used was the one from Arduino Cookbook. Without using a transistor between output and arduino the values I get from the MIC output are between 3,000 and 6,000 in silence and over 10,000 in noise. From the SPL output 50,000 to 85,000 in silence and they get low as 9,000 in noise. Using a 10K resistor between arduino analog and SPL output I get 50,000 and 100,000 in silence and they get as low as 6000 when there is noise. If I connect to the mic output it is between 3,000 to 6,000 in silence and over 10,000 when there’s noise again. This is the code without tinkering anything.The light remains on the whole time.

/*
microphone sketch

SparkFun breakout board for Electret Microphone is connected to analog pin 0
*/

const int ledPin = 13;            //the code will flash the LED in pin 13
const int middleValue = 512;      //the middle of the range of analog values
const int numberOfSamples = 128;  //how many readings will be taken each time

int sample;                       //the value read from microphone each time
long signal;                      //the reading once you have removed DC offset
long averageReading;              //the average of that loop of readings

long runningAverage=0;          //the running average of calculated values
const int averagedOver= 16;     //how quickly new values affect running average
                                //bigger numbers mean slower

const int threshold=400;        //at what level the light turns on

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  long sumOfSquares = 0;
  for (int i=0; i<numberOfSamples; i++) { //take many readings and average them
    sample = analogRead(0);               //take a reading
    signal = (sample - middleValue);      //work out its offset from the center
    signal *= signal;                     //square it to make all values positive
    sumOfSquares += signal;               //add to the total
  }
  averageReading = sumOfSquares/numberOfSamples;     //calculate running average
  runningAverage=(((averagedOver-1)*runningAverage)+averageReading)/averagedOver;

  if (runningAverage>threshold){         //is average more than the threshold ?
    digitalWrite(ledPin, HIGH);          //if it is turn on the LED
  }else{
    digitalWrite(ledPin, LOW);           //if it isn't turn the LED off
  }
  Serial.println(runningAverage);        //print the value so you can check it
}

If now using the mic output without the resistor I change the mid value to 5,000 and 6,000 for the threshold I get results below zero (-133,333,569) and the light does not flash. If I change to the SPL output put 50,000 for mid value and 60,000 for threshold and reverse LOW and HIGH I do get the light to turn on with the sound of my voice although it randomly blinks the whole time afterwards even in silence.

I do have the same issue when I use the code I posted in the first place. I can make the relay turn ON/OFF my issue was that the normal state should be either on or off but the microphone’s values were turning the relay on/off continuously and they kept on for longer when there was noise.

Problem seems to be solved somehow. I changed my relay with a solid state one,used a BD139 transistor and also I plugged in the microphone VCC into the 3.3 V instead of the 5V. Probably the problem is still there but the voltage is now too low to make the lights blink uncontrollably. It seems that the microphone was getting too much voltage from the arduino although when it was independent from it even with a 6 V battery it was working perfectly.