Introduction
This tutorial covers how to use the Adafruit STEMMA Capacitive Soil Moisture Sensor. This sensor works using the same technology as capacitive touchscreens, which are so effective precisely because the human body contains a large amount of water; water is particularly good at altering the capacitance in the sensor, which makes using capacitance to measure moisture an effective tactic.
Pros and Cons
This sensor is an excellent choice for someone seeking to generally measure moisture over a long period of time; unlike resistive moisture sensors (commonly seen in Arduino projects), this sensor does not degrade quickly over time and should produce reliable readings for a long period. However, this sensor also comes with a major downside: because it only produces a capacitive measurement, it is not possible to calibrate it to a specific water quantity or moisture level, such as a volumetric water content (VWC). This lack of calibration ability means this sensor is best suited to environments in which you already know a lot about the water provision to a source, or projects where what matters is the binary presence/complete lack of water in a specific location.
Tutorial Use Case
The purpose of this tutorial is to introduce the sensor and some basic code that can be used to obtain and display measurements from the sensor. In addition, the code includes a “moving-window” approach to reporting data, where the current measurement is averaged with some number of previous measurements before reporting. This approach helps eliminate outliers and other “noise” in the raw sensed data. Thus, the tutorial code is best applied to situations where you are immediately reporting data readings with no additional, after-the-fact data processing or analysis. The code also includes a “wet/not wet” threshold of 400 and prints out the “wetness” status it is sensing based on that cutoff. This threshold can be adjusted based on the context in which you are using the sensor.
Parts List
- Arduino UNO Board & Starter Kit
- Adafruit STEMMA Capacitive Soil Moisture Sensor
- JST PH 2mm 4-pin to Male Header Cable/I2C STEMMA Cable, 200mm
Circuit Diagram & Wiring
Attach the sensor directly to the Arduino board using the 4-pin cable with the following connections (shown in the diagram below as well):
- Red wire (V in) to 5V power supply pin
- Black wire (Ground) to GND pin
- Green wire (SCL) to A5
- White wire (SDA) to A4
Code
To use this sensor, first install the Seesaw library from Adafruit, which can be found by searching “seesaw” in the Sketch>Include Library>Manage Libraries dialogue box:
Once the library is installed, paste the below code into a new Arduino sketch file, and upload it to your board. Once the code is uploaded, open the Serial Monitor (make sure it is set to read data at a 115,200 baud rate), and check out your temperature and moving-window average capacitance readings. Because the sensor reports capacitance, you can test to see if it’s working by simply touching your finger to the sensor—it uses the same technology as many touchscreen devices.
NOTE: For purposes of demonstration, this code takes a measurement reading every second. When measuring soil moisture levels, it is likely unnecessary to measure this frequently, and the interval can be increased by changing the delay quantity at the very end of the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
/* Capacitive Moisture Sensor Tutorial * Henry Feinstein * March 30, 2022 * CPLN571 Sensing the City Spring 2022 * University of Pennsylvania * * The purpose of this code is to demonstrate the use of the Adafruit STEMMA Capacitive Moisture Sensor * for testing soil moisture. In addition to demonsrating how to obtain readings from the sensor, * the code also includes a "moving window" calculation to average readings over time, which helps * mitigate error and variation in sensor readings over time. This is helpful in the context of soil * moisture, since the rate of response for addressing soil moisture is fairly slow--this allows for * us to take more time to process the data before acting on it. * * The sensor also includes a low-precision temperature sensor for at-a-glance environmental information; * this tutorial also includes information on how to obtain that temperature reading. * * Documentation for this sensor can be found on the Adafruit website at the link below: * https://learn.adafruit.com/adafruit-stemma-soil-sensor-i2c-capacitive-moisture-sensor * * This code is based closely on example code written by lady ada of Adafruit, which can be found here: * https://learn.adafruit.com/adafruit-stemma-soil-sensor-i2c-capacitive-moisture-sensor/arduino-test */ #include "Adafruit_seesaw.h" //This library includes the functions and object definitions necessary to use the sensor Adafruit_seesaw ss; //Initialize the ss ("seesaw") object, which represents the sensor const int array_len = 4; //Change this value to adjust the size of the "moving window" array to calculate over-time average //NOTE: this variable must be defined as constant for the array memory allocation to be successful float reading_array[array_len]; //Initialize "moving window" array int array_pos = 0; //Variable to use to walk through indices of array float capread_avg = 0; //Variable to use to store average capacitance reading int is_wet = 0; //Variable to indicate whether sensed environment is wet (1) or dry (0) void setup() { Serial.begin(115200); //Initialize the serial monitor at a 115,200 baud rate if (!ss.begin(0x36)) { //Check to see if sensor is successfully connected to Arduino board Serial.println("ERROR! seesaw not found"); while(1) delay(1); } else { Serial.print("seesaw started! version: "); Serial.println(ss.getVersion(), HEX); } } void loop() { float tempC = ss.getTemp(); //Using the library, obtain temperature reading from the sensor uint16_t capread = ss.touchRead(0); //Using the library, obtain capacative touch sensor reading from sensor if (array_pos >= array_len) { //Checking to see if we've reached the end of the array array_pos = 0; //If we have, go back to first position and start replacing values there } reading_array[array_pos] = capread; //Put newest capacitive reading into array array_pos++; capread_avg = 0; //Set up variable to calculate average for (int i = 0; i < array_len; i++) { capread_avg += reading_array[i]; //Add together all values in array } capread_avg = capread_avg / array_len; //Divide by length of array to get average if (capread_avg > 400) { //Check averaged measurement to see if environment is wet, and adjust is_wet accordingly is_wet = 1; } else { is_wet = 0; } //Print resulting readings to the serial monitor Serial.print("Temperature Reading: "); Serial.print(tempC); Serial.println("*C"); Serial.print("Capacitive Reading: "); Serial.println(capread_avg); Serial.print("Soil Moisture Status: "); if (is_wet == 1) { Serial.println("Wet"); } else { Serial.println("Not Wet"); } delay(1000); //Wait before taking next reading } |
References:
One reply on “Using a Capacitive Sensor to Measure Soil Moisture”
Hi, i have a problem the sensor work normally with the exemple and now i don’t know why he doesn’t work anymore i have every time “Error seesaw not found” do you have an idea what is the problem ?