How to build a Capacitive Soil Moisture Sensor (HW-390) for ESP8266, ESP32, Raspberry Pi Pico W for Alexa
In this part, we’ll guide you on building a Capacitive Soil Moisture Sensor that can be connected to either an ESP32, ESP8266, or Raspberry Pi Pico W. Once it’s set up, you’ll be able to check the soil moisture level using Amazon Alexa, ask Alexa for information about it, and also monitor it through the Sinric Pro app and send a push notification when soil moisture is dry or wet.
Prerequisites :
- ESP32, ESP8266 or Raspberry Pi Pico W x 1.
- Capacitive Soil Moisture Sensor (HW-390) x 1.
- Plant with a pot x 1.
- Jumper Wires.
Note: on ESP32, ADC2 (GPIO04, GPIO02, GPIO15) is unstable when Wi-Fi is being used.
Quick introduction to Capacitive Soil Moisture Sensor
Capacitive soil moisture sensors measure the electrical resistance between two probes. The resistance changes depending on the amount of water in the soil. The sensor is connected to a microcontroller (MCU), which measures the capacitance between the probes. The HW-390 circuit converts the capacitance measurement to a voltage signal (ADC), which is then sent to the MCU. The MCU can use this signal to display the soil moisture level or to control other devices.
Note: Fertilizers and corrosion can reduce the accuracy of the sensor over time.
Wiring Capacitive Soil Moisture Sensor
MCU | GPIO Pin |
---|---|
ESP32 | 34 (Analog ADC1_CH6) |
ESP8266 | A0 |
Pico W | GP26 (ADC0) |
Let’s verify that sensor is wired correctly and working.
#if defined(ESP8266)
const int adcPin = A0;
#elif defined(ESP32)
const int adcPin = 34;
#elif defined(ARDUINO_ARCH_RP2040)
const int adcPin = 26;
#endif
void setup() {
Serial.begin(9600);
pinMode(adcPin, INPUT);
}
void loop() {
Serial.println(analogRead(adcPin));
delay(1000);
}
Let’s convert the analog input to a precentage value.
#if defined(ESP8266)
const int adcPin = A0;
#elif defined(ESP32)
const int adcPin = 34;
#elif defined(ARDUINO_ARCH_RP2040)
const int adcPin = 26;
#endif
const int DRY = 720; // TODO: This is value when soil is dry. Adjust according to your usecase
const int WET = 560; // TODO: This is value when soil is wet. Adjust according to your usecase
void setup() {
Serial.begin(9600);
pinMode(adcPin, INPUT);
}
void loop() {
int sensorVal = analogRead(adcPin);
int percentage = map(sensorVal, WET, DRY, 100, 0);
Serial.printf("Sensor value: %d percentage: %d%%\r\n", sensorVal, percentage);
delay(1000);
}
Step 1 : Connect to Sinric Pro
Step 1.1 : Creating a custom device type for Capacitive Soil Moisture Sensor.
Sinric Pro does not have a built-in device type for Capacitive Soil Moisture Sensor so we are going to create a custom device type for Capacitive Soil Moisture Sensor using Device Template feature to see.
-
Soil is Wet or Dry
-
Moisture level.
Note: You can use the device template import feature mentioned below to skip creating the full template.
-
Login to your Sinric Pro account and goto Device Templates
-
Click Add Device Template. Enter name and description as Capacitive Soil Moisture Sensor and select Capacitive Soil Moisture Sensor as device type
- Click Capabilities.
Here we must select the features of our Soil Moisture Sensor. We want to know whether Soil is Wet or Dry and the Moisture level. So let’s drag a Range, a Mode and Push Notification capability.
-
What’s Range capability?
Range capability can be use to represents a number. eg: current speed of a blender.
-
What’s Mode capability?
Mode capability can be use to represents a value out of list of predefined values. eg: current wash cycle mode of a washing machine.
-
What’s Push Notification capability?
Notification capability provides the ability to send a push notification message from the code.
Click on Configure button and setup the two capabilities like below.
Click on Save to save.
Click on Save to save the template.
Now you can see the template we just created.
Import an existing template?
If you are feeling lazy setup all the Modes and Range values, you can use the import feature.
Paste this Template:
-
Login to your Sinric Pro account, go to Devices menu on your left and click Add Device button (On top left).
-
Enter the device name Garden, description My Garden and select the device type as Capacitive Soil Moisture Sensor.
-
Click Others tab and Click Save
-
Next screen will show the credentials required to connect the device you just created.
-
Copy the Device Id, App Key and App Secret Keep these values secure. DO NOT SHARE THEM ON PUBLIC FORUMS !
-
Click on Code Generator to Generate the code. Click on Download to download the code.
Step 2 : Connect to Sinric Pro
Step 2.1 Install Sinric Pro Library
2.2 Complete Code
Now you should be able to see the soil moisture via Alexa, Sinric Pro App
Alexa, What’s the moisture (mode name) in garden(device name)
Alexa, What’s the moisture level(range name) in garden(device name)
Troubleshooting
-
Google Home or SmartThings are not supproted.
-
Please refer to our Troubleshooting page for more details.
This document is open source. See a typo? Please create an issue