Mutli-sensor Capacitive soil array (4)

Hello all Im trying to follow this project:

Water Your Garden with IoT - Soil Moisture Sensors - YouTube
WayinTop (WayinTop) · GitHub

I can get one sensor to read and write to the serial monitor in the code below with 1 working sensor. With the following code:

I have the 3.3v connected to the Aref and the vcc in the sensor.

/*

  Soil Moisture Sensor Calibration

  soil_calibrate_uno.ino

  Gets raw reading from soil sensor and displays on Serial Monitor

  Use to establish minuimum and maximum values

  Works with Capacitive and Resistive Sensors

  Uses Arduino Uno

 

  DroneBot Workshop 2022

  https://dronebotworkshop.com

  

  Mariano's notes

  https://www.youtube.com/watch?v=pgGpuws7f9o

  https://dronebotworkshop.com/soil-moisture/

*/

 

// Variable for sensor value

// Constant for dry sensor

const int DryValue = 864;

 

// Constant for wet sensor

const int WetValue = 557;

 

// Variables for soil moisture

int soilMoistureValue; 

int soilMoisturePercent;

 

// Analog input port

#define SENSOR_IN 0

  

void setup() {

  // Open Serial Monitor

  Serial.begin(9600);

 

  // Set ADC to use 3.3-volt AREF input

  analogReference(EXTERNAL);

}

void loop() {

 

  // Get soil mositure value

  soilMoistureValue = analogRead(SENSOR_IN);

 

  // Print to serial monitor

  Serial.print(soilMoistureValue);

  Serial.print("V");

  Serial.print(" - ");

  Serial.print("%");

delay (1000);  

 

  // Determine soil moisture percentage value

  soilMoisturePercent = map(soilMoistureValue, DryValue, WetValue, 0, 100);

   

  // Keep values between 0 and 100

  soilMoisturePercent = constrain(soilMoisturePercent, 0, 100);

    

  // Print to serial monitor

  Serial.println(soilMoisturePercent);

    

  delay(1000);

}

serial monitor results:
609V - %83
608V - %83
597V - %86
592V - %88
865V - %0
866V - %0
867V - %0

The following code is my attempt at getting it to work with 4 sensors. But I could not get it to work. Im not to sure what Im doing as I am a beginner with no formal programing education. I tried to fix and match the integers and the pins and variables. Thank you.
I usually do the projects a step at a time and expand the code, so I can troubleshoot more easily, idk if this is a good way, but Im definitely open to advice.

/*

  Soil Moisture Sensor Calibration

  soil_calibrate_uno.ino

  Gets raw reading from soil sensor and displays on Serial Monitor

  Use to establish minuimum and maximum values

  Works with Capacitive and Resistive Sensors

  Uses Arduino Uno

 

  DroneBot Workshop 2022

  https://dronebotworkshop.com

  

  M's notes

  https://www.youtube.com/watch?v=pgGpuws7f9o

  https://dronebotworkshop.com/soil-moisture/

*/

 

// Variable for sensor value

// Constant for dry sensor

const int DryValue = 864;

 

// Constant for wet sensor

const int WetValue = 557;

 

// Variables for soil moisture

int soilMoistureValue0; 

int soilMoistureValue1;

int soilMoistureValue2;

int soilMoistureValue3;

int soilMoisturePercent0;

int soilMoisturePercent1;

int soilMoisturePercent2;

int soilMoisturePercent3;

 

// Analog input port

#define SENSOR_IN 0

#define SENSOR_IN 1

#define SENSOR_IN 2

#define SENSOR_IN 3

  

void setup() {

  // Open Serial Monitor

  Serial.begin(9600);

 

  // Set ADC to use 3.3-volt AREF input

  analogReference(EXTERNAL);

}

