ESP32 IoT Soil Moisture and Temperature Monitoring System
This project demonstrates how to build an automated soil moisture and temperature monitoring system using an ESP32 microcontroller and a capacitive soil moisture sensor. The ESP32 periodically reads analog moisture data and temperature values, processes them, and publishes the telemetry data over Wi-Fi to a cloud dashboard.

How It Works
A capacitive soil moisture sensor measures the dielectric permittivity of the soil, which changes depending on the water content. Unlike resistive sensors that suffer from galvanic corrosion over time due to direct DC current exposure through the soil, capacitive sensors use an AC measurement technique that greatly extends probe lifespan.
Capacitive Sensing: The sensor acts as one plate of a capacitor, with the surrounding soil and coating acting as the dielectric. Higher moisture increases capacitance and lowers the analog output voltage.
ESP32 ADC Integration: The ESP32 features built-in successive approximation ADCs. However, its ADC has a non-linear response curve near ground and VCC, so taking multiple oversampled readings and applying calibration mapping is essential for accurate percentages.
Wi-Fi Connectivity: The onboard Wi-Fi and TCP/IP stack allow the device to transmit sensor metrics directly to cloud endpoints or MQTT brokers without requiring an intermediate gateway.
Components Needed
- ESP32 Development Board
- Capacitive Soil Moisture Sensor v1.2
- DS18B20 Waterproof Temperature Sensor (with 4.7kΩ pull-up resistor)
- Breadboard and jumper wires
- USB-C or Micro-USB cable for programming and power
Wiring to the ESP32
Connect the VCC pin of the moisture sensor to the 3.3V supply rail, GND to ground, and the Analog Output (AOUT) to GPIO 36 (VP). For the DS18B20 temperature sensor, connect VCC to 3.3V, GND to GND, and the data line to GPIO 4, ensuring a 4.7kΩ pull-up resistor is placed between data and power.
| Module pin | ESP32 pin | Function |
|---|---|---|
| Sensor VCC | 3.3V | Power supply |
| Sensor GND | GND | Common ground |
| Moisture AOUT | GPIO 36 (VP) | Analog moisture signal |
| DS18B20 Data | GPIO 4 | 1-Wire temperature data |

Build and Upload
Open the Arduino IDE and ensure the ESP32 Board Package is installed.
Create a new sketch and paste the monitoring code provided below.
Replace the placeholder Wi-Fi SSID and password with your network credentials.
Select your specific ESP32 board model from the board menu and connect via USB.
Upload the sketch and open the Serial Monitor at 115200 baud to verify sensor initialization and data transmission.
Example Code
ESP32 soil moisture and temperature telemetry reporting over Wi-Fi. Upload it with the board set to your ESP32 model and open the Serial Monitor at 115200 baud.
#include <WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Wi-Fi credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
// Pin definitions
const int MOISTURE_PIN = 36; // VP
const int ONE_WIRE_BUS = 4;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// Calibration constants for dry and wet air/water
const int AIR_VALUE = 3200; // Sensor in dry air
const int WATER_VALUE = 1450; // Sensor fully submerged in water
void setup() {
Serial.begin(115200);
delay(1000);
sensors.begin();
// Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected! IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
// Read analog moisture level
int rawValue = analogRead(MOISTURE_PIN);
int moisturePercent = map(rawValue, AIR_VALUE, WATER_VALUE, 0, 100);
moisturePercent = constrain(moisturePercent, 0, 100);
// Read temperature
sensors.requestTemperatures();
float temperatureC = sensors.getTempCByIndex(0);
// Print results to Serial Monitor
Serial.print("Raw Moisture: ");
Serial.print(rawValue);
Serial.print(" | Moisture: ");
Serial.print(moisturePercent);
Serial.print("% | Temperature: ");
Serial.print(temperatureC);
Serial.println(" °C");
delay(5000); // Report every 5 seconds
}
Applications
- Automated greenhouse and indoor plant watering systems
- Agricultural IoT monitoring fields and crop hydration levels
- Smart garden controllers with remote cloud alerts
- Botanical research experiments tracking soil dynamics
Board Notes and Constraints
The ESP32 is powered by a dual-core Tensilica Xtensa 32-bit LX6 microprocessor operating at 160 or 240 MHz, paired with 520 KB of internal SRAM and Wi-Fi/Bluetooth capabilities.
Logic Voltage: The ESP32 operates at 3.3V logic. Connecting a 5V sensor output directly to an ESP32 GPIO pin can permanently damage the input stage.
ADC Nonlinearity: The ESP32 ADC exhibits nonlinearity near its lower (0V) and upper (3.3V) rails. For precise measurements, make use of calibration curves or look-up tables rather than relying solely on linear map() functions.
Wi-Fi Power Spikes: Radio transmissions draw instantaneous current spikes up to several hundred milliamps. Ensure your USB port or external power supply provides stable regulation to prevent unexpected brownout resets.
| ESP32 characteristic | Value | Why it matters here |
|---|---|---|
| Logic voltage | 3.3V | Requires caution to avoid overvoltage from 5V peripherals |
| ADC resolution | 12-bit (0–4095) | Provides finer resolution than 10-bit boards for analog sensors |
| Analog input pins | 15 channels (ADC1 and ADC2) | Note that ADC2 cannot be used when Wi-Fi is actively running |
| Connectivity | Wi-Fi 802.11 b/g/n & BLE | Enables direct publishing of sensor telemetry to cloud platforms |
Troubleshooting
- Moisture reading stays at 0% or 100%: Check if the analog pin is correctly mapped. If using ADC2 pins while Wi-Fi is active, readings will fail; stick to ADC1 channels like GPIO 36, 37, 38, or 39.
- Temperature reads -127°C: This error code from the DS18B20 indicates a communication failure or missing pull-up resistor on the 1-Wire data line. Verify your wiring and resistor placement.
- Board reboots randomly during Wi-Fi connection: This is typically caused by power supply voltage sags during high current radio transmission bursts. Use a higher-current power source or add a 100µF decoupling capacitor across the 3.3V and GND rails.
- Readings fluctuate wildly: Soil moisture varies across small distances. Ensure the capacitive probe is inserted to its recommended depth line and consider adding software-level moving average filters.
Taking It Further
Once your basic sensor telemetry is reporting correctly over the Serial Monitor, you can expand the project's capabilities:
Cloud Integration: Send your JSON-formatted sensor payloads via MQTT or HTTP POST to platforms like Firebase Firestore or AWS IoT Core to track long-term trends.
Actuator Control: Integrate a 5V relay module driven via a transistor switch to automatically trigger water pumps or solenoid valves when soil moisture drops below a specific threshold.
Local Web Server: Implement an asynchronous web server directly on the ESP32 (ESPAsyncWebServer) so local devices on the same network can view live moisture graphs by browsing to the ESP32's local IP address.