Plantmate Ⓡ Capacitive Soil Moisture Sensor with OLED Display & Arduino:

Overview:

Whether you’re a professional gardener or just a plant lover, it is ideally important to monitor your soil moisture level. In this tutorial, we are about to learn how to interface Plantmate Ⓡ Moisture Sensor with Arduino then display the soil moisture value in percentage (%) on an OLED display.

Introduction to PlantmateCapacitive Soil Moisture Sensor 3.3V:

There are two types of soil moisture sensors, capacitive and resistive. Although the capacitive sensor and resistive sensor have the same goal in measuring moisture, they differ in their methods. Capacitive sensors are considered to be more accurate and stable. Other than that, the major issue with resistive soil moisture sensors is the corrosion of the sensor probes, not just because it is in contact with the soil but also because there is a DC current flowing which causes electrolysis of the sensors. And that is why the capacitive type of sensor has must longer life.

  1. Dealing with water or moisture, water proof is essentially important. The components of the Plantmate Ⓡ Capacitive Soil Moisture Sensor are over-molding, it is water proof;
  2. PlantmateⓇ Capacitive Soil Moisture Sensor has the sensor probes on both side of the PCB, it is more sensitive and accurate;
  3. Plantmate Ⓡ Capacitive Soil Moisture Sensor has a longer cable to contact with your Arduino board, cable itself is about 85cm with a Micro USB male end. it comes with a Micro USB female adapter to make it even easier to use. Surely that you can cut off the Micro USB male end to contact with your Arduino pins directly.
  4. Plantmate Ⓡ Capacitive Soil Moisture Sensor operating with 3.3V and it is compatible with low-voltage MCUs. To make it compatible with a Raspberry Pi, an ADC converter is required.

Hardwares need for this project:

1. Arduino UNO Board
2. 0.91-inch SSD1306 OLED Display Module
3. PlantmateⓇ Capacitive Soil Moisture Sensor
4. Breadboard
5. Male to male jumper wires
6. Soldering Tools (soldering pins to OLED and Micro USB adapter if need.)

Arduino UNO0.91″ OLED Display PlantmateⓇ Capacitive Soil Moisture Sensor
Breadboard Male to male jumper wire Soldering Tools (soldering pins to OLED and Micro USB adapter if need.)

Connecting Moisture Sensor, Arduino UNO, and OLED:

Softwares need for this project:

  • Arduino IDE;
  • 2 Libraries need (Adafruit_GFX.h, Adafruit_SS1306.h).

To check if you have these 2 libraries installed
Open the Arduino IDE;
Click Tools > Manage Library…

Type ‘Adafruit_gfx’ in the search bar, if you haven’t installed this library, click ‘Install’ to install.

Type ‘Adafruit_ssd1306’ in the search bar, if you haven’t installed this library, click ‘Install’ to install.

After all the above steps you have taken, we are ready to run the codes:

Each moisture sensor does not provide accurate data as expected, but we can get the closest accurate value by running these codes below:

/*
 The Codes below is to get the 2 Values: Value_1 that the sensor is on air dry and the Value_2 that the sensor is into the water completely.
 
 Water Comparator pins explanation:
 The Water Comparator has 5 pins, we will use 3 pins (V.+in, Aout, GND)and ignore the 2 pins(Pump+, Pump-)for this purpose.
 1.  V.+ in 
     Connect 3.3V from Arduino Board, please pay attention it is the 3.3V, NOT 5V.
 2.  Aout
     Analog Input can be connected with any analog pins on the Arduino Board, on this demonstrates, we use analog pin 0.
 3. GND
    Connect GND to GND on Arduino.

  modified 18 Aug 2021
  By Long Duck Dong
  This example code is in the public domain.
  http://plantmate.ca/blog/
*/


int sensorPin = A0;    // select the input pin for the Water Comparator
int sensorValue = 0;
void setup() {
  Serial.begin(9600); //open serial port, set the baud rate to 9600bps
}

void loop() {
  sensorValue = analogRead(sensorPin); // read the value from the sensor

  Serial.print("Value: "); Serial.println(sensorValue); 
}
  1. On your Arduino IDE, Click on’File’ > ‘New’ to open a new sketch window;
  2. Copy the code above then paste it to the sketch window that you just open;
  3. Connect the moisture senor Micro USB cable to the Micro USB adapter;
  4. Connect the Arduino UNO board to your computer via USB cable;

The next step is to upload the code to your Arduino UNO board, but before that, please click ‘Tools’ on your Arduino IDE to double-check if your board information and the Port is connected.

Upload the code to your Arduino UNO

Now we leave the moisture sensor on dry, and click the ‘Serial Monitor’ on your Arduino IDE:

