Arduino - Capacitive Soil Moisture Sensor

Arduino Jul 30, 2020

This tutorial will go over how to use a Capacitive Soil Moisture Sensor 🌱

Supplies

  • (1) Arduino Mega2560
  • (1) Capacitive Soil Moisture Sensor

For reference I have included the pinout diagram for the Arduino Mega2560
Arduino Mega2560 Pinout

Setup

Connecting the Capacitive Soil Moisture Sensor to the Mega2560 is as easy connecting:

  1. GND -> GND
  2. VCC -> 3.3v
  3. AOUT -> A0
Capacitive Soil Moisture Sensor Setup Diagram 

Calibration

Capacitive Soil Moisture Sensors require calibration before use by doing the following;

  1. Get the value when the sensor is exposed to the air
  2. Get the value when the sensor is in water
void setup() {
  Serial.begin(9600); // open serial port, set the baud rate to 9600 bps
}

void loop() {
  Serial.println(analogRead(A0));
  delay(250);
}

Code

Once you get the numbers from the calibration above, put those numbers as the defaults for AirValue and WaterValue.

const int AirValue = ?;   // This is the value of your sensor when you place it in the open air
const int WaterValue = ?;  // This is the value when you place your sensor in a glass of water

int value = 0;
int percent = 0;

void setup() {
  Serial.begin(9600); // open serial port, set the baud rate to 9600 bps
}

void loop() {
  value = analogRead(A0);

  percent = map(value, AirValue, WaterValue, 0, 100);
  
  if (percent > 100) {
    Serial.print(value);
    Serial.print(" | ");
    Serial.println("100 %");
  } else if (percent < 0) {
    Serial.print(value);
    Serial.print(" | ");
    Serial.println("0 %");
  } else if (percent > 0 && percent < 100) {
    Serial.print(value);
    Serial.print(" | ");
    Serial.print(percent);
    Serial.println("%");
  }
  delay(250);
}

Result

After that uploading the following code to your Arduino mega2560 and opening up the terminal you should see the following:

Tags

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.