analogRead returning 1500+ values ?

Hi,

I am using Grove Soil Moisture sensor in one of my project but the sensor is returning values between 500-1550 on my analog pin.

It's not really a problem because I constraint and map them but for my understanding (i am a beginner), is it not supposed to be values between 0-1023 ?

Does 1500 value mean that the sensor is returning more than 5v ?

Thank for your help.

What kind of board are you using? For example, a Due has a 12-bit ADC so values between 0 and 4096 :wink:

septillion:
What kind of board are you using? For example, a Due has a 12-bit ADC so values between 0 and 4096 :wink:

Sorry I forgot to mention it !

It's an Arduino Uno rev3 (5v)

karnalta:
Sorry I forgot to mention it !
It's an Arduino Uno rev3 (5v)

The UNO's 10-bit ADC cannot return values larger than 1023. What leads you to believe 'analogRead()' is returning larger values?
Perhaps you should post your code, between [code]code tags[/code] of course.

OldSteve:
The UNO's 10-bit ADC cannot return values larger than 1023. What leads you to believe 'analogRead()' is returning larger values?
Perhaps you should post your code, between

[code]code tags[/code]

of course.

Here is my read function and the Serial.Println return value greater than 1023. Am I doing something wrong ?

(There is 3 soil sensor and I am doing 3 read on each to avoid peak)

void SoilSensor::GetSensorsValues(int* pinsValues)
{
  int pins[] = {_sensor1Pin, _sensor2Pin, _sensor3Pin};
  
  for (int i=0; i<3; i++)
  {
    int value = 0;
    for (int x = 0; x <= SOIL_READ_CYCLE; x++)
    {
      value += analogRead(pins[i]);
      delay(READ_CYCLE_DELAY);
    }
    value /= SOIL_READ_CYCLE;  

    #ifdef DEBUG
    Serial.println("Soil " + String(i) + " : " + String(value));
    #endif
    
    value = constrain(value, SOIL_MIN_VALUE, SOIL_MAX_VALUE);
    value = map(value, SOIL_MIN_VALUE, SOIL_MAX_VALUE, 100, 0);

    pinsValues[i] = value;

    if (value == 0)
      pinsValues[i] = -1;
  }
}

EDIT :

Oops, I saw my error.. My for loop should be x < SOIL_READ_CYCLE and not <=. I read 4 times and divide by 3... So value are too high..

You nailed it :wink:

This is all that's relevant:-

for (int x = 0; x <= SOIL_READ_CYCLE; x++)
    {
      value += analogRead(pins[i]);
      delay(READ_CYCLE_DELAY);
    }
    value /= SOIL_READ_CYCLE;

If we assume that "SOIL_READ_CYCLE" = 3, then your 'for' loop will do 4 iterations: 0, 1, 2, 3, and therefore 4 analogReads and add the 4 returned values together.

Then for a final result you divide by "SOIL_READ_CYCLE", (3), whereas you really need to divide by 4.

Change this:-

value /= SOIL_READ_CYCLE;

to this:-

value /= (SOIL_READ_CYCLE+1);

Edit: You edited while I was typing. We both did it a different way, but the result is the same - no more error. :slight_smile: