Build a LoRaWAN Soil Sensor With ESP32 and TTN

DIY Electronics
Date:December 4, 2026
Topic:
Build a LoRaWAN Soil Sensor With ESP32 and TTN
⏱ 3 min read

Soil moisture data shouldn't cost a monthly cell bill. With an ESP32, a LoRaWAN module, and The Things Network (TTN), you can deploy sensors across acres of field or garden and read them from anywhere for free. This guide walks through hardware selection, low-power firmware, and TTN integration so your first node survives months on a LiFePO4 cell.

Hardware Bill of Materials

ComponentModelNotes
MCUESP32-WROOM-32EDeep sleep < 10 µA
LoRa RadioRFM95W / SX1276868/915 MHz matching region
Soil SensorCapacitive v1.2Corrosion resistant, 3.3 V
Power3.2 V LiFePO4 14500Flat discharge curve, no regulator
Antenna1/4 wave whipCut to 868 or 915 MHz
EnclosureIP67 project boxCable glands for probe leads
💡
TipSkip the voltage regulator. The ESP32 and RFM95W both run happily at 3.2 V, saving 30% quiescent current.

Wiring Essentials

Connect RFM95W SPI pins to ESP32 HSPI (GPIO 13/12/14/15). Soil sensor signal goes to ADC1_CH0 (GPIO 36). Pull RFM95W RESET to GPIO 27 and DIO0 to GPIO 26 for LMIC interrupt handling. Power everything from the LiFePO4 positive rail; add a 10 µF ceramic + 100 ¿ electrolytic near the radio VCC pin.

Firmware: Deep Sleep Cycle

cpp
#include <lmic.h>
#include <hal/hal.h>
#include <driver/rtc_io.h>

#define SLEEP_MINUTES 15
RTC_DATA_ATTR uint32_t bootCount = 0;

void do_send(osjob_t* j) {
  float moisture = readSoil();
  uint16_t payload = (uint16_t)(moisture * 100);
  LMIC_setTxData2(1, (uint8_t*)&payload, 2, 0);
}

void setup() {
  esp_sleep_enable_timer_wakeup(SLEEP_MINUTES * 60ull * 1000000ull);
  rtc_gpio_isolate(GPIO_NUM_12); // MISO leak fix
  os_init(); LMIC_reset();
  // ... keys & session setup ...
  do_send(&sendjob);
}

void loop() {
  os_runloop_once();
  if (LMIC.opmode & OP_TXRXPEND) return;
  esp_deep_sleep_start();
}
ℹ️
NoteRTC_DATA_ATTR preserves bootCount across deep sleep. Isolate SPI pins before sleep to stop leakage current through the radio.

TTN Console Setup

Create an application in the TTN v3 console. Register an end device with OTAA: paste DevEUI, JoinEUI, and AppKey from your firmware. Set Frequency Plan to your region (EU868 / US915). Add a Payload Formatter (JavaScript) to decode the two-byte integer back to percentage:

javascript
function decodeUplink(input) {
  const raw = input.bytes;
  const moisture = ((raw[0] << 8) | raw[1]) / 100;
  return { data: { moisture_pct: moisture } };
}

Power Budget Reality Check

StateCurrentDurationCharge
Deep Sleep8 µA14.9 min2.0 mAh
Wake + Sensor12 mA2 s0.007 mAh
LoRa TX (SF7)110 mA1.2 s0.037 mAh
Total per cycle15 min2.04 mAh

A 600 mAh LiFePO4 yields ~290 cycles — roughly 72 days at 15-minute intervals. Doubling the interval to 30 minutes pushes lifetime past 140 days.

"

LoRaWAN isn't magic physics, but it feels like it when your sensor still reports after three months on a single AA-sized cell.

— Field test log, day 87

Next Steps

Add a BME280 for temperature and humidity on the same I2C bus. Implement ADR (Adaptive Data Rate) so the node backs off to SF12 only when gateway RSSI drops. For fleets, script TTN CLI to bulk-register devices and push firmware updates via multicast Class B windows.

⚠️
WarningDuty cycle limits (1% EU, 0.4% US sub-bands) cap minimum interval. Respect them or your gateway gets blocked.

✦

Flash the sketch, seal the box, bury the probe. Open the TTN live data tab and watch the first packet arrive. That’s your network — no SIM, no subscription, just physics and patience.

Share𝕏 Twitterin LinkedInin Whatsapp