Analog sensor value influenced by other variable

Hello everyone,

i’m facing a problem which i onestly don’t understand. Maybe it is just my fault because i’ve been struggling with this project last days and there has always been another issue coming on, but any advice will be useful!

I am trying to make a simple audiometer with an audio sensor (KY-038) and some leds. Leds should light according to the values measured by the sensor and compared to a threshold (that’s because sensor values come in a narrow range and i need to determine where my “silence point” is). I can modify the threshold with two simple buttons (one up, one down).

The problem is: when the threshold (modified using those buttons) gets to “touch” values coming from the sensor, sensor values shift, like they were “avoiding” the threshold. Please refer to this image for a better understanding:

As you can see, when the threshold, which is the red line (and is manually modified using buttons) “touches” the sensor values (blue line), sensor values shift in the other direction.

I really can not understand how a variable coming from a digital input interferes with analog values coming from a sensor.

Here’s the code i’m using (it’s just for testing, so don’t expect any complete or optimized code):

int thresholdUp = 13;

int ledsOn;
float threshold = 0;

void setup() 
{
  pinMode (sound, INPUT);
  pinMode(thresholdDown, INPUT_PULLUP);
  pinMode(thresholdUp, INPUT_PULLUP);
  for(int i=0;i<8;i++){
    pinMode(ledPins[i], OUTPUT);
  }
  Serial.begin(9600);
}

void loop() 
{
  if(digitalRead(thresholdDown) == LOW) threshold -= 0.2;	//threshold is modified long-pressing buttons
  if(digitalRead(thresholdUp) == LOW) threshold += 0.2;

  (void)analogRead(sound);	
  int sensor = analogRead(sound);  

  Serial.print(sensor);
  Serial.print(",");
  Serial.println(threshold);

  if(sensor < threshold){
    ledsOn = 0;
  }

  if(sensor >= threshold && sensor < threshold+2){
    ledsOn = 1;
  }
  
   if(sensor >= threshold+2 && sensor < threshold +4){
    ledsOn = 2;
  }

  [...]

  if(ledsOn == 0){
    for(int i=0;i<8;i++)digitalWrite(ledPins[i],LOW);
  }
 
  for(int i=0;i<ledsOn;i++){
    digitalWrite(ledPins[i],HIGH);
  }
}

Thank you in advance for any advice or solution :slight_smile:

The power used to light up the LEDs may slightly vary the voltage of the Arduino and that voltage is taken as a reference to measure the analog signal. As your signal varies in a small range this might explain the special behavior. Remove the LED switching part from the sketch and measure again. I'd guess that the behavior won't show up again.

Which analog input pin is "sound"?

(it's just for testing, so don't expect any complete or optimized code)

That's not how you get problems solved. Please post a complete sketch showing the issue.

You wouldn't take a photo of a car like yours to the garage to be fixed, you take the car
with the problem that needs fixing.

pylon:
The power used to light up the LEDs may slightly vary the voltage of the Arduino and that voltage is taken as a reference to measure the analog signal. As your signal varies in a small range this might explain the special behavior. Remove the LED switching part from the sketch and measure again. I'd guess that the behavior won't show up again.

Could i also try powering the sensor from an external source in your opinion? Or is it something that has nothing to do with what you wrote?

MarkT:
That's not how you get problems solved. Please post a complete sketch showing the issue.

You wouldn't take a photo of a car like yours to the garage to be fixed, you take the car
with the problem that needs fixing.

The one i posted is the complete sketch, what i intended is that maybe the code won't be optimized, being in a "prototype of the prototype" phase :slight_smile:

The one i posted is the complete sketch

No, it isn't, that code doesn't compile.

Could i also try powering the sensor from an external source in your opinion? Or is it something that has nothing to do with what you wrote?

No, do what I told you and post the result. The LEDs need current to light up and that may influence the voltage a bit. And that voltage is the reference for the sensor reading. As the LEDs are switched on and off based on the reading, the influence seems obvious.