Build it step by step
0 of 8 doneWire the supply
Power the probe from 12 V and the ESP32 from the buck at 5 V.
Wire the MAX485
Connect DI, RO through the divider, and DE and RE together.
Connect the bus
A to A and B to B.
Set the speed and registers
Check the datasheet for baud, address and registers.
Upload
The serial monitor shows N, P and K or an error.
Test in moist soil
Push the probe in fully.
Compare
Take a soil lab result and compare with the probe.
Mount
Keep the electronics dry above the soil.
How do you build a soil NPK reader over RS485?
An RS485 soil probe reports nitrogen, phosphorus and potassium, and an ESP32 reads it with Modbus and shows the values. You need ESP32 DevKit V1, NPK soil sensor (RS485), RS485 to TTL module (MAX485), LM2596 Buck Module, 12 V 2 A DC adapter, SSD1306 OLED Display, Resistor 1 kΩ and Resistor 2 kΩ. It takes 8 steps and about 180 minutes.
- Level
- Advanced
- Time
- About 180 min
- Parts
- 8
Parts list
- Development BoardsBuyESP32 DevKit V1 × 1
- SensorsBuy
- InterfacesBuy
- PowerBuy
- PowerBuy
- DisplaysBuy
- PassivesBuyResistor 1 kΩ × 1
- PassivesBuyResistor 2 kΩ × 1
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| 12 V adapter + | LM2596 IN+ and the probe's brown wire | Probe power, typically 9 to 24 V |
| 12 V adapter - | LM2596 IN- and the probe's black wire | Common ground |
| LM2596 OUT+ | ESP32 VIN and MAX485 VCC | Set to 5.0 V with a meter first |
| LM2596 OUT- | ESP32 GND and MAX485 GND | Ground |
| MAX485 DI | ESP32 GPIO17 | Transmit to the bus |
| MAX485 DE and RE joined | ESP32 GPIO4 | High to transmit, low to listen |
| MAX485 RO | 1 kΩ resistor then ESP32 GPIO16, with 2 kΩ from GPIO16 to GND | Divider brings 5 V down to about 3.3 V |
| MAX485 A | Probe A wire (yellow) | Bus A |
| MAX485 B | Probe B wire (blue) | Bus B |
| OLED VCC and GND | ESP32 3V3 and GND | Power |
| OLED SDA and SCL | ESP32 GPIO21 and GPIO22 | I2C |
Code
Modbus RTU is a request and reply protocol: the ESP32 sends the address, function code 3 (read registers), the start register and count, and two bytes of CRC with the low byte first, and the probe answers with its own CRC, which the code checks before using any number. The register numbers and speed above suit many cheap probes but not all; check your probe's datasheet. Drive DE and RE together so the MAX485 transmits or listens. The MAX485 output is 5 V, so it goes through a divider to the ESP32. The probe should be pushed fully into moist soil. These probes give estimates, not laboratory results, and readings change with moisture and temperature, so check against a soil laboratory test before changing a fertiliser plan. Keep the electronics dry and not in the soil. Arduino IDE with the ESP32 boards package, board "ESP32 Dev Module".
Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// Modbus RTU read of three registers from an NPK probe. Library: Adafruit SSD1306 and GFX.
// Many probes use address 1, 4800 baud, function 3, registers 0x001E to 0x0020. Check your datasheet.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const byte DE_RE = 4;
const long BAUD = 4800;
uint16_t crc16(const uint8_t *d, int n) {
uint16_t crc = 0xFFFF;
for (int i = 0; i < n; i++) {
crc ^= d[i];
for (int b = 0; b < 8; b++) crc = (crc & 1) ? (crc >> 1) ^ 0xA001 : crc >> 1;
}
return crc;
}
bool readNPK(uint16_t &n, uint16_t &p, uint16_t &k) {
uint8_t req[8] = {0x01, 0x03, 0x00, 0x1E, 0x00, 0x03, 0, 0};
uint16_t c = crc16(req, 6);
req[6] = c & 0xFF; req[7] = c >> 8; // CRC low byte first
while (Serial2.available()) Serial2.read();
digitalWrite(DE_RE, HIGH);
Serial2.write(req, 8); Serial2.flush();
digitalWrite(DE_RE, LOW);
uint8_t resp[11]; int got = 0; unsigned long t0 = millis();
while (millis() - t0 < 500 && got < 11) { if (Serial2.available()) resp[got++] = Serial2.read(); }
if (got < 11 || resp[0] != 1 || resp[1] != 3 || resp[2] != 6) return false;
uint16_t rc = resp[9] | (resp[10] << 8);
if (crc16(resp, 9) != rc) return false; // reject a reply with a bad checksum
n = (resp[3] << 8) | resp[4];
p = (resp[5] << 8) | resp[6];
k = (resp[7] << 8) | resp[8];
return true;
}
void setup() {
Serial.begin(115200);
pinMode(DE_RE, OUTPUT); digitalWrite(DE_RE, LOW);
Serial2.begin(BAUD, SERIAL_8N1, 16, 17);
Wire.begin(21, 22);
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
oled.setTextColor(WHITE);
}
void loop() {
uint16_t n, p, k;
oled.clearDisplay(); oled.setTextSize(1);
if (readNPK(n, p, k)) {
oled.setCursor(0, 0); oled.print("N "); oled.print(n); oled.print(" mg/kg");
oled.setCursor(0, 14); oled.print("P "); oled.print(p); oled.print(" mg/kg");
oled.setCursor(0, 28); oled.print("K "); oled.print(k); oled.print(" mg/kg");
oled.setCursor(0, 48); oled.print("Estimate only");
Serial.printf("N %u P %u K %u\n", n, p, k);
} else {
oled.setCursor(0, 20); oled.print("No valid reply");
Serial.println("no valid reply");
}
oled.display();
delay(3000);
}What the serial monitor should show
Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual ESP32 Dev Module with its sensors at their default values. Your numbers will follow your real readings, but the format should match.
no valid reply
Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Probe in air | Values near zero |
| Probe in moist soil | Plausible N, P and K |
| Wire disconnected | No valid reply message |
| Corrupted reply | Rejected by the CRC check |
If it does not work
No valid reply
Check A and B are not swapped and the baud and address match.
Garbage
Check the DE and RE control and the 5 V to 3.3 V divider.
Values all zero
The probe may need to be in wet soil.
OLED blank
Check I2C pins and address 0x3C.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the soil NPK reader over RS485?
ESP32 DevKit V1, NPK soil sensor (RS485), RS485 to TTL module (MAX485), LM2596 Buck Module, 12 V 2 A DC adapter, SSD1306 OLED Display, Resistor 1 kΩ and Resistor 2 kΩ.
How long does the soil NPK reader over RS485 take to build?
About 180 minutes for a advanced maker, in 8 steps.
How do I know the soil NPK reader over RS485 works?
Probe in air: Values near zero. Probe in moist soil: Plausible N, P and K. Wire disconnected: No valid reply message.
No valid reply: what should I check?
Check A and B are not swapped and the baud and address match.
Garbage: what should I check?
Check the DE and RE control and the 5 V to 3.3 V divider.
Values all zero: what should I check?
The probe may need to be in wet soil.
OLED blank: what should I check?
Check I2C pins and address 0x3C.
