ਪੰਜਾਬੀਯੂਨੀpunjabiuni

Kisan · Tutorial

Build a water-table sensor for your AWD pani pipe

A waterproof ultrasonic sensor over your field's pani pipe that reads the water table every hour and tells you exactly when it has reached the 15cm Alternate Wetting and Drying threshold — instead of guessing from the bund.

Untested on hardwareThis design has not been built and tested on real hardware yet. The wiring and the code have been carefully reviewed — every ESP32 function was checked against Espressif’s current documentation, and an automated test pins the data it sends to what the server accepts — but nobody has yet run it on a real board in real soil. Expect to troubleshoot. If you build one, what you learn is genuinely useful.

Why bother

Punjab's groundwater is being drawn down faster than it recharges, and irrigation is the largest single reason why — mostly paddy. Alternate Wetting and Drying (AWD) lets a paddy field dry down between irrigations without costing yield, and it is a proven practice, not an experiment: IRRI and PAU trials both show real water savings with no yield loss when the field is re-flooded at the right moment.

The reason farmers over-irrigate is not stubbornness. A paddy field with no standing water still looks dry from the bund, and the water table under it may be 3cm down or 20cm down — there is no way to tell by looking. This sensor puts a number on it: it reads the water level inside your pani pipe every hour and reports centimetres of depth below the soil surface to your Kisan dashboard. The rule is one sentence: at 15cm, irrigate; below 15cm, wait.

This build extends the soil-moisture kit — same ESP32, same WiFi, same deep sleep, same account, same device key. If you already built that one, you are buying only the probe and the pipe this time.

What you need

Roughly ₹950–₹1200 all in with the recommended JSN-SR04T sensor and riser. Prices and links below were checked on 2026-08-29; where no product page could be confirmed, the exact search terms are given instead of a guessed link.

PartWhat to look forApprox.Where
ESP32 development boardAny ESP32-WROOM-32 DevKit, 30-pin or 38-pin, with a micro-USB socket. The same board the soil-moisture kit uses — if you already have one running, it can be moved here for a second sensor.₹399Robocraze
JSN-SR04T waterproof ultrasonic sensorSealed transducer on a 2.5m lead, 5V, 30mA, 0.5cm resolution. IMPORTANT: its minimum measurable distance is 25cm — mounted directly at the top of a standard pani pipe, the entire AWD operating band falls inside that blind zone (see the build note below). This kit assumes you are adding the ~₹100 riser described in step 2, which moves the sensor clear of the dead zone.₹329ElectronicsComp
PVC pipe riser, 110mm diameter, ~700mm length, with a couplerExtends your pani pipe so the sensor sits about 40cm above the soil instead of 10cm — clear of the JSN-SR04T's 25cm blind zone with headroom to spare. Use 110mm pipe, not 75mm: at 40-55cm the sensor's ~70° acoustic cone needs the wider bore to avoid hitting the pipe wall. Any local hardware shop.price not confirmedany electronics shop
search: 110mm PVC pipe coupler 700mm
IP65 ABS enclosure for the ESP32A sealed plastic box, roughly 80×82×85mm, for the electronics end. Must stay dry.₹115VSM Plast (IndiaMART)
search: IP65 ABS enclosure 80x82x85mm
Resistors for the ECHO voltage dividerOne 1kΩ and one 2kΩ resistor (any 1/4W). The JSN-SR04T's ECHO pin runs at 5V; the ESP32's GPIOs are 3.3V and not 5V tolerant. This pair steps ECHO down to a safe 3.33V. See the wiring diagram below.price not confirmedany electronics shop
search: 1k ohm 2k ohm resistor pack
Jumper wires, female-to-female2.54mm Dupont connectors, 20cm — the same wires the soil kit uses. No soldering required if you use the resistors as a small breadboard bridge, or solder the divider directly onto a short lead if you prefer a permanent joint.price not confirmedRobocraze (category page)
search: female to female jumper wires 40 pin 20cm
Micro-USB cableA DATA cable, not charge-only. Same requirement as the soil kit.price not confirmedany electronics shop
search: micro USB data cable
Power supplyA 5V USB phone charger and an outdoor socket, or a USB power bank with an 'always on' mode (deep sleep between readings is what makes a power bank last, and some banks switch off when current drops too low).price not confirmedany electronics shop
search: USB power bank always on mode
A02YYUW / DFRobot SEN0311 waterproof ultrasonic sensor · optionalThe alternative to the JSN-SR04T + riser above. UART interface, 3.3–5V, IP67, and a blind zone of only 3cm — so it can sit at the top of a standard pani pipe with no riser at all. About 5.6x the price of the JSN-SR04T; that difference buys the 22cm of dead zone. Needs a different sketch (UART, not the pulse-timing firmware below) — not covered by the download on this page.₹1840Fab.to.Lab

