Skip to content

Instantly share code, notes, and snippets.

@sinkers
Last active February 7, 2020 08:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sinkers/d3cea96c2b178275dfb4efbfd0d3d915 to your computer and use it in GitHub Desktop.
Save sinkers/d3cea96c2b178275dfb4efbfd0d3d915 to your computer and use it in GitHub Desktop.
soil_moisture.c
#define voltageFlipPin1 6
#define voltageFlipPin2 7
#define voltageFlipPin3 4
#define voltageFlipPin4 5
#define sensorPin A1
#define SENSOR_PIN_SOIL_VIN A2
#define SENSOR_PIN_TEMP A0
#define NUM_READS 10
int flipTimer = 500;
int Vin = 5;
float R1 = 4700;
float R2 = 0;
float buffer = 0;
float Vout = 0;
void setup() {
Serial.begin(9600);
pinMode(voltageFlipPin1, OUTPUT);
pinMode(voltageFlipPin2, OUTPUT);
pinMode(voltageFlipPin3, OUTPUT);
pinMode(voltageFlipPin4, OUTPUT);
pinMode(sensorPin, INPUT);
pinMode(SENSOR_PIN_SOIL_VIN, INPUT);
pinMode(SENSOR_PIN_TEMP, INPUT);
delay(200);
}
void setSensorPolarity(boolean flip) {
if (flip) {
digitalWrite(voltageFlipPin1, HIGH);
digitalWrite(voltageFlipPin2, LOW);
} else {
digitalWrite(voltageFlipPin1, LOW);
digitalWrite(voltageFlipPin2, HIGH);
}
}
void setSoilSensorOff() {
digitalWrite(voltageFlipPin1, LOW);
digitalWrite(voltageFlipPin2, LOW);
}
void setSensorPolarityTemp(boolean flip) {
if (flip) {
digitalWrite(voltageFlipPin3, HIGH);
digitalWrite(voltageFlipPin4, LOW);
} else {
digitalWrite(voltageFlipPin3, LOW);
digitalWrite(voltageFlipPin4, HIGH);
}
}
void loop() {
//
delay(1000);
int sum = 0;
int val1 = 0;
float avg = 0.0;
int vin = 0;
int sum_vin = 0;
float avg_vin = 0.0;
float vout = 0.0;
bool flip = true;
// Soil measurements with a Watermark 200SS
// With a 10000ohm resistor totally saturated reads at 1.23V
// Note there is about a 1.8% variation in the reading per degree C (or 1% for 1F - http://www.irrometer.com/faq.html#reads0)
setSensorPolarity(flip);
// We need about 500ms as warm up for the sensor with voltage applied
delay(500);
for (int i = 0; i < NUM_READS; i++) {
val1 = analogRead(sensorPin);
Serial.print(val1);
Serial.print(", ");
sum += val1;
vin = analogRead(SENSOR_PIN_SOIL_VIN);
sum_vin += vin;
if (flip) {
flip = false;
} else {
flip = true;
}
setSensorPolarity(flip);
delay(500);
}
/*
setSensorPolarity(false);
// We need about 500ms as warm up for the sensor with voltage applied
delay(500);
for (int i = 0; i < NUM_READS; i++) {
val1 = analogRead(sensorPin);
Serial.print(val1);
Serial.print(", ");
vin = analogRead(SENSOR_PIN_SOIL_VIN);
sum += val1;
sum_vin += vin;
delay(50);
}
*/
setSoilSensorOff();
avg = sum / (NUM_READS);
Serial.println();
Serial.print("Soil Moisture: ");
Serial.print(avg);
avg_vin = (sum_vin / NUM_READS) * (5.0 / 1023);
Serial.print(" , avg_vin: ");
Serial.print(avg_vin);
// TODO fix the avg_vin reading
vout = (4.99 / 1023) * avg;
Serial.print(" , vout: ");
Serial.print(vout);
// TODO this needs to be adjusted for temp
vout = mapfloat(vout, 0, 2.6, 0, 100);
Serial.print(" , moisture (%): ");
Serial.print(vout);
// Temp measurements using a Davis IC6470 resistive temp sensor
// Calibrated against a certified thermometer
// 87C water 1V
// Frozen water 0.8C 4.25v
// TODO from spec resistance 10-20C is 19.8kohm to 12.5Kohm so prob should use a 15kohm resistor, currently 4.7kohm
sum = 0;
setSensorPolarityTemp(true);
// We need about 500ms as warm up for the sensor with voltage applied
delay(500);
Serial.println();
Serial.print("Temp: ");
for (int i = 0; i < NUM_READS; i++) {
val1 = analogRead(SENSOR_PIN_TEMP);
sum += val1;
delay(10);
}
setSensorPolarityTemp(false);
avg = sum / NUM_READS;
Serial.print(avg);
// TODO fix the avg_vin reading
vout = (4.98 / 1023) * avg;
Serial.print(" , vout: ");
Serial.print(vout);
// Get a temp reading in Celcius
float temp = 0.0;
temp = mapfloat(vout, 1.0, 4.25, 87, 0.8);
Serial.print(" , temp (C): ");
Serial.print(temp);
Serial.println();
delay(1000);
}
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment