2

I am very new to this community. My first project with Arduino was using a LDR (light dependent resistor) in a circuit like this:

enter image description here

The input I got was as expected between 0 - 1023.

I then tried a similar project this time using a sound sensor like this:

enter image description here

My circuit is as follows:

  1. A0 on mic to A0 on Arduino
  2. GND to GND
  3. +ve to 5V
  4. LED along with resistor to digital pin 9 on Arduino

My program is as follows:

const int analogInPin = A0; 
int sensorValue = 0;            

void setup() {
  pinMode(analogInPin, INPUT);
  pinMode(9, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  sensorValue = analogRead(analogInPin);
  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\n");
  if(sensorValue > 900)
    digitalWrite(9,HIGH);
  else
  digitalWrite(9,LOW);
}

The problem is the serial monitor gives a weird reading, randomly giving 1023 and 0 after regular intervals.

sensor = 124
sensor = 0
sensor = 1023
sensor = 39
sensor = 352
sensor = 1023
sensor = 142
sensor = 0
sensor = 1023
sensor = 44
sensor = 338
sensor = 1023
sensor = 163
sensor = 0
...
...

I can't figure out the problem myself so I came here.

  • 1
    Do you know what a sound wave looks like? – Majenko Oct 14 '16 at 21:33
  • i understand it is made up of rarefactions and compressions . what i was expecting this device to do was show approx loudness though(intensity if i am correct) . – 95_96 Oct 14 '16 at 21:39
  • I mean electrically. When you view it on an oscilloscope, or as a WAV or MP3 file in, say, audacity. It's a long sequence of discrete voltages at different points in time. That is what you are seeing. Discrete voltages at different points in time. That "module" is a microphone and amplifier. Nothing more. It is up to you to process those values to do something meaningful with them. – Majenko Oct 14 '16 at 21:41
  • could you recommend something to read . i was expecting to light the led above a certain loudness. i can see though the similarities between this and a wave ( periodical 1023 and 0s ).Also what am i getting as the input then ?? – 95_96 Oct 14 '16 at 21:52
  • 1
    Another thing: you can't print to serial whilst at the same time monitor the audio. You are only seeing very short snatches of sound in between your serial prints. Like if you stick your fingers in your ears, then pull them out and put them back in again very quickly. You just get short snatches of sound that make no sense. – Majenko Oct 14 '16 at 21:55
2

I plotted the values you got in a spreadsheet:

Microphone input

You must admit that looks a bit like a sound wave! If you want to know if it is a soft sound or a loud sound you really need to see the maximum values (1023 in your case). If the maximum was 512 then clearly the incoming sound isn't as loud.

An alternative, if you just want loudness would be to filter the incoming signal, eg.

schematic

simulate this circuit – Schematic created using CircuitLab

This would smooth the incoming voltage so that consecutive readings would be in a narrower range.


Warning: Judging by the graph, you may be getting negative input into A0. The Arduino pins are not designed for negative voltage. You may want to use a diode to eliminate the negative component.

  • Thanks for the analysis...it felt like a wave to me too. I am afraid though that the maximum is always 1023 and lowest 0 no matter how loud or soft the sound is ,therefore I can't really check the loudness by measuring the peak. Reading through other answers and comments it feels like I should have been expecting a wave like input (am I right in saying it?) .Does that mean the microphone atm is too sensitive(therefore I am always getting a peak of 1023). – 95_96 Oct 15 '16 at 2:26
  • Possibly there is a connection problem and you are getting random values. Or you might be right about it being sensitive. I would be measuring with an oscilloscope to see what is really happening. – Nick Gammon Oct 15 '16 at 2:32
  • This is severely mistaken. First, the data you plot is probably severely undersampled, given the serial printing at 9600 baud in the sampling loop. Next, it is as likely AC power line hum as intended audio. Your comment on maximum values is quite mistaken as it ignores that the data is an AC signal centered at some intermediate value, to measure a meaningful maximum the DC component must be removed and the absolute value taken. Your lowpass filter proposal is the opposite of what would be useful - it removes the AC that should be analyzed, and provides the DC part that should be ignored. – Chris Stratton Oct 15 '16 at 15:55
  • Chris is right about the sampling rate. A higher baud rate may help a bit (and remove the string "sensor = "). He may also be right about the hum - you would need to know when the samples were taken to know what frequency that signal is. – Nick Gammon Oct 15 '16 at 21:32
  • This is severely mistaken. - no doubt the OP would be grateful if you would post an answer showing how to go about solving his problem. – Nick Gammon Oct 16 '16 at 4:22
1

Based on your code, it looks like you are trying to use the analog signal to obtain a digital output (turn an LED on or off). The sound sensor board does this for you and has a digital output pin called "D0".

Heres a simple sketch using the serial monitor to show you what the sensor "D0" pin is putting out. Connect ground and 5 volts as you did before, and connect sensor D0 to Arduino pin 3.

const byte DigitalInputPin = 3;

void setup(){
  Serial.begin(9600);
}

void loop(){
  Serial.print("Digital Pin = " );
  Serial.println(digitalRead(DigitalInputPin));
  delay(50);
}

You don't have to connect the sensor to an Arduino to adjust it or test it. There are 2 LEDs on the sensor circuit board. The one closest to the potentiometer will turn on when the D0 pin is high and off when it's low. You use the potentiometer to set the "trigger point" based on the level of sound you want to make the output go high.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.