ESP32-Based Smart Soil Monitoring System with RS485 and Multiple Sensors
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to interface an ESP32 microcontroller with various sensors including a DS18B20 temperature sensor, a YL-69 soil moisture sensor, a pH meter, an RS485 communication module, and an NPK soil sensor. The ESP32 reads data from these sensors and communicates with the NPK soil sensor via RS485.
Component List
ESP32 Wroom Dev Kit
- Description: A powerful microcontroller with Wi-Fi and Bluetooth capabilities.
- Pins: NOP, EN, VP, VN, GPIO 34, GPIO 35, GPIO 32, GPIO 33, GPIO 25, GPIO 26, GPIO 27, GPIO 14, GPIO 13, V5, GPIO 23, GPIO 22, TXD, RXD, GPIO 21, GND, GPIO 19, GPIO 18, GPIO 5, GPIO 17, GPIO 16, GPIO 4, GPIO 0, GPIO 15, 3V3
Humidity YL-69
- Description: A soil moisture sensor.
- Pins: A0, D0, GND, VCC
DS18B20
- Description: A digital temperature sensor.
- Pins: signal, GND, vcc
Resistor
- Description: A 4700 Ohms resistor.
- Pins: pin1, pin2
- Properties: Resistance: 4700 Ohms
PH Meter
- Description: A sensor to measure pH levels.
- Pins: Signal, VCC, GND
RS485
- Description: A module for RS485 communication.
- Pins: VCC, RO, RE, GND, B, A, DE, DI
NPK Soil Sensor (mini)
- Description: A sensor to measure NPK levels in soil.
- Pins: VCC, GND, A, B
Wiring Details
ESP32 Wroom Dev Kit
- GPIO 5 connected to Resistor pin1
- GPIO 34 connected to PH Meter Signal
- GPIO 35 connected to Humidity YL-69 A0
- GPIO 18 connected to RS485 RE and RS485 DE
- GPIO 17 connected to RS485 DI
- GPIO 16 connected to RS485 RO
- V5 connected to RS485 VCC and NPK Soil Sensor (mini) VCC
- NOP connected to PH Meter GND, DS18B20 GND, Humidity YL-69 GND, RS485 GND, NPK Soil Sensor (mini) GND
- NOP connected to PH Meter VCC, DS18B20 vcc, Humidity YL-69 VCC
Humidity YL-69
- A0 connected to ESP32 GPIO 35
- GND connected to ESP32 NOP
- VCC connected to ESP32 NOP
DS18B20
- signal connected to Resistor pin2
- GND connected to ESP32 NOP
- vcc connected to ESP32 NOP
Resistor
- pin1 connected to ESP32 GPIO 5
- pin2 connected to DS18B20 signal
PH Meter
- Signal connected to ESP32 GPIO 34
- VCC connected to ESP32 NOP
- GND connected to ESP32 NOP
RS485
- VCC connected to ESP32 V5
- RO connected to ESP32 GPIO 16
- RE connected to ESP32 GPIO 18
- GND connected to ESP32 NOP
- B connected to NPK Soil Sensor (mini) B
- A connected to NPK Soil Sensor (mini) A
- DE connected to ESP32 GPIO 18
- DI connected to ESP32 GPIO 17
NPK Soil Sensor (mini)
- VCC connected to ESP32 V5
- GND connected to ESP32 NOP
- A connected to RS485 A
- B connected to RS485 B
Code Documentation
#include <OneWire.h>
#include <DallasTemperature.h>
#include <HardwareSerial.h>
// DS18B20
#define ONE_WIRE_BUS 5
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// Soil Moisture Sensor
#define SOIL_MOISTURE_PIN 35
// pH Sensor
#define PH_PIN 34
// RS485
HardwareSerial RS485Serial(1);
#define RS485_RX 16
#define RS485_TX 17
#define RS485_DE_RE 18
void setup() {
Serial.begin(115200);
// DS18B20
sensors.begin();
// RS485
RS485Serial.begin(9600, SERIAL_8N1, RS485_RX, RS485_TX);
pinMode(RS485_DE_RE, OUTPUT);
digitalWrite(RS485_DE_RE, LOW); // Set to receive mode initially
}
void loop() {
// Read DS18B20
sensors.requestTemperatures();
float tempC = sensors.getTempCByIndex(0);
Serial.print("DS18B20 Temperature: ");
Serial.print(tempC);
Serial.println(" *C");
// Read Soil Moisture Sensor
int soilMoistureValue = analogRead(SOIL_MOISTURE_PIN);
Serial.print("Soil Moisture Value: ");
Serial.println(soilMoistureValue);
// Read pH Sensor
int phValue = analogRead(PH_PIN);
Serial.print("pH Value: ");
Serial.println(phValue);
// Read NPK Sensor via RS485
// Set to transmit mode
digitalWrite(RS485_DE_RE, HIGH);
RS485Serial.write("Your command to NPK sensor"); // Replace with actual command
delay(100); // Wait for the command to be sent
// Set to receive mode
digitalWrite(RS485_DE_RE, LOW);
if (RS485Serial.available()) {
String response = RS485Serial.readString();
Serial.print("NPK Sensor Response: ");
Serial.println(response);
}
delay(2000);
}
This code initializes the sensors and the RS485 communication module, reads data from the sensors, and prints the data to the serial monitor. The RS485 communication is used to send and receive data from the NPK soil sensor.