void loop() {

 

  // Get soil mositure value

  soilMoistureValue0 = analogRead(SENSOR_IN), 0 ;

 soilMoistureValue1 = analogRead(SENSOR_IN), 1;

 soilMoistureValue2 = analogRead(SENSOR_IN), 2;

 soilMoistureValue3 = analogRead(SENSOR_IN), 3;

  // Print to serial monitor

  Serial.print(soilMoistureValue0), 0;

  Serial.print("V");

  Serial.print(" - ");

  Serial.print("%");

delay (1000);  

  Serial.print(soilMoistureValue1), 1;

  Serial.print("V");

  Serial.print(" - ");

  Serial.print("%");

delay (1000);

Serial.print(soilMoistureValue2), 2;

  Serial.print("V");

  Serial.print(" - ");

  Serial.print("%");

delay (1000); 

Serial.print(soilMoistureValue3), 3;

  Serial.print("V");

  Serial.print(" - ");

  Serial.print("%");

delay (1000); 

  // Determine soil moisture percentage value

  soilMoisturePercent0 = map(soilMoistureValue0, DryValue, WetValue, 0, 100);

  soilMoisturePercent1 = map(soilMoistureValue1, DryValue, WetValue, 0, 100);

  soilMoisturePercent2 = map(soilMoistureValue2, DryValue, WetValue, 0, 100);

  soilMoisturePercent3 = map(soilMoistureValue3, DryValue, WetValue, 0, 100);

 

  // Keep values between 0 and 100

  soilMoisturePercent0 = constrain(soilMoisturePercent0, 0, 100);

   soilMoisturePercent1 = constrain(soilMoisturePercent1, 0, 100);

    soilMoisturePercent2 = constrain(soilMoisturePercent2, 0, 100);

     soilMoisturePercent3 = constrain(soilMoisturePercent3, 0, 100);

 

  // Print to serial monitor

  Serial.println(soilMoisturePercent0);

  

  Serial.println(soilMoisturePercent1);

   

  Serial.println(soilMoisturePercent2);

  Serial.println(soilMoisturePercent3);

 

  delay(1000);

}

Thank you for your time

you are not declaring you sensor pins correctly.

Try this:
(Compiles, NOT tested!)

/*

  Soil Moisture Sensor Calibration

  soil_calibrate_uno.ino

  Gets raw reading from soil sensor and displays on Serial Monitor

  Use to establish minuimum and maximum values

  Works with Capacitive and Resistive Sensors

  Uses Arduino Uno



  DroneBot Workshop 2022

  https://dronebotworkshop.com



  M's notes

  https://www.youtube.com/watch?v=pgGpuws7f9o

  https://dronebotworkshop.com/soil-moisture/

*/



// Variable for sensor value

// Constant for dry sensor

const int DryValue = 864;



// Constant for wet sensor

const int WetValue = 557;



// Variables for soil moisture

int soilMoistureValue0;

int soilMoistureValue1;

int soilMoistureValue2;

int soilMoistureValue3;

int soilMoisturePercent0;

int soilMoisturePercent1;

int soilMoisturePercent2;

int soilMoisturePercent3;



// Analog input port

#define SENSOR_IN0 A0

#define SENSOR_IN1 A1

#define SENSOR_IN2 A2

#define SENSOR_IN3 A3



void setup() {

  // Open Serial Monitor

  Serial.begin(9600);



  // Set ADC to use 3.3-volt AREF input

  analogReference(EXTERNAL);

}

