ESP32-Based Automated Composting System with Soil and Gas Sensors
View and Edit Project In Cirkit Designer
Automated Composting System Documentation
Summary
This document provides a detailed overview of an automated composting system designed to monitor and manage compost conditions using an ESP32 microcontroller. The system integrates an MQ-7 sensor for carbon monoxide levels and a Soil RK520-01 sensor for soil moisture and temperature. It calculates the Carbon to Nitrogen (C:N) ratio and logs data to ThingSpeak. A 4-channel relay controls a 12V water pump, activating it for 5 seconds if soil moisture falls below 50%.
Component List
Water Pump 12V
- Description: A 12V water pump used to irrigate the compost.
- Pins: +, -
Relay 4 Channel 5V
- Description: A 4-channel relay module used to control the water pump.
- Pins: GND, IN1, IN2, IN3, IN4, VCC, COM1, COM2, COM3, COM4, NO1, NO2, NO3, NO4, NC1, NC2, NC3, NC4
Soil RK520-01
- Description: A sensor used to measure soil moisture and temperature.
- Pins: GND, VCC, Hum_out, Temp_out
ESP32 (30 pin)
- Description: A microcontroller used to process sensor data and control the relay.
- Pins: EN, VP, VN, D34, D35, D32, D33, D25, D26, D27, D14, D12, D13, GND, Vin, D23, D22, TX0, RX0, D21, D19, D18, D5, TX2, RX2, D4, D2, D15, 3V3
MQ-7 Breakout
- Description: A sensor used to measure carbon monoxide levels.
- Pins: VCC, GND, DO, AO
Comment
- Description: Placeholder for comments in the circuit design.
- Pins: None
Wiring Details
Water Pump 12V
- + connected to NO1 of Relay 4 Channel 5V
- - connected to GND (common ground with other components)
Relay 4 Channel 5V
- GND connected to GND (common ground with other components)
- IN1 connected to D19 of ESP32 (30 pin)
- IN2 connected to D19 of ESP32 (30 pin)
- NO1 connected to + of Water Pump 12V
- VCC connected to 3V3 of ESP32 (30 pin)
Soil RK520-01
- GND connected to GND (common ground with other components)
- VCC connected to 3V3 of ESP32 (30 pin)
- Hum_out connected to VN of ESP32 (30 pin)
- Temp_out connected to D34 of ESP32 (30 pin)
ESP32 (30 pin)
- GND connected to GND (common ground with other components)
- 3V3 connected to VCC of Soil RK520-01 and MQ-7 Breakout
- D19 connected to IN1 and IN2 of Relay 4 Channel 5V
- VN connected to Hum_out of Soil RK520-01
- D34 connected to Temp_out of Soil RK520-01
- D5 connected to AO of MQ-7 Breakout
MQ-7 Breakout
- GND connected to GND (common ground with other components)
- VCC connected to 3V3 of ESP32 (30 pin)
- AO connected to D5 of ESP32 (30 pin)
Code Documentation
/*
* Automated Composting System
* This Arduino Sketch uses an ESP32 to monitor and manage compost conditions.
* It integrates an MQ-7 sensor for carbon monoxide levels and a Soil RK520-01
* sensor for soil moisture and temperature. The system calculates the C:N ratio
* and logs data to ThingSpeak. A 4-channel relay controls a 12V water pump,
* activating it for 5 seconds if soil moisture falls below 50%.
*/
#include <WiFi.h>
#include <HTTPClient.h>
// Pin definitions
#define MQ7_AO_PIN 5
#define SOIL_HUM_PIN 36
#define SOIL_TEMP_PIN 34
#define RELAY_IN1_PIN 19
#define RELAY_IN2_PIN 19
// WiFi credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
// ThingSpeak settings
const char* server = "http://api.thingspeak.com/update";
const char* apiKey = "your_API_KEY";
void setup() {
Serial.begin(115200);
pinMode(MQ7_AO_PIN, INPUT);
pinMode(SOIL_HUM_PIN, INPUT);
pinMode(SOIL_TEMP_PIN, INPUT);
pinMode(RELAY_IN1_PIN, OUTPUT);
pinMode(RELAY_IN2_PIN, OUTPUT);
digitalWrite(RELAY_IN1_PIN, LOW);
digitalWrite(RELAY_IN2_PIN, LOW);
connectToWiFi();
}
void loop() {
int mq7Value = analogRead(MQ7_AO_PIN);
int soilHumValue = analogRead(SOIL_HUM_PIN);
int soilTempValue = analogRead(SOIL_TEMP_PIN);
float soilMoisture = map(soilHumValue, 0, 4095, 0, 100);
if (soilMoisture < 50) {
activatePump();
}
sendDataToThingSpeak(mq7Value, soilMoisture, soilTempValue);
delay(60000); // Delay for 1 minute
}
void connectToWiFi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void activatePump() {
digitalWrite(RELAY_IN1_PIN, HIGH);
delay(5000); // Pump on for 5 seconds
digitalWrite(RELAY_IN1_PIN, LOW);
}
void sendDataToThingSpeak(int mq7, float soilMoisture, int soilTemp) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String(server) + "?api_key=" + apiKey + "&field1=" + mq7 + "&field2=" + soilMoisture + "&field3=" + soilTemp;
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
Serial.println("Data sent to ThingSpeak");
} else {
Serial.println("Error sending data");
}
http.end();
} else {
Serial.println("WiFi not connected");
}
}
This code initializes the ESP32, sets up the pins, connects to WiFi, and continuously reads sensor data. If the soil moisture falls below 50%, it activates the water pump for 5 seconds. The data is then sent to ThingSpeak for logging and analysis.