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.

Sinric Pro HW-390 capacitive soil moisture sensor

Prerequisites :

  1. ESP32, ESP8266 or Raspberry Pi Pico W x 1.
  2. Capacitive Soil Moisture Sensor (HW-390) x 1.
  3. Plant with a pot x 1.
  4. 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

Sinric Pro HW-390 esp8266 capacitive soil moisture sensor wiring

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);
}

HW-390 capacitive soil moisture sensor arduino readings

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);
}

Sinric Pro moisture sensor esp8266 readings

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.

  1. Soil is Wet or Dry

  2. Moisture level.

Note: You can use the device template import feature mentioned below to skip creating the full template.

Sinric Pro capacitive soil moisture sensor device template

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.

Sinric Pro custom device type for capacitive soil moisture sensor

Click on Configure button and setup the two capabilities like below.

Sinric Pro moisture sensor template mode and range settings

Click on Save to save.

Sinric Pro moisture sensor template mode and range settings

Click on Save to save the template.

Sinric Pro moisture saved 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.

Sinric Pro capacitive soil moisture sensor import template

Paste this Template:

Sinric Pro create device alexa

Sinric Pro copy device id

Sinric Pro copy device id

Step 2 : Connect to Sinric Pro

Step 2.1 Install Sinric Pro Library

Sinric Pro install SinricPro 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)

Sinric Pro Alexa water sensor

Sinric Pro portal

Troubleshooting

  1. Google Home or SmartThings are not supproted.

  2. Please refer to our Troubleshooting page for more details.

This document is open source. See a typo? Please create an issue