Now you will see the “Values” on your pop-up Serial Monitor window similar as below:

Write the Vaule_dry on a piece of paper.

Now we put the moisture sensor into the water completely, and click the ‘Serial Monitor’ on your Arduino IDE:

Now you will see the “Values” on your pop-up Serial Monitor window similar as below:

Write the Vaule_wet on the paper that you wrote the Vaule_dry.

Finally, we open a new sketch, copy the code below to your new sketch.

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

/*
 * OLED to Arduino UNO Board PIN connection:
 * pin connection see: https://www.arduino.cc/en/Reference/Wire
 * 1. SDA to A4 (or SDA)
 * 2. SCL to A5 (or SCL)
 * 3. VCC to 3.3V
 * 4. GND to GND :-)
 * ===========================
 * Planmate Moisture Sensor Micro USB adapter to UNO Board PIN connection:
 * 1. VCC to 3.3V
 * 2. GND to GND
 * 3. D+ to A0
 * 4. D- to A0
 * 5. ID (NA)
 */
#define OLED_RESET 4 //Reset pin # (or-1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(OLED_RESET);
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

const int Value_dry = 580;   //replace this value with Value_dry from water-detect-values.ino
const int Value_wet = 280;   //replace this value with Value_wet from water-detect-values.ino
int SensorValue = 0;
int MoisturePercent=0;
void setup() {
  Serial.begin(9600); // use 9600 for 16MHz
  while (!Serial){;}

// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
  display.clearDisplay();
  display.display(); 
               }

void loop() {
SensorValue = analogRead(A0); 
Serial.println(SensorValue);
MoisturePercent = map(SensorValue, Value_dry, Value_wet, 0, 100); // Translate Moisture Sensor to a scale of 0% to 100%
                                                                // More info: https://www.arduino.cc/reference/en/language/functions/math/map/
if(MoisturePercent > 100) //correct the percentage to 100% if read over 100.
{
  Serial.println("100 %");

  display.setCursor(0,0);  //oled display
  nonameText("Moisture:", 1,0,1,false); //nonameText(String text, int x, int y,int size, boolean d)
  nonameText("100 %", 50,20,1,false);
  display.display();
  delay(250);
  display.clearDisplay();
}
else if(MoisturePercent <0) //correct the percentage to 0% if read less than 0.
{
  Serial.println("0 %");
  
  display.setCursor(0,0);  //oled display
  nonameText("Moisture:", 1,0,1,false);
  nonameText("0 %", 50,20,1,false);
  display.display();
  delay(250);
  display.clearDisplay();
}
else if(MoisturePercent >0 && MoisturePercent < 100)
{
  Serial.print(MoisturePercent);
  Serial.println("%");
  
  display.setCursor(0,0);  //oled display
  nonameText("Moisture:", 1,0,1,false);
  nonameText(String(MoisturePercent), 50,20,1,false);
  nonameText("%", 65,20,1,false);
  display.display();
  delay(250);
  display.clearDisplay();
}  
}
/*
 * nonameText(String text, int x, int y,int size, boolean d)
 * text is the text string to be printed
 * x is the integer x position of text
 * y is the integer y position of text
 * z is the text size, 1, 2, 3 etc
 * d is either "true" or "false". Not sure, use true
 */
void nonameText(String text, int x, int y,int size, boolean d) {

  //display.setFont(&FreeSans9pt7b);
  display.setTextSize(size);
  display.setTextColor(WHITE);
  display.setCursor(x,y);
  display.println(text);
  if(d){
    display.display();
  }
  
}

Now replace the Value_dry and Value_wet with the new values that you wrote down from the prior step.

Save the code and upload it to the Arduino UNO.

If you did all the steps correctly, you should see moisture value in percentage (%) on an OLED display.

Additional Remark:

You may notice that the Plantmate Capacitive Soil Moisture Sensor is a 3.3V rated sensor and I am using an Arduino UNO which is a 5V rate on analog pins, which means that it can read 0-5V with 1023 steps. That is, 5000 mV/1024 = 4.88 mV, meaning that it can only measure a signal change in the ADC if the voltage increases or decreases by about 5 mV.

The default analog reference of 5 V (on 5V Arduino boards) or 3.3V (on 3.3V Arduino boards)

We can use small voltage output sensors (less than 5 V) on projects. However, this makes the 5 V Arduino boards reading less accurate.

There are many ways to increase the accuracy for a 3.3 V sensor with a 5V operating board,
related articles:

Can I use a 3.3 V rated sensor with a 5 V operating board?

 Arduino board’s Internal reference

The easy way for this demo project to increase accuracy is using a jumper wire to connect the 3.3V with the AREF pin on your Arduino UNO board.

The END!


Leave a Reply

Your email address will not be published. Required fields are marked *