How do I loop over soil moisture sensors

Hello folks how do I loop over a1-a8 each represent a soil moisture

I have been looking around on various topics

this is my atempt

#define AOUT_PIN0 A0 // Arduino pin that connects to AOUT pin of moisture sensor
#define AOUT_PIN1 A1 // Arduino pin that connects to AOUT pin of moisture sensor

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

void loop() {
  int value1 = analogRead(AOUT_PIN0); // read the analog value from sensor
  int value2 = analogRead(AOUT_PIN1); // read the analog value from sensor

  Serial.print("Moisture 0: ");
  Serial.println(value1);

  Serial.print("----");

Serial.print("Moisture 1: ");
  Serial.println(value2);

  delay(500);
}

The output is:
Moisture 1: 38

Moisture 0: 572

I dont get why moisture1 is so low on?

any clues?

I am using

Basically you can go on this way. Ready in 10 minutes, but bulky code.
You could have a look at arrays and for loops.
No copying needed and only around 10 lines of code needed.

I tested following:



void setup() {
  Serial.begin(9600); // serial port setup
}

void loop() {
Serial.println("sensor 1");
Serial.println(analogRead(A0));
Serial.println("----");
Serial.println("sensor 2");

Serial.println(analogRead(A1));

  delay(100); // slight delay between readings
}

though the output is wrong it shows the first sensor OK

sensor 1

569


sensor 2

43

for (int sensePin = A0; sensePin <= A6; sensePin ++)

Try two reads per pin, and ignore the first one.

thanks a lot i tested following code:


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

void loop() {
 for (int sensePin = A0; sensePin <= A6; sensePin ++){
  Serial.println(sensePin);
 }


  delay(5000);
}

The output is though:
55
56
57
58
59
60
54
55
56
57
58
59
60
54
55
56
57

Then I tested:

float volt;
unsigned long val = 0;
unsigned long real = 0;

void setup() 
{
  
  Serial.begin(9600);// put your setup code here, to run once:
  pinMode(A0, INPUT);
}

void loop() 
{
     for(int i = 0; i < 8; i++)
    {
      val += analogRead(A0);
      delay(10);
    } 
  
   real = val / 100;
   volt = real * 5.0 / 1023.0;
   Serial.println(volt, 4);
   Serial.println(real, DEC);
   Serial.println();
   delay(2000);
}

output:
0.6696

137

0.8895

182

1.1144

228

1.3392

274

1.5591

319

1.7840

365

2.0088

411

2.2287

456

2.4536

502

2.6784

548

2.8983

593

You seem to have missed the bit where I mentioned chucking away the first reading from the pin.

You also forgot to zero your summation variable

How should it be instead?

@SemperIdem
I made the code like following:

//Example Sketch using multiple analog sensors into one analog input pin
//Tutorials and more at http://www.cabuu.com

int Value_A0;
int Value_A1;
int Pin_A0 = A0;
int Pin_A1 = A1;

void setup() {

  Serial.begin(9600);   //Start serial monitor
  pinMode(Pin_A0,OUTPUT);
  pinMode(Pin_A1,OUTPUT);
  pinMode(A0,INPUT);
  
}

void loop() {
  
  digitalWrite(Pin_A0, HIGH);     //Turn D7 On
  delay(100);                     //Wait for sensor
  Value_A0 = analogRead(0);       //Read Analog pin as D7
  digitalWrite(Pin_A0, LOW);      //Turn D7 Off

  //Repeat for D8
  digitalWrite(Pin_A1, HIGH);     //Turn D8 On
  delay(100);                     //Wait for sensor
  Value_A1 = analogRead(0);       //Read Analog pin as D8
  digitalWrite(Pin_A1, LOW);      //Turn D8 Off
  delay(100);                     //Wait for sensor
  
  //Print the results to the serial monitor
  Serial.print("A0 = ");   
  Serial.print(Value_A0);
  Serial.print(" / A1 = ");   
  Serial.println(Value_A1);
    
}

output:
A0 = 1020 / A1 = 1021
A0 = 1020 / A1 = 1021
A0 = 1020 / A1 = 1022
A0 = 1019 / A1 = 1023
A0 = 1019 / A1 = 1022
A0 = 1020 / A1 = 1021
A0 = 1019 / A1 = 1021
A0 = 1020 / A1 = 1022
A0 = 1020 / A1 = 1022
A0 = 1020 / A1 = 1022
A0 = 1019 / A1 = 1022
A0 = 1020 / A1 = 1022
A0 = 1019 / A1 = 1021
A0 = 1020 / A1 = 1021

Seems this reasonable?:

actually its wrong because I measured the sensors in water and wet soil. hmm

Why do you output the HIGH level to the analog pin?

You were told to do completely different thing:

When I use those things I calibrate them first. I hold the sensor in the air, wipe clean with a paper towel, and take the average of a few readings to get the sensors dry output. Then I put the sensor into water to get the sensors 100% readings. At that point the map() function can be used to determine percent of moisture.

Which Arduino.

This won't work on a classic Nano,
because A6 and A7 are analogueRead only.
Leo..

Sorry I am on a Arduino Mega 2560.

That line will work on a Mega, but I don't see why you're doing this.
You should only read the voltage coming from the sensor, like in post#1.

Do watch that youtube video, because most of those sensors are badly made.
Some can be fixed by simply adding a 1Megohm resistor.
Others need to be returned.
Leo..

Really?

I didnt know that thanks a lot!

If I would use a Mega for these sensors, I would use the internal 2.56volt reference.
Try this (untested) example sketch. Should work, assuming you have 'fixed' any bad sensor.
Leo..

const byte sensor0 = A0;
const byte sensor1 = A1;
int rawValue0;
int rawValue1;

void setup() {
  Serial.begin(9600);
  analogReference(INTERNAL2V56); // switch to internal 2.56volt Aref (Mega only)
}

void loop() {
  rawValue0 = analogRead(sensor0); // dummy read
  delay(1); // settle
  rawValue0 = analogRead(sensor0); // keep
  rawValue1 = analogRead(sensor1);
  delay(1);
  rawValue1 = analogRead(sensor1);

  Serial.print("Moisture 0 raw: ");
  Serial.println(rawValue0);
  Serial.print("Moisture 1 raw: ");
  Serial.println(rawValue1);
  Serial.println("----");

  delay(4000); // dirty delay
}

Many thanks I will give it a try!

If you have a DMM, then measure the resistance between sensor output and sensor ground, with the sensor disconnected from the Arduino. You should measure about 1Megohm.
If you don't measure 1Megohm, then you can add a 1Megohm resistor externally.
Post a clear picture of the sensor anyway.
Include a link where you got them from.
Leo..