This guide shows you how to build an ESP8266 IoT soil test station that measures soil moisture, soil temperature and TDS (Total Dissolved Solids), then uploads the readings to the cloud over Wi-Fi. It is a student and final year project for smart agriculture — a working prototype you can demonstrate with real soil and real readings.

The NodeMCU (ESP8266) has only one analog input pin, A0. The moisture sensor and the TDS sensor are both analog — they cannot both connect to A0 directly. This project solves that with a CD74HC4067 16-channel analog multiplexer: both sensors connect to the multiplexer, and the ESP8266 switches between them, reading one at a time through A0. The wiring table and the code below show exactly how.
| Component | Specification | Price / link |
|---|---|---|
| NodeMCU | ESP8266 CH340 Wi-Fi board | NodeMCU ESP8266 CH340 Wi-Fi Board — Rs 590 |
| Soil moisture sensor | Capacitive type | Soil Moisture Sensor for Arduino — Rs 155 |
| Temperature sensor | DS18B20 waterproof | — |
| TDS sensor | Analog TDS module | — |
| Multiplexer | CD74HC4067 16-channel analog | — |
| Resistor | 4.7k ohm (pull-up for DS18B20) | — |
| Display (optional) | 16×2 LCD with I2C | — |
| Power | Rechargeable battery / power bank (5V USB) | — |
| Breadboard + jumper wires | — | — |
The shop's soil moisture sensor is the resistive type — it works the same way on the same pin as the capacitive sensor, but recalibrate the dry and wet numbers in the code for your sensor. The DS18B20, TDS module, multiplexer and LCD do not have individual listings in the QKZEE shop — they are available at the Hall Road shop on request.
| Component | ESP8266 Pin | Notes |
|---|---|---|
| Multiplexer SIG | A0 | The single analog input — both analog sensors are read through this pin |
| Multiplexer S0 | D5 | Channel select |
| Multiplexer S1 | D6 | Channel select |
| Multiplexer S2 | D7 | Channel select |
| Multiplexer S3 | D8 | Channel select |
| Multiplexer VCC / GND | 3V3 / GND | — |
| Multiplexer EN | GND | Enable pin must be grounded |
| Moisture sensor output | Multiplexer channel 0 | — |
| TDS sensor output | Multiplexer channel 1 | — |
| Moisture + TDS VCC | 3V3 | — |
| DS18B20 data | D4 | OneWire data |
| DS18B20 VCC / GND | 3V3 / GND | — |
| 4.7k ohm resistor | Between DS18B20 data (D4) and 3V3 | Pull-up — without it the sensor will not reply |
| LCD SDA | D2 | I2C data |
| LCD SCL | D1 | I2C clock |
| LCD VCC / GND | 5V / GND | Most I2C LCD backpacks run on 5V |
| Power | 5V USB (power bank or adapter) | — |
The two probes go into the soil. The capacitive moisture sensor measures how much water is in the soil, the DS18B20 measures soil temperature, and the TDS module measures how many dissolved solids are in the soil water. The ESP8266 reads the moisture and TDS sensors one at a time through the multiplexer, reads the temperature sensor directly, shows the values on the LCD, and every 20 seconds uploads them to ThingSpeak. Farmers and researchers can use the ThingSpeak dashboards to monitor trends and plan irrigation.
Install these libraries first from the Arduino IDE Library Manager (Sketch → Include Library → Manage Libraries):
Then upload this sketch. Replace the Wi-Fi name, password and ThingSpeak Write API Key with your own (see the ThingSpeak section below).
/*
ESP8266 IoT Soil Test Station
Reads soil moisture + TDS through a CD74HC4067 multiplexer on A0,
soil temperature from DS18B20, shows values on Serial + LCD,
and uploads them to ThingSpeak.
Board: NodeMCU 1.0 (ESP-12E).
Moisture sensor -> mux channel 0, TDS sensor -> mux channel 1.
DS18B20 data pin D4, with a 4.7k ohm resistor to 3.3V.
*/
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define S0 D5
#define S1 D6
#define S2 D7
#define S3 D8
#define ONE_WIRE_BUS D4
const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";
const char* thingSpeakApiKey = "YOUR_THINGSPEAK_WRITE_API_KEY";
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature tempSensor(&oneWire);
LiquidCrystal_I2C lcd(0x27, 16, 2);
int readMux(int channel) {
digitalWrite(S0, bitRead(channel, 0));
digitalWrite(S1, bitRead(channel, 1));
digitalWrite(S2, bitRead(channel, 2));
digitalWrite(S3, bitRead(channel, 3));
delay(10);
return analogRead(A0);
}
void setup() {
Serial.begin(115200);
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
tempSensor.begin();
lcd.init();
lcd.backlight();
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected");
}
void loop() {
int moistureRaw = readMux(0);
int tdsRaw = readMux(1);
tempSensor.requestTemperatures();
float temperatureC = tempSensor.getTempCByIndex(0);
// Capacitive moisture sensor: dry soil reads high, wet soil reads low.
// 550 = dry (in air), 300 = wet (in water). Calibrate both numbers
// for your own sensor and soil — they vary between sensors.
int moisturePercent = map(moistureRaw, 550, 300, 0, 100);
moisturePercent = constrain(moisturePercent, 0, 100);
// TDS: rough estimate from the analog reading.
// Calibrate against a known solution for a real measurement.
float tdsValue = (tdsRaw / 1023.0) * 1000.0;
Serial.print("Moisture: ");
Serial.print(moisturePercent);
Serial.print(" % | TDS: ");
Serial.print(tdsValue);
Serial.print(" ppm | Temp: ");
Serial.print(temperatureC);
Serial.println(" C");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Moist: ");
lcd.print(moisturePercent);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("TDS:");
lcd.print((int)tdsValue);
lcd.print("ppm");
// ThingSpeak accepts an update at most every 15 seconds
static unsigned long lastUpload = 0;
if (millis() - lastUpload > 20000 && WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = "http://api.thingspeak.com/update?api_key=" + String(thingSpeakApiKey)
+ "&field1=" + String(moisturePercent)
+ "&field2=" + String((int)tdsValue)
+ "&field3=" + String(temperatureC);
http.begin(url);
Serial.print("ThingSpeak response: ");
Serial.println(http.GET());
http.end();
lastUpload = millis();
}
delay(2000);
}
YOUR_THINGSPEAK_WRITE_API_KEY, along with your Wi-Fi name and password.Firebase and Blynk are also possible for this project, but the sketch above uses ThingSpeak because it is the simplest free option for a student demo: no app to build, just a web dashboard.
The NodeMCU board and soil moisture sensor are available at QKZEE Technologies, Shop 20, 1st Floor, Al-Khalil Center, Hall Road, Lahore, and online at qkzeetech.com with delivery all over Pakistan — see the links in the components table above. The DS18B20, TDS module, multiplexer and LCD are available at the Hall Road shop on request.
If a sensor does not respond, or your ThingSpeak charts stay empty, message us — tell us what the Serial Monitor shows (copy the exact lines) and which step you are on, and we will help you find the fault.
Chat on WhatsApp Watch on YouTube
Why do I need a multiplexer?
The NodeMCU has only one analog input pin (A0), but the moisture sensor and the TDS sensor are both analog. The CD74HC4067 multiplexer connects both to A0 and the ESP8266 switches between them, reading one at a time.
Can I use an ESP32 instead of the NodeMCU?
Yes. The ESP32 has several analog pins, so you can connect the moisture sensor to one ADC pin and the TDS sensor to another, and skip the multiplexer. The code changes slightly — the multiplexer part is removed.
Why does the temperature show −127°C?
That is the DS18B20's "not connected" value. Check the data wire on D4 and the 4.7k ohm resistor between the data pin and 3V3.
Why do the moisture and TDS readings look wrong?
Both need calibration. The moisture numbers in the code (550 dry, 300 wet) are starting points — measure your own sensor in air and in water and use those numbers. The TDS number is a rough estimate until you calibrate it against a known solution.
Do I need the LCD?
No, it is optional. Without it, all readings appear on the Serial Monitor and on ThingSpeak. The display only helps if you want to show values on the station itself.
Can I use Firebase or Blynk instead of ThingSpeak?
Yes. The sensors and wiring stay the same — only the cloud code changes. ThingSpeak is used here because it is the simplest free dashboard for a student demo.
Building an ESP8266 IoT soil test station with moisture, temperature and TDS sensing is a strong final year project: it combines sensors, a multiplexer, Wi-Fi, and cloud dashboards in one portable device. Follow the wiring above, calibrate the sensors for your own soil, and you will have live soil data on ThingSpeak to show your teachers and judges.