How do I loop over soil moisture sensors

I tested it with two different sensors

unfortunately even when I dry the sensor from wet,
it still same value I also Is using a resistor as well hmm.

I made following that seems to work

const byte sensor0 = A0;
const byte sensor1 = A1;
int rawValue0;
int rawValue1;
#define soilWet 500  // Define max value we consider soil 'wet'
#define soilDry 750  // Define min value we consider soil 'dry'
#define sensorPower 7


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

void loop() {
  rawValue0 = analogRead(sensor0);  // dummy read
  delay(1);                         // settle
  rawValue0 = analogRead(sensor0);  // keep
  rawValue1 = analogRead(sensor1);
  delay(1);
  rawValue1 = analogRead(sensor1);
  int moisture0 = readSensor0();
  int moisture1 = readSensor1();


  Serial.println("--------------------------------------");
  Serial.print("Moisture 0 raw: ");
  Serial.println(rawValue0);
// Determine status of our soil
  if (moisture0 < soilWet) {
    Serial.println("Status: Soil is too wet");
  } else if (moisture0 >= soilWet && moisture0 < soilDry) {
    Serial.println("Status: Soil moisture is perfect");
  } else {
    Serial.println("Status: Soil is too dry - time to water!");
  }
  Serial.println("--------------------------------------");

  delay(4000);


  Serial.println("--------------------------------------");
  Serial.print("Moisture 1 raw: ");
  Serial.println(rawValue1);
  // Determine status of our soil
  if (moisture1 < soilWet) {
    Serial.println("Status: Soil is too wet");
  } else if (moisture1 >= soilWet && moisture1 < soilDry) {
    Serial.println("Status: Soil moisture is perfect");
  } else {
    Serial.println("Status: Soil is too dry - time to water!");
  }
  Serial.println("--------------------------------------");
}





int readSensor0() {
  digitalWrite(sensorPower, HIGH);  // Turn the sensor ON
  delay(10);                        // Allow power to settle
  int val = analogRead(sensor0);    // Read the analog value form sensor
  digitalWrite(sensorPower, LOW);   // Turn the sensor OFF


  return val;  // Return analog moisture value
}


int readSensor1() {
  digitalWrite(sensorPower, HIGH);  // Turn the sensor ON
  delay(10);                        // Allow power to settle
  int val = analogRead(sensor1);    // Read the analog value form sensor
  digitalWrite(sensorPower, LOW);   // Turn the sensor OFF
  return val;                       // Return analog moisture value
}

How could I simplifiy this ? so bad coded.

Don´t make code duplicates.

Create an object that contains all the necessary information, such as port pin, wet value, dry value, timing information, etc. A structured array represents the list of measuring points and a corresponding service takes care of this data.

You are indicating that the sensor produces the same reading when held in the air as when held in the water?

Post an image of your project.

#define pin0 A0
#define pin1 A1
#define pin2 A2
#define pin3 A3
#define pin4 A4
#define pin5 A5
#define pin6 A6
#define pin7 A7


byte Pins[] = {pin0, pin1, pin2,pin3,pin4,pin5,pin6,pin7};
void setup(){
  Serial.begin(9600);
}

void loop(){
  for (int x = 0; x < 7; x++){
    Serial.println(analogRead(Pins[x]));
    Serial.print("pin: ");
    Serial.println(Pins[x]);
    delay(1000);
  }
}

How can I add into the loop what pin is printed?

Print as hex values...

this is what I got so far but it seems like analogPin 1 is printing wrong value:



#define pin0 A0
#define pin1 A1
#define pin2 A2
#define pin3 A3
#define pin4 A4
#define pin5 A5
#define pin6 A6
#define pin7 A7
#define sensorPower 7
#define soilWet 500  // Define max value we consider soil 'wet'
#define soilDry 750  // Define min value we consider soil 'dry'

struct {
  int pin;
  int value;
  int percentage;
  int Dry;
  int Wet;

} sensor[19];  //20