void loop() {



  // Get soil mositure value

  soilMoistureValue0 = analogRead(SENSOR_IN0);

  soilMoistureValue1 = analogRead(SENSOR_IN1);

  soilMoistureValue2 = analogRead(SENSOR_IN2);

  soilMoistureValue3 = analogRead(SENSOR_IN3);

  // Print to serial monitor

  Serial.print(soilMoistureValue0);

  Serial.print("V");

  Serial.print(" - ");

  Serial.print("%");

  delay (1000);

  Serial.print(soilMoistureValue1);

  Serial.print("V");

  Serial.print(" - ");

  Serial.print("%");

  delay (1000);

  Serial.print(soilMoistureValue2);

  Serial.print("V");

  Serial.print(" - ");

  Serial.print("%");

  delay (1000);

  Serial.print(soilMoistureValue3);

  Serial.print("V");

  Serial.print(" - ");

  Serial.print("%");

  delay (1000);

  // Determine soil moisture percentage value

  soilMoisturePercent0 = map(soilMoistureValue0, DryValue, WetValue, 0, 100);

  soilMoisturePercent1 = map(soilMoistureValue1, DryValue, WetValue, 0, 100);

  soilMoisturePercent2 = map(soilMoistureValue2, DryValue, WetValue, 0, 100);

  soilMoisturePercent3 = map(soilMoistureValue3, DryValue, WetValue, 0, 100);



  // Keep values between 0 and 100

  soilMoisturePercent0 = constrain(soilMoisturePercent0, 0, 100);

  soilMoisturePercent1 = constrain(soilMoisturePercent1, 0, 100);

  soilMoisturePercent2 = constrain(soilMoisturePercent2, 0, 100);

  soilMoisturePercent3 = constrain(soilMoisturePercent3, 0, 100);



  // Print to serial monitor

  Serial.println(soilMoisturePercent0);



  Serial.println(soilMoisturePercent1);



  Serial.println(soilMoisturePercent2);

  Serial.println(soilMoisturePercent3);



  delay(1000);

}

also just for reference, "soilMoistureValue" is the ADC value of the voltage read from the sensor. it is not the actual voltage value (to get the actual voltage value is just another map function! :wink: )

hope that helps....

consider

const int DryValue = 864;
const int WetValue = 557;

// Variables for soil moisture
int soilMoistureValue;
int soilMoisturePercent;

const byte SensorPins [] = { A0, A1, A2, A3 };
char s [80];

void loop()
{
    for (unsigned n = 0; n < sizeof(SensorPins); n++)  {
        int moisture = analogRead (SensorPins [n]);
        int pct      = map(moisture, DryValue, WetValue, 0, 100);

        sprintf (s, " %6d %3d%%,", moisture, pct);
        Serial.print (s);
    }
    Serial.println ();

    delay (1000);
}

void setup() {
    Serial.begin(9600);
 // analogReference(EXTERNAL);
}

Thank you very much for the correct syntax for the pinouts. Just a quick question about the format. So you could only something like #define SENSOR_IN0 B0 only if you have a pin on your board labeled B0 correct?

" also just for reference, "soilMoistureValue" is the ADC value of the voltage read from the sensor. it is not the actual voltage value (to get the actual voltage value is just another map function! :wink: ) "

It would be the voltage across the sensor?
Thank you very much for your help.

correct.

the analogRead() on a UNO outputs a value of 0 - 1023 which can be equated to a voltage read of 0 - 5V

hope that helps...

This is the code I ended up using thank you very much,

/*

  Soil Moisture Sensor Calibration

  soil_calibrate_uno.ino

  Gets raw reading from soil sensor and displays on Serial Monitor

  Use to establish minuimum and maximum values

  Works with Capacitive and Resistive Sensors

  Uses Arduino Uno



  DroneBot Workshop 2022

  https://dronebotworkshop.com



  M's notes

  Complied with help from the Arduino forum users' and M

  https://www.youtube.com/watch?v=pgGpuws7f9o

  https://dronebotworkshop.com/soil-moisture/

*/



// Variable for sensor value

// Constant for dry sensor

const int DryValue = 864;


// Constant for wet sensor

const int WetValue = 557;



// Variables for soil moisture

int soilMoistureValue0;

int soilMoistureValue1;

int soilMoistureValue2;

int soilMoistureValue3;

int soilMoisturePercent0;

int soilMoisturePercent1;

int soilMoisturePercent2;

int soilMoisturePercent3;



// Analog input port

#define SENSOR_IN0 A0

#define SENSOR_IN1 A1

#define SENSOR_IN2 A2

#define SENSOR_IN3 A3



void setup() {

  // Open Serial Monitor

  Serial.begin(9600);



  // Set ADC to use 3.3-volt AREF input

  analogReference(EXTERNAL);

}

