Sensor readings inconsistent

I am trying to run the following sensor on my Particle Electron:

https://www.dfrobot.com/wiki/index.php/Analog_Sound_Sensor_SKU:_DFR0034

Even when I play a constant sound, the readings are very inconsistent, going from a value of 50 to 0 to 0 to 19, for example. I’m not sure why.

Below is the code I wrote:

int sound = A0;

void setup() {
    Serial.begin(9600);
pinMode(sound, INPUT);
}

void loop() {
    String soundValue = String(analogRead(sound));
Particle.publish("sound value", soundValue, 20);
delay(1000); // send data every 1 seconds

}

That’s not a large range in values.
How do you have it wired?
Are you sure that you’re using A0, 3v3, and Ground?
You want to keep the wires as short as possible.

You can comment out the Particle.publish and use Serial.print for testing, with a shorter delay.

Yes I have it wired correctly. If i do serial.print, how can i see the serial monitor?

From the sensor website you posted, there is a typical sensor readout from the serial monitor. The values range from 0 to 153. You’re values of 19 and 50 seem to be within the norm. The sensor tutorial doesn’t say anything about the sound conditions during the sample. If you are playing a constant tone, you could be sampling various instantaneous points on the waveform. The sensor description page also recommends you use the sensor with the “Audio Analyzer” which is another equalizer filter breakout board.

image

If you use a Serial.println(), it would show up in the serial monitor in-line with all of the soundValue readings. You would be able to output to serial at a much faster rate to see values almost realtime… or some reasonable time period that is shorter than 1 second. If you get enough datapoints fast enough, you could copy over to Excel and create a graph to see if there is a pattern to the datapoints.

This would output sound values 10 times a second:

int sound = A0;

void setup() {
    Serial.begin(9600);
    pinMode(sound, INPUT);

    Serial.println("Starting sound measurements...");
}

void loop() {
    Serial.print(analogRead(sound));
    Serial.print(", ");
    delay(100);
}
1 Like

For analogRead() you should not set pinMode(pin, INPUT) since it actually uses a dedicated AN_INPUT mode which is set by the command itself.

@lunchboxp, also be aware that the Electron is using a 12bit ADC with a value range of 0…4095 for 3.3V compared to an Arduino 10bit ADC with a range of 0…1023 for 5V. So the voltage step per unit for the Electron is much smaller than what you’d get for one step on the Arduino.
One Arduino step would mean six (6!) steps on the Electron.

1 Like