How it goes together

JSN-SR04Twaterproof ultrasonicESP32 DevKitany ESP32-WROOM-32 board5VVINpower (5V, not 3V3)GNDGNDgroundTRIGGPIO5triggerECHOGPIO18echo — through a 1k/2k divider1kΩ / 2kΩ
Four wires. The ECHO line needs a resistor divider (1kΩ to the GPIO, 2kΩ from the GPIO to GND) — the JSN-SR04T’s ECHO pin pulses at 5V, and the ESP32’s GPIOs are 3.3V and not 5V tolerant. Connecting it directly may appear to work and will degrade the pin.

Step by step

1. Install the Arduino IDE and the ESP32 board package

If you already set this up for the soil-moisture sensor, skip to step 2 — it is the same ESP32 board, same IDE, same board package.

Download the Arduino IDE from arduino.cc/en/software and install it. Open File → Preferences, paste https://espressif.github.io/arduino-esp32/package_esp32_index.json into 'Additional boards manager URLs', then install 'esp32' by Espressif Systems from Tools → Board → Boards Manager.

Go to Tools → Board and choose 'ESP32 Dev Module'.

2. Build the pani pipe with a riser

Follow IRRI's standard field water tube: sink a perforated pipe so 20cm sits below the soil surface, with the perforated section buried and the pipe standing proud above ground. Scoop the soil out of the inside so water genuinely finds its own level.

This is where the design differs from a plain pani pipe: fit a coupler and extend the pipe upward so the TOP of the pipe — where the sensor will sit — ends up about 40cm above the soil surface, not the usual 10cm. This is not decorative. The JSN-SR04T cannot reliably measure anything closer than 25cm, and a plain 10cm-tall pipe puts the entire AWD measuring range (5cm to 25cm from the sensor) inside that blind zone. The riser moves the whole operating band comfortably clear of it.

Cap the top of the pipe so rain and birds stay out, with a hole for the sensor to look straight down through.

3. Wire the sensor to the board

With the ESP32 unplugged: sensor 5V → ESP32 VIN (not 3V3 — this is a 5V module). Sensor GND → ESP32 GND. Sensor TRIG → ESP32 GPIO5, directly.

Sensor ECHO needs the voltage divider before it reaches the ESP32: connect the 1kΩ resistor between ECHO and GPIO18, then the 2kΩ resistor between GPIO18 and GND. This brings the module's 5V ECHO pulse down to a safe 3.33V at the pin. Skipping this may appear to work at first and will degrade the pin over time.

This is a different kind of pin choice from the soil sensor's GPIO34 rule — TRIG and ECHO are plain digital pins, not analog, so the ADC1-versus-ADC2-versus-WiFi problem documented in that sketch does not apply here.

4. Download the code and make your own config file

Download both files at the bottom of this page and put them in a folder named exactly 'awd-water-sensor' — the Arduino IDE requires the folder name to match the .ino file name.

Rename config.example.h to config.h. Open it and fill in your WiFi name, WiFi password, your device key from the Kisan dashboard, the server address, and a short name for this sensor.

Fill in SENSOR_HEIGHT_CM once you have physically measured it in step 5 below — do not guess it from the pipe lengths.

Never share your filled-in config.h. It holds your WiFi password and your device key.

5. Measure the mounting height and site the pipe

With the sensor fixed in place at the top of the riser, measure with a tape from the transducer face straight down to the soil surface. This is SENSOR_HEIGHT_CM — write it into config.h before you flash. Do not compute it from the pipe lengths: the pipe will not be perfectly vertical, and it will settle.

Site the pipe in a representative part of the field — not the lowest corner, not next to the water inlet.

Re-measure SENSOR_HEIGHT_CM after the first heavy irrigation. Pipes settle, and a stale height quietly shifts every reading by the same amount.

