multiple AnalogRead

Hi All

I have 4 Lm393 Soil Moisture sensors (analog) Connected to Analog pins 0 - 3

define PIN1 = A0

define PIN2 = A1

define PIN3 = A2

define PIN4 = A3

in my loop() I have

int valueA = analogRead(PIN1); int valueB = analogRead(PIN2); int valueC = analogRead(PIN3); int valueD = analogRead(PIN4);

One sensor is in dry dirt one in moist and one in water So the reading should be different But they are almost the same 50 , 53 , 58

If I lift the sensor into the air I'll get a reading around 800s --- 900s

But I'm finding it hard to believe the different between wet and dry soil is so small and that the values are so close together

I think the readings are getting swapped on confused

What is the correct way to read multiple analog inputs ?

Do I need a pinMode for analog inputs ?? pinMode(A0, INPUT) or something similar ?

Thanks in advance

What is the correct way to read multiple analog inputs ?

I have seen it recommended that you read the input twice in succession and use the value returned by the second reading.

Do I need a pinMode for analog inputs ??

Not needed as they default to being inputs, but no harm doing it if it makes the intention clear in the code.

You should probably post your real code. Snippets rarely contain the problem.

As posted, these snippets wouldn't compile. Anyway.

Hint:

TheBearF8:

define PIN1 = A0

define PIN2 = A1

define PIN3 = A2

define PIN4 = A3

define statements are text-replacment macros, not variables.

Here is the full code so far

#include <SPI.h>
#include <stdio.h>

#include "Adafruit_GFX.h"
#include "Adafruit_RA8875.h"

#define   VALVE_1_PIN    10
#define   VALVE_2_PIN    11
#define   VALVE_3_PIN    12
#define   VALVE_4_PIN    13

#define   LIGHT_PIN      A0

#define   WATER_LIMIT    500

#define   WATER_1_PIN    A1
#define   WATER_2_PIN    A2
#define   WATER_3_PIN    A3

#define   RA8875_CS   22
#define   RA8875_INT  24
#define   RA8875_RST  25

int  water_1_level   = 0;
int  water_2_level   = 0;
int  water_3_level   = 0;

Adafruit_RA8875 tft = Adafruit_RA8875(RA8875_CS, RA8875_RST);

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

  pinMode(VALVE_1_PIN, OUTPUT);
  pinMode(VALVE_2_PIN, OUTPUT);
  pinMode(VALVE_3_PIN, OUTPUT);
  pinMode(VALVE_4_PIN, OUTPUT);

  digitalWrite(VALVE_1_PIN, HIGH);
  digitalWrite(VALVE_2_PIN, HIGH);
  digitalWrite(VALVE_3_PIN, HIGH);
  digitalWrite(VALVE_4_PIN, HIGH);

  pinMode(RA8875_CS,  OUTPUT);  digitalWrite(RA8875_CS,  HIGH);
  pinMode(RA8875_INT, OUTPUT);  digitalWrite(RA8875_INT, HIGH);
  pinMode(RA8875_RST, OUTPUT);  digitalWrite(RA8875_RST, HIGH);

  if (!tft.begin(RA8875_800x480)) {
    Serial.print("RA8875 NOT Found  \n\n");
  } else {
    Serial.print("RA8875 Found  \n\n");
  }
  
  tft.displayOn(true);
  tft.GPIOX(true);
  tft.PWM1config(true, RA8875_PWM_CLK_DIV1024);
  tft.PWM1out(255);
  
  tft.fillScreen(RA8875_RED);    delay(500);
  tft.fillScreen(RA8875_WHITE);  delay(500);
  tft.fillScreen(RA8875_BLUE);   delay(500);
  tft.fillScreen(RA8875_GREEN);  delay(500);
  tft.fillScreen(RA8875_RED);    delay(500);
  tft.fillScreen(RA8875_WHITE);  delay(500);
  tft.textMode();

  tft.textEnlarge(1);
  tft.textColor(RA8875_WHITE, RA8875_RED);

  tft.textSetCursor(  5, 71);    tft.textWrite("Light           0.00                   0.00      ");

  tft.textEnlarge(1);
  tft.textColor(RA8875_YELLOW, RA8875_RED);
}


