Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

ESP32-Based Automated Composting System with Soil and Gas Sensors

View and Edit Project In Cirkit DesignerImage of ESP32-Based Automated Composting System with Soil and Gas Sensors

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

  1. Water Pump 12V

    • Description: A 12V water pump used to irrigate the compost.
    • Pins: +, -
  2. 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
  3. Soil RK520-01

    • Description: A sensor used to measure soil moisture and temperature.
    • Pins: GND, VCC, Hum_out, Temp_out
  4. 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
  5. MQ-7 Breakout

    • Description: A sensor used to measure carbon monoxide levels.
    • Pins: VCC, GND, DO, AO
  6. Comment

    • Description: Placeholder for comments in the circuit design.
    • Pins: None

Wiring Details

Water Pump 12V

Relay 4 Channel 5V

Soil RK520-01

ESP32 (30 pin)

MQ-7 Breakout

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.