How to Build ESP8266 IoT Soil Test Station in Lahore, Pakistan

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.

ESP8266 IoT Soil Test Station Circuit Diagram

What This Station Measures

  • Soil moisture — how wet the soil is, with a capacitive soil moisture sensor
  • Soil temperature — with a waterproof DS18B20 sensor pushed into the soil
  • TDS — Total Dissolved Solids, an estimate of dissolved salts and nutrients in soil water, with an analog TDS module
  • Cloud dashboard — all three readings upload to ThingSpeak, so you can watch them on a web dashboard from your phone or laptop

One Important Hardware Fact

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.

Components Required

ComponentSpecificationPrice / link
NodeMCUESP8266 CH340 Wi-Fi boardNodeMCU ESP8266 CH340 Wi-Fi Board — Rs 590
Soil moisture sensorCapacitive typeSoil Moisture Sensor for Arduino — Rs 155
Temperature sensorDS18B20 waterproof—
TDS sensorAnalog TDS module—
MultiplexerCD74HC4067 16-channel analog—
Resistor4.7k ohm (pull-up for DS18B20)—
Display (optional)16×2 LCD with I2C—
PowerRechargeable 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.

Wiring Connections

ComponentESP8266 PinNotes
Multiplexer SIGA0The single analog input — both analog sensors are read through this pin
Multiplexer S0D5Channel select
Multiplexer S1D6Channel select
Multiplexer S2D7Channel select
Multiplexer S3D8Channel select
Multiplexer VCC / GND3V3 / GND—
Multiplexer ENGNDEnable pin must be grounded
Moisture sensor outputMultiplexer channel 0—
TDS sensor outputMultiplexer channel 1—
Moisture + TDS VCC3V3—
DS18B20 dataD4OneWire data
DS18B20 VCC / GND3V3 / GND—
4.7k ohm resistorBetween DS18B20 data (D4) and 3V3Pull-up — without it the sensor will not reply
LCD SDAD2I2C data
LCD SCLD1I2C clock
LCD VCC / GND5V / GNDMost I2C LCD backpacks run on 5V
Power5V USB (power bank or adapter)—

How the Station Works

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.

Arduino Code for ESP8266 IoT Soil Test Station

Install these libraries first from the Arduino IDE Library Manager (Sketch → Include Library → Manage Libraries):

  • OneWire by Paul Stoffregen
  • DallasTemperature by Miles Burton
  • LiquidCrystal_I2C by Frank de Brabander (only if you fit the LCD)

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

Cloud Integration with ThingSpeak

  1. Create a free account at thingspeak.com and make a new channel with 3 fields: Field 1 = Soil Moisture, Field 2 = TDS, Field 3 = Soil Temperature.
  2. Open the channel's API Keys tab and copy the Write API Key.
  3. Paste it into the sketch where it says YOUR_THINGSPEAK_WRITE_API_KEY, along with your Wi-Fi name and password.
  4. Upload the sketch, open the Serial Monitor at 115200 baud, and watch the readings appear. After about 20 seconds, open your ThingSpeak channel — the charts will start filling in.

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.

Testing the Station

  1. Test the temperature sensor first. Upload the sketch and open the Serial Monitor. You should see a temperature near the air temperature. If it shows −127°C, the DS18B20 is not wired correctly — check D4 and the 4.7k ohm pull-up resistor.
  2. Test the moisture sensor next. Hold the probe in the air (dry — moisture should read near 0%), then dip it in a glass of water (wet — near 100%). If the numbers are backwards or stuck, check the multiplexer wiring (S0–S3 on D5–D8, SIG on A0, EN grounded).
  3. Test the TDS sensor last. Dip the TDS probe in clean water, then in salty water — the ppm value should rise clearly. The absolute number is a rough estimate until you calibrate it.
  4. Test the cloud upload. Once all three readings look sensible, check your ThingSpeak channel. If the charts stay empty, check the Wi-Fi password and the API key first.

Troubleshooting

  • Temperature shows −127°C: the DS18B20 is not connected. Check the data wire on D4 and the 4.7k ohm resistor between data and 3V3.
  • Moisture or TDS reads 0 or 1023 all the time: check the multiplexer — SIG must go to A0, S0–S3 to D5–D8, and the EN pin must be connected to GND. A floating EN pin disables the multiplexer.
  • Both sensors show the same value: the channel select is not working — check S0–S3 wiring. Channel 0 is moisture, channel 1 is TDS.
  • LCD shows nothing: check SDA → D2, SCL → D1, and the I2C address — most LCD backpacks use 0x27, some use 0x3F. Try the other address if the screen stays blank.
  • Wi-Fi never connects: check the Wi-Fi name and password (both are case-sensitive), and use a 2.4 GHz network — the ESP8266 does not connect to 5 GHz Wi-Fi.
  • ThingSpeak response is 0: no internet on the Wi-Fi network, or the API key is wrong. A response of 200 means the update worked.

Applications

  • Smart farming — know when to water, instead of guessing
  • Agricultural research — log soil data over days and weeks
  • School and university projects — a complete IoT system with sensors, a microcontroller and a cloud dashboard
  • Final year project base — add a water pump relay for automatic irrigation, or GPS tagging for field mapping

Where to Buy the Components

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.

Still Stuck?

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

FAQs

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.

Conclusion

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.