void loop() {



  // Get soil mositure value

  soilMoistureValue0 = analogRead(SENSOR_IN0);

  soilMoistureValue1 = analogRead(SENSOR_IN1);

  soilMoistureValue2 = analogRead(SENSOR_IN2);

  soilMoistureValue3 = analogRead(SENSOR_IN3);

  // Print to serial monitor

  Serial.println("'Volts'");

  Serial.print(soilMoistureValue0);

  Serial.print("V");

  Serial.print(" - ");

  delay (1000);

  Serial.print(soilMoistureValue1);

  Serial.print("V");

  Serial.print(" - ");

  
  delay (1000);

  Serial.print(soilMoistureValue2);

  Serial.print("V");

  Serial.print(" - ");

  
  delay (1000);

  Serial.print(soilMoistureValue3);

  Serial.print("V");

  

  

  Serial.println();

  delay (1000);

  // Determine soil moisture percentage value

  soilMoisturePercent0 = map(soilMoistureValue0, DryValue, WetValue, 0, 100);

  soilMoisturePercent1 = map(soilMoistureValue1, DryValue, WetValue, 0, 100);

  soilMoisturePercent2 = map(soilMoistureValue2, DryValue, WetValue, 0, 100);

  soilMoisturePercent3 = map(soilMoistureValue3, DryValue, WetValue, 0, 100);



  // Keep values between 0 and 100

  soilMoisturePercent0 = constrain(soilMoisturePercent0, 0, 100);

  soilMoisturePercent1 = constrain(soilMoisturePercent1, 0, 100);

  soilMoisturePercent2 = constrain(soilMoisturePercent2, 0, 100);

  soilMoisturePercent3 = constrain(soilMoisturePercent3, 0, 100);



  // Print to serial monitor

  Serial.println();
  
  Serial.print("Moisture");
  
  Serial.println();

  Serial.print("%");
  
  Serial.println(soilMoisturePercent0);

  Serial.print("%");

  Serial.println(soilMoisturePercent1);

  Serial.print("%");

  Serial.println(soilMoisturePercent2);

  Serial.print("%");

  Serial.println(soilMoisturePercent3);

  Serial.println();



  delay(1000);

}

Im still not to sure how to use the map function to get the voltage as I have never used a map function before. I will mess around with the map function to see if I can get the actual voltage with the map function. It helped alot , thank you very much.

Ahh I see what you mean.

So I would have to set an

int voltageValue0;
int voltageValue1;
int voltageValue2;
int voltageValue3;

void setup() {
}

void loop(); {

voltageValue0 = map(soilMoistureValue0, DryValue, WetValue, 0, 1023);
voltageValue1 = map(soilMoistureValue1, DryValue, WetValue, 0, 1023);
voltageValue2 = map(soilMoistureValue2, DryValue, WetValue, 0, 1023);
voltageValue3 = map(soilMoistureValue3, DryValue, WetValue, 0, 1023);

voltageValue0 = constrain(voltageValue0,0,5);
voltageValue1 = constrain(voltageValue1,0,5);
voltageValue2 = constrain(voltageValue2,0,5);
voltageValue3 = constrain(voltageValue3,0,5);
{

Is this on the right path? It compiled correctly but timeout upload error was produced.

I get his error :

C:\Users\maria\AppData\Local\Temp\ccqTANd1.ltrans0.ltrans.o: In function main': C:\Users\maria\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\cores\arduino/main.cpp:43: undefined reference to setup'
collect2.exe: error: ld returned 1 exit status Compilation error: exit status 1

if you were using the sensor voltage value within the your could then that would definitely be the way!

however looking at the example code you originally posted, I think it is simply using the ADC value here. so IMHO you there is no requirement for converting the ADC value to voltage! :slight_smile:

// Variable for sensor value

// Constant for dry sensor

const int DryValue = 864; //<---- this is the ADC value IMHO!


// Constant for wet sensor

const int WetValue = 557; //<---- this is the ADC value IMHO!

hope that helps...

it compiles for me

i don't think you copied the entire code. does your copy have setup()?

Ahh I see, I was confusing the voltage for the analog to digital conversion value, thank you for clarifying. I was mistakenly under the impression that I required the voltage for some reason.

I did not have setup() and it did upload. Thank you very much