int analogPin1 = A0;
int analogPin2 = A1;
int analogPin3 = A2;
int analogPin4 = A3;
int analogPin5 = A4;
int analogPin6 = A5;
int analogPin7 = A6;
int analogPin8 = A7;




void setup() {
  Serial.begin(9600);  // open the serial port at 9600 bps:

  sensor[0].pin = A0;
  sensor[1].pin = A1;
  sensor[2].pin = A2;
  sensor[3].pin = A3;
  sensor[4].pin = A4;
  sensor[5].pin = A5;
  sensor[6].pin = A6;
  sensor[7].pin = A7;
  sensor[0].Dry = 750;
  sensor[0].Wet = 500;
  sensor[1].Dry = 750;
  sensor[1].Wet = 500;
  sensor[2].Dry = 750;
  sensor[2].Wet = 500;
  sensor[3].Dry = 750;
  sensor[3].Wet = 500;
  sensor[4].Dry = 750;
  sensor[4].Wet = 500;
  sensor[5].Dry = 750;
  sensor[5].Wet = 500;
  sensor[6].Dry = 750;
  sensor[6].Wet = 500;
  sensor[7].Dry = 750;
  sensor[7].Wet = 500;
}
void loop() {

  for (int i = 0; i < 2; i++) {
    Serial.println("Moisture:");
    Serial.println(i, DEC);  // print as an ASCII-encoded decimal
    Serial.println();
    digitalWrite(sensorPower, HIGH);  // Turn the sensor ON
    delay(10);
    Serial.println(analogRead(sensor[i].pin));
    Serial.println("--------------------------------------");
    digitalWrite(sensorPower, LOW);  // Turn the sensor OFF
  }
  delay(4000);
}


Moisture:

1

981


Moisture:

0

1023


Moisture:

1

981


though AnalogPin0 is working fine.

Any ideas?

below is how I connected:

Thanks solved that part @build_1971 though other issues now.

See my last post @Idahowalker

Which ones...
We didn't see any links or pictures yet.
As the youtube video shows, more than 82% tested were crap.

My code should work if... you have the right sensors.
Leo..

Bad ground connection to sensor 0? Breadboard connections may easily get loose. Take care that your wires go in far enough. Wobble them to see if things improve...
Why would 981 be good?
Don't you expect all values to be within 500 -750 range?

Also, you might wanna use for loop to initialize your struct.

for (int i=0; i<7; i++){
    sensor[i].Wet = 500;
    sensor[i].Dry = 750;
}

what for sensor do you recommend any particular?
that have good quality.

indead hmm though with some code they display a range between 1023-400~ and with some other code 980-450~

I am not sure honetly what is proberly right unfortunately.

The sensor you already have...

Unfortunately several manfacturers are making them, and some are cutting corners.
That's why I asked you for a picture of the sensors you have.
Need to know which exact parts are used.
It's all explained in that youtube video.
Leo..

I have got

Olivine 10PCS Capacitive Soil Moisture Sensor for Plant Care Soil Tester Automatic Watering System, for

then I have got 5 of:

If you recommend me to use either or I dont really mind.
I go with ur recommendation.

The first link is the sensor you should use.
It has the 3.3volt regulator and maybe the right 555, and maybe the 1Megohm resistor.

Need to see the exact printing on the 555 chip to confirm,
and you need to measure the resistance between output and ground (should be 1Megohm).

The second link...
Throw it all in the bin.
Leo..

Ok I get back to you!

many thanks!

I tested both 5 volt and 3.3 volt then I get very low values:

Moisture:
1

238
--------------------------------------
Moisture:
0

111

I bought some new Soil moisture v2 I hope those works better.

You should only use 5volt. The sensors (should) have their own 3,3volt regulator

So you didn't show us what the old sensors (printing on the chips).
and you ordered another unknown lot without asking first.

I'm out..
Leo..