wteele
Published © GPL3+

Auto Calibration Program for Capacitive Soil Moisture Sensor

This program guides you through an automated calibration program for capacitive soil moisture sensors, so you don't have to do it manually.

BeginnerFull instructions provided4,641
Auto Calibration Program for Capacitive Soil Moisture Sensor

Things used in this project

Story

Read more

Code

Guided Auto Calibration Program for Capacitive Soil Moisture Sensors

C/C++
You implement it to your existing project so you don't have to manually enter the values. This guided auto calibration program does this for you, and redefines the air and wet values of your soil moisture sensor.
//You need to use a 16x2 LCD display. There are plenty of tutorials on how to connect these to your arduino. Just make sure the pins are correct.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);



int AirValue = 600;   //This is the sensor value when completely dry.
int WetValue = 250;  //This is the sensor value when submerged in water.
unsigned long timeReadingWet = 60000; //This is the time the sensor needs to reach the full wet value (in milliseconds).
unsigned long timeReadingAir = 10000; //This is the time the sensor needs to reach the full air value (in milliseconds).
byte CalibrationButton = 8; //This is the pin number for the button (you don't need any resistors, the Arduino uses an internal one when declared as INPUT_PULLUP (below).




void setup() {

  Serial.begin(9600);
  
  lcd.begin(16, 2); 

  pinMode(CalibrationButton, INPUT_PULLUP); //This is where you declare your button pin as INPUT_PULLUP.

}

void loop() {




// This is the guided program that calibrates your capacitive soil moisture sensor. Just add it to your exising code!



  if (digitalRead(CalibrationButton) == LOW) { //when the button is pressed

    lcd.setCursor(0, 0);
    lcd.print("Let's calibrate");
    lcd.setCursor(0, 1);
    lcd.print("the sensor");
    delay(3000);
    lcd.clear();


    while (digitalRead(CalibrationButton) == HIGH) { //the program waits for the user to press the button again

      
      lcd.setCursor(0, 0);
      lcd.print("Dry sensor");
      lcd.setCursor(0, 1);
      lcd.print("completely B"); //the B stands for Button, it tells the user that the button needs to be pressed to continue


      if (digitalRead(CalibrationButton) == LOW) { //this happens when the button is pressed again

        lcd.clear();

        for (unsigned long timeStartAir = millis(); (millis() - timeStartAir) < timeReadingAir; ) { //this loop reads the sensor for timeReadingAir (defined in the header)
          AirValue = max(analogRead(0), AirValue);
          lcd.setCursor(0, 0);
          lcd.print("Calibrating");
          lcd.setCursor(0, 1);
          lcd.print("for air ");
          lcd.print((timeReadingAir/1000)-(millis() - timeStartAir)/1000); //then displays the time remaining
          lcd.print("s  ");
          Serial.println(AirValue);

        }

        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Air value");
        lcd.setCursor(0, 1);
        lcd.print("established: "); //then it lets the user know that the reading was established (then prints it)
        lcd.print(AirValue);
        Serial.println(AirValue);
        delay(5000);

        lcd.clear();


        while (digitalRead(CalibrationButton) == HIGH) { //same thing but for wet



          lcd.setCursor(0, 0);
          lcd.print("Now submerge");
          lcd.setCursor(0, 1);
          lcd.print("in water B");

          if (digitalRead(CalibrationButton) == LOW) {

            lcd.clear();

            for (unsigned long timeStartWater = millis(); (millis() - timeStartWater) < timeReadingWet; ) {
              WetValue = min(analogRead(0), WetValue);
              lcd.setCursor(0, 0);
              lcd.print("Calibrating");
              lcd.setCursor(0, 1);
              lcd.print("for wet ");
              lcd.print((timeReadingWet/1000)-(millis() - timeStartWater)/1000);
              lcd.print("s  ");
              Serial.println(WetValue);

            }

            lcd.clear();

            lcd.setCursor(0, 0);
            lcd.print("Wet value");
            lcd.setCursor(0, 1);
            lcd.print("established: ");
            lcd.print(WetValue);
            Serial.println(WetValue);
            delay(5000);

            lcd.clear();

            lcd.setCursor(0, 0);
            lcd.print("Calibration");
            lcd.setCursor(0, 1);
            lcd.print("successful");

            Serial.println(WetValue);
            delay(5000);
            lcd.clear();


            return;

          }


        }



      }

    }

  }

}

Credits

wteele

wteele

0 projects • 0 followers

Comments