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