6. Flash and check the Serial Monitor

Plug the board into the computer with the USB data cable. In the Arduino IDE press the → (Upload) button, wait for 'Done uploading', then open Tools → Serial Monitor and set the speed to 115200.

You should see a raw distance in centimetres, then a water depth in centimetres below the soil surface. If it instead prints that the reading is inside the sensor's blind zone, the riser is not tall enough, or SENSOR_HEIGHT_CM is set wrong — check both before assuming the sensor is faulty.

The sketch refuses to post a reading it cannot trust — a blind-zone reading, a sensor timeout, or an implausible depth. That is deliberate: a missing reading on the dashboard is honest, a confidently wrong one is how a field gets irrigated on bad information.

7. Power it and put it in the field

Put the ESP32 in its sealed enclosure with wires exiting through a small hole facing downward, so rain cannot run in along them.

The sensor needs to stay within reach of your WiFi. Power it, and within a few minutes the first reading should appear on your Kisan dashboard. After that it wakes once an hour, sends one reading, and sleeps again.

The code

Both files below are the real files this site serves — what you see is byte-for-byte what the download gives you. Put them in one folder named awd-water-sensor, and rename config.example.h to config.h before uploading.

awd-water-sensor.inoDownload
Loading…
config.example.hDownload
Loading…

When it does not work

Every one of these has a specific cause. Work down the list — the first two account for most first-time failures.

Every reading says it is inside the blind zone.

Why: The sensor is not far enough above the soil — either the riser was not built tall enough, or SENSOR_HEIGHT_CM is set lower than the sensor is actually mounted.

Fix: Physically re-measure the height from transducer to soil with a tape and check it against MIN_VALID_CM (25 for the JSN-SR04T). The operating band needs the sensor at roughly 40cm to keep even a flooded (5cm) reading outside the 25cm dead zone.

The reading jumps around wildly between cycles.

Why: The ~70° acoustic cone is reflecting off the inside of a too-narrow pipe wall, or floating debris (a leaf, foam) is sitting on the water surface.

Fix: Use 110mm pipe, not 75mm, if you are using the riser. Check the pipe interior is clear of debris; the perforated section should let the water level equalise cleanly with the field.

It never reports a reading at all — Serial Monitor shows 'Only 0 of 9 pings returned an echo'.

Why: The sensor is not wired correctly, or the ECHO divider is missing and the pin has been damaged.

Fix: Re-check TRIG on GPIO5 and ECHO on GPIO18 through the 1kΩ/2kΩ divider. Confirm 5V (not 3.3V) is reaching the sensor's power pin.

The Arduino IDE cannot see the board at all — no port to select.

Why: Almost always a charge-only USB cable, or a missing USB driver.

Fix: Try a different micro-USB cable — one you know transfers files from a phone. If the port still does not appear, install the CP2102 or CH340 driver (the chip name is printed on the board next to the USB socket).

Upload fails with 'Failed to connect to ESP32'.

Why: Some boards need the BOOT button held while the upload starts.

Fix: Press Upload, and when the IDE prints 'Connecting......', hold the BOOT button until it starts writing. Release it then.

It never connects to WiFi — the Serial Monitor prints dots then 'timed out'.

Why: 5GHz network, wrong password, or the sensor is out of range.

Fix: Confirm the network is 2.4GHz. Re-check WIFI_SSID and WIFI_PASSWORD in config.h for stray spaces. Test the board indoors beside the router first; if it works there, the field position is out of range.

The server replies 401.

Why: The device key is wrong, or has been replaced.

Fix: Copy KISAN_API_KEY again from where you saved it when the account was set up. Keys are shown once and are not recoverable — if it is lost, a new one has to be issued.

The server replies 400.

Why: The reading itself was rejected.

Fix: The server's reply, printed in the Serial Monitor, names the field it did not like. A value outside 0–1000 for water_level almost always means the depth calculation went wrong — check SENSOR_HEIGHT_CM.

It works for a few days, then stops.

Why: Usually power, or the pipe has been knocked over.

Fix: Check the power bank has not switched itself off, and check the pipe is still upright — a fallen pipe will read as an out-of-range depth and the sketch will refuse to post rather than send a wrong number.

← Back to the Kisan dashboard

ਕਿਸਾਨ