Display Plant Moisture Using a ESP32 and a LCD
by Matt RG in Circuits > Microcontrollers
403 Views, 4 Favorites, 0 Comments
Display Plant Moisture Using a ESP32 and a LCD
In this tutorial you'll learn how to set up and calibrate the moisture sensors, and then display the data on a LCD all using a ESP32.
Supplies
To make this project you will need the following:
- ESP32
- 2 Breadboards
- Liquid Crystal Display (LCD)
- 2 Capacitive Soil Moisture Sensors
- Potentiometer
- HW-131 Power Supply module
- 9V battery
- Various wires
- A potted plant!
Set Up the ESP32 in Arduino IDE
The ESP32 doesn't come as a preloaded or standard library for the Arduino IDE and must be set up first.
If you don't already have the ESP32 set up for the Arduino IDE, I found this article by Rui Santos to be very useful at setting up the ESP32 and debugging any potential problems. I recommend following his guide.
https://randomnerdtutorials.com/installing-the-esp32-board-in-arduino-ide-windows-instructions/
Wire and Calibrate the Moisture Sensors
Now that the ESP32 is set up with Arduino IDE, it's important to test and calibrate the moisture sensors. Capacitive moisture sensors, can be prone to manufacturing defects which will prevent it from detecting any change in moisture, so by doing this we'll find out which sensors are functional.
Wire the ESP32
Wiring the moisture sensors to the ESP32 is simple. One wire (red) to the 5V source, another wire (black) to ground, and the third (yellow) to an analog pin. In this case we are using pin 34.
Calibrate the Sensors
Once the sensor is wired, upload the code ME608_ESP32_Soil_SensorTest.io attached to help calibrate the sensors. We will have to set two constant variables at the beginning of the code, AirValue and WaterValue. This will set a percentage moisture scale.
#define soilPin 34
//Declare Soil Sensor Variables.
const int AirValue = 3500;
const int WaterValue= 1500;
int soilmoistureValue = 0;
int soilmoisturepercent = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
soilmoistureValue= analogRead(soilPin);
Serial.println(soilmoistureValue);
//Map the percentage from a 0 to 100 scale. With the AirValue as 0% and WaterValue as 100% moisture.
soilmoisturepercent = map(soilmoistureValue, AirValue, WaterValue, 0 ,100);
//Eliminate edge cases
if(soilmoisturepercent >= 100)
{
Serial.println("100%");
}
else if(soilmoisturepercent <= 0)
{
Serial.println("0%");
}
else
{
Serial.print(soilmoisturepercent);
Serial.print("%");
}
Serial.println(" ");
delay(250);
}
When the program running, have the moisture sensor dry and in the air. The serial monitor will display two numbers, the moisture value and percentage moisture. Take the average of the moisture value, this will be the AirValue. In my case, this number was about 3500.
Now take the moisture sensor and submerge the majority of it in a glass of water while being sure not to get the circuits wet at the top. Take the average of the moisture value, this will be the WaterValue. In my case, this number was about 1500.
Notice that when you take your fingers and touch the sensor that this will also change the values.
Update Values
Update AirValue and WaterValue to the numbers you found. 0% moisture will now occur at the AirValue found, and 100% moisture will show when the sensor is submerged in water. This will be a scale that we can now use to compare relative moisture. Record these numbers for later as well.
Downloads
Set Up LCD Screen
Now that we know the air and water values for the moisture sensor, we can set up the LCD screen. Remove everything from the breadboard.
The ESP32 doesn't fit cleanly onto a single breadboard with the LCD screen. I recommend placing the ESP32 between two connected breadboards so that it bridges from one board, over the power rails, onto the other. This will allow us to use both sets of pins on the ESP32.
Wiring the LCD
We will be using pins 15,2,19,21,22,23 on the ESP32 to connect to the LCD. The LCD also requires a potentiometer to allow us to adjust the contrast on the screen.
Refer to the wiring diagram provided and recreate it on your board. Red is power and green/black is ground, other colors go to pins.
Combine It All and Test
Now that the LCD screen is hooked up, we can add the capacitive soil moisture sensors from earlier.
Wiring the Moisture Sensors
To do this, we will be using pins 34 and 35 of the ESP32. Attach each sensor to both ground (black/green wires) and the power (red wires) on the top power rail, and wire one sensor to pin 34 and the other to pin 35. We'll call these sensor 1 and sensor two respectively. Refer to the diagram attached for help.
Testing
Upload the following code to the ESP32. The screen should immediately turn on and start cylcing through text showing "Soil_1Moist. %:" If this doesn't happen there might be a wiring issue.
//This is the library for the LCD.
#include <LiquidCrystal.h>
//Define Pins
#define soilPin1 34
#define soilPin2 35
//LCD, Define Pins
LiquidCrystal lcd(15,2,19,21,22,23);
//Define Global Vars. Use the Air and Water Values you found earlier.
const int AirValue = 3500;
const int WaterValue= 1500;
int m_percent1 = 0;
int m_percent2 = 0;
void setup() {
Serial.begin(9600);
//LCD Set up, we have 16 characters and 2 rows
lcd.begin(16,2);
}
void loop() {
//Row 1,Collumn 0 is indexed as 0.
lcd.setCursor(0,0);
//Read and display moisture percetage of the first sensor
m_percent1 = readMoisture(soilPin1);
lcd.setCursor(0,0);
lcd.print("Soil_1 Moist. %:");
lcd.setCursor(0,1);
lcd.print(m_percent1);
delay(5000);
//Read and display moisture percetage of the second sensor
m_percent2 = readMoisture(soilPin2);
lcd.setCursor(0,0);
lcd.print("Soil_2 Moist. %:");
lcd.setCursor(0,1);
lcd.print(m_percent2);
delay(5000);
}
int readMoisture(int soilPinNum) {
//This function reads the moisture sensor and converts it into a percetage.
int soilmoistureValue;
int soilmoisturepercent;
soilmoistureValue = analogRead(soilPinNum);
soilmoisturepercent = map(soilmoistureValue, AirValue, WaterValue, 0 ,100);
//Remove Edge cases
if(soilmoisturepercent >= 100)
{
soilmoisturepercent = 99;
}
else if(soilmoisturepercent <= 0)
{
soilmoisturepercent = 0;
}
return soilmoisturepercent;
}
Update Values
Update AirValue and WaterValue to the numbers you found from earlier .Now you have two fully functioning moisture sensors and a screen to read the data from! Test it by touch the sensor with your hand and watching the values change. The screen alternates between displaying sensor 1 and sensor 2 every 5 seconds.
Make It Battery Powered
Now that everything is working properly, we can remove the need to keep it connected to a computer and have it be battery powered.
Add the Power Module
To do this, unplug the device from your computer and place the HW-131 power module onto the breadboard so that all four pairs of pins run on the power rails. Make sure all grounds and 5V power inputs run to either of these rails. Once this is done, plug in a 9V battery and press the white switch to turn on the device.
Be sure to turn off the power when not in use as the LCD screen can drain the battery over long periods of time if left running.
Simple Video Demo
Have Fun, and Potential Improvements
Congratulations! You now have two working moisture sensors and a LCD to show the values. Use this to grow seedlings or track moisture levels in different soil depths.
Some potential improvements
- Using a I2C LCD module. This can reduce the amount of wires to the LCD and remove the need for an external potentiometer.
- Use the deep sleep function to make the device energy efficient.
- Add an automatic watering feature when the moisture levels get low.
- Add more moisture sensors
Thank you for following this Tutorial.