[EN] ESP8266+Soil Moisture Sensor

This article uses the ESP8266 to read the values ​​from the soil moisture sensor. The appearance of the selected sensor is shown in Figure 1. The circuit of the sensor is shown in Figure 2 which there’s a part for the conversion of resistance from current flow to analog and digital values. In which the digital sector has to be rotated to adjust the resistance from the variable resistor. After that, the voltage obtained by adjusting the resistance is used as a comparator of the voltage obtained by the sensor circuit and output the data as a digital signal as a value of 0 or 1, but in this article choose to read the value from analog signal via A0, which is 10 bits analog to digital converter (ADC) consequently the value read from analog signal will be integer start from 0 to 1024. The example program is written in Python along with showing the converted values ​​from the ADC displayed on the web page.

(Figure. 1) Soil moisture sensor
(Figure. 3) Circuit for soil moisture sensor


ADC is an analog-to-digital conversion circuit. The analog values are represented by integers in the range from 0 to the maximum value based on the bit size of the ADC circuit. ESP8266 has one 10 bits ADC pin. The pin which connected to this circuit was called “A0”

One thing to be aware of when using an ADC is that the ADC circuit was designed to be compatible with voltages up to 1VDC. With this limitation, most board manufacturers add an R2R circuit to reduce the input voltage to a value below 1VDC therefore, using Pin A0 must supply a voltage that does not exceed 3V3., because over 3V3 will cause the input voltage to the ADC to exceed 1VDC

To create a reference pin variable to the ESP8266 ADC, import the machine library, and declare the variable as follows.

from machine import ADC
adcVar = ADC(0)

To read values from the ESP8266 ADC circuit, use the following format command.

intVal = adcVar.read()

Circuit connection

The circuit connection is as shown in Figure 3 by connecting the signal pin from the sensor circuit to the ESP8266 as follows.

Sensor circuit –> ESP8266
Vcc –> 3V3
A0 –> A0

(Figure. 3) Example of circuit connection

Example code

code4-1 is to read a value from conversions of the analog signal to the digital signal, display via RS232 every 1 second.

import time
from machine import ADC
pinAdc = ADC(0)
while True:
    dValue = pinAdc.read()

Example code, displaying the result on the webpage

Code4-2 is to improve the functionality of code4-1 to switch to web display.

import socket
import network
import esp
from machine import ADC
pinAdc = ADC(0)
ap = network.WLAN(network.AP_IF)
ssid = 'JarutEx-AP'
password = '123456789'
ap.config(essid=ssid, password=password)
while ap.active() == False:
print('Connection successful')
def web_page():
    dValue = pinAdc.read()
    html = """<html><head><meta name="viewport"
      content="width=device-width, initial-scale=1"></head>
      <body><h1>Sensor Value = """
    html += str(dValue)
    html += "</h1></body></html>"
    return html
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
while True:
    conn, addr = s.accept()
    print('conn {} from {}'.format(conn, addr))
    request = conn.recv(1024)
    print('request = {}'.format(request))
    response = web_page()
    conn.send('HTTP/1.1 200 OK\n')
    conn.send('Content-Type: text/html\n')
    conn.send('Connection: close\n\n')


In this article, we get to know the ADC, an essential module that converts digital signals up to 1VDC which has a circuit to reduce the voltage from 3V3 to 1VDC and wrote a python program to read the values from the circuit to displayed via RS232 in code4-1 and displayed via web in AP mode in code4-2.

Finally, we, JarutEx, hope that the example from this chapter will provide an example and a guideline for those interested in coding a program to read the values from the soil moisture sensor. And have fun with programming.

If you want to discuss something, feel free to leave comments below.

(C) 2020, Danai Jedsadathitikul and Jarut Busarathid
updated 2020-10-04

Leave a Reply

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