void loop() {
  delay(1000);

  int light          = analogRead(LIGHT_PIN);
  int water_1_level  = analogRead(WATER_1_PIN);
  int water_2_level  = analogRead(WATER_2_PIN);
  int water_3_level  = analogRead(WATER_3_PIN);

  if (water_1_level > WATER_LIMIT)  digitalWrite(VALVE_1_PIN, LOW);  else  digitalWrite(VALVE_1_PIN, HIGH);
  if (water_2_level > WATER_LIMIT)  digitalWrite(VALVE_2_PIN, LOW);  else  digitalWrite(VALVE_2_PIN, HIGH);
  if (water_3_level > WATER_LIMIT)  digitalWrite(VALVE_3_PIN, LOW);  else  digitalWrite(VALVE_3_PIN, HIGH);
                                                                           digitalWrite(VALVE_4_PIN, HIGH);


  char   temp[15];
  
  Serial.println("  ");

  Serial.print("Light Level: ");
  Serial.print(light);
  Serial.print("         ");
  Serial.print((float)(1023-light) * 10 / light, DEC);

  
  dtostrf(light,                                         8, 2, temp);  tft.textSetCursor(230, 71);  tft.textWrite(temp);
  dtostrf((float)(1023-light) * 10 / light,              8, 2, temp);  tft.textSetCursor(565, 71);  tft.textWrite(temp);


  char text[18]  = "";
  
  sprintf(text, "      A: %4d ", water_1_level);          Serial.print(text);
  if (water_1_level > 700) Serial.print(" [ ON]");  else  Serial.print(" [OFF]");

  sprintf(text, "      B: %4d ", water_2_level);          Serial.print(text);
  if (water_2_level > 700) Serial.print(" [ ON]");  else  Serial.print(" [OFF]");
  
  sprintf(text, "      C: %4d ", water_3_level);          Serial.print(text);
  if (water_3_level > 700) Serial.print(" [ ON]");  else  Serial.print(" [OFF]");
}

Basically an automatic watering device with 3 zones which should turn on or off a solenoid valve based on the soil moisture value

But right now dry soil, damp soil and wet soil all have very much the same readings
between 51 and 65 Whereas If I take the sensor out of the ground (in the air) the reading is in the 800s

Thanks again in advance

What is the sensor?

(And please use

 tags when posting code.)

For what it is worth, the answer to your original question would be, you are doing multiple analogRead() correctly. 

So your topic and title aren't descriptive of your issue. Your issue is, "how do properly detect dry, damp, and wet soil?"

Try this, see if it makes a difference in your readings:

Instead of:

  int light          = analogRead(LIGHT_PIN);
  int water_1_level  = analogRead(WATER_1_PIN);
  int water_2_level  = analogRead(WATER_2_PIN);
  int water_3_level  = analogRead(WATER_3_PIN);

Try:

  int dummy = analogRead(LIGHT_PIN);
  int light          = analogRead(LIGHT_PIN);
  dummy = analogRead(WATER_1_PIN);
  int water_1_level  = analogRead(WATER_1_PIN);
  dummy = analogRead(WATER_2_PIN);
  int water_2_level  = analogRead(WATER_2_PIN);
  dummy = analogRead(WATER_3_PIN);
  int water_3_level  = analogRead(WATER_3_PIN);

Sorry about the

or lack there of I'm remember

The sensors are lm393 
Sensitivity Adjustable Soil Moisture Sensor Hygrometer Detection Module Pack Of 5
[url=https://www.amazon.com/gp/product/B00NAV2FW2/ref=oh_aui_detailpage_o07_s00?ie=UTF8&psc=1[/url]

Thanks again to all

Hi all,

I was trying to use two analogRead using arduino due, but one of the readings are always a random number. I did try using the code by 756E6C, but the reading is still a random number. Does anyone has any other ideas?

:'( :'( :'( :'(

LiTing: I was trying to use two analogRead using arduino due, but one of the readings are always a random number. I did try using the code by 756E6C, but the reading is still a random number. Does anyone has any other ideas?

Show YOUR code.

Did you connect the inputs to something? If not, they are floating and can reading them can result in anything.

Hi sterretje,

I am using the codes by TheBearF8. Sorry for not mentioning.

Also I did connect to a certain sensor. When I tested the sensor individually using one analogRead,it works well. However, when I use to connect to two sensors, fluctuation occurs.

In reply #7 you mention the code by 756E6C, now you mention the code by TheBearF8.

Post YOUR code.