Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

ESP32-Based Beehive Monitoring System with Battery Power

Image of ESP32-Based Beehive Monitoring System with Battery Power

Circuit Documentation

Summary

This document provides a detailed overview of a beehive monitoring system. The system uses an ESP32 microcontroller to interface with various sensors and modules, including a DHT22 temperature and humidity sensor, an HX711 weighing sensor module, an MQ135 air quality sensor, an SW-420 vibration sensor, and a buzzer module. The system is powered by a 18650 Li-ion battery managed by a TP4056 charging module.

Component List

  1. ESP32 (30 pin)

    • Description: A powerful microcontroller with built-in Wi-Fi and Bluetooth capabilities.
    • 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
  2. 18650 Li-ion Battery

    • Description: A rechargeable lithium-ion battery.
    • Pins: -, +
  3. TP4056

    • Description: A lithium battery charger module with overcharge protection.
    • Pins: OUT-, B-, B+, OUT+, IN-, IN+
  4. SW-420 Vibration Sensor

    • Description: A vibration sensor module that detects vibrations and outputs a digital signal.
    • Pins: vcc, Ground, Digital output
  5. HX711 Weighing Sensor Module

    • Description: A precision 24-bit analog-to-digital converter (ADC) designed for weigh scales and industrial control applications.
    • Pins: B-, B+, A-, A+, E-, E+, VCC, CK/TX, DO/RX, GND
  6. DHT22

    • Description: A digital temperature and humidity sensor.
    • Pins: GND, VCC, DAT
  7. MQ135

    • Description: An air quality sensor that detects a wide range of gases.
    • Pins: VCC, GND, A0, D0
  8. Buzzer Module

    • Description: A simple buzzer module that can be controlled by a microcontroller to produce sound.
    • Pins: GND, Vcc, I/O
  9. Load Cell - Red/white/black/green

    • Description: A load cell used for measuring weight.
    • Pins: E+, A-, E-, A+

Wiring Details

ESP32 (30 pin)

  • D32: Connected to A0 of MQ135
  • GND: Connected to GND of DHT22, Buzzer Module, SW-420 Vibration Sensor, HX711 Weighing Sensor Module, MQ135, and OUT- of TP4056
  • Vin: Connected to OUT+ of TP4056
  • D23: Connected to I/O of Buzzer Module
  • D22: Connected to CK/TX of HX711 Weighing Sensor Module
  • D21: Connected to DO/RX of HX711 Weighing Sensor Module
  • D5: Connected to Digital output of SW-420 Vibration Sensor
  • D4: Connected to DAT of DHT22
  • 3V3: Connected to VCC of DHT22, Buzzer Module, SW-420 Vibration Sensor, HX711 Weighing Sensor Module, and MQ135

18650 Li-ion Battery

  • -: Connected to B- of TP4056
  • +: Connected to B+ of TP4056

TP4056

  • OUT+: Connected to Vin of ESP32
  • OUT-: Connected to GND of ESP32
  • B-: Connected to - of 18650 Li-ion Battery
  • B+: Connected to + of 18650 Li-ion Battery

SW-420 Vibration Sensor

  • vcc: Connected to 3V3 of ESP32
  • Ground: Connected to GND of ESP32
  • Digital output: Connected to D5 of ESP32

HX711 Weighing Sensor Module

  • CK/TX: Connected to D22 of ESP32
  • DO/RX: Connected to D21 of ESP32
  • GND: Connected to GND of ESP32
  • VCC: Connected to 3V3 of ESP32

DHT22

  • GND: Connected to GND of ESP32
  • VCC: Connected to 3V3 of ESP32
  • DAT: Connected to D4 of ESP32

MQ135

  • VCC: Connected to 3V3 of ESP32
  • GND: Connected to GND of ESP32
  • A0: Connected to D32 of ESP32

Buzzer Module

  • GND: Connected to GND of ESP32
  • Vcc: Connected to 3V3 of ESP32
  • I/O: Connected to D23 of ESP32

Documented Code

#include "HX711.h"          // HX711 library for load cell
#include "DHT.h"            // DHT sensor library

// --- Pin Definitions ---
// DHT22 Sensor
#define DHTPIN 4            // GPIO 4 for DHT22
#define DHTTYPE DHT22       // Define the type of DHT sensor

// HX711 Load Cell
#define HX711_DT 21         // GPIO 21 for DT (DOUT) of HX711
#define HX711_SCK 22        // GPIO 22 for SCK (CLK) of HX711

// MQ135 Air Quality Sensor
#define MQ135_PIN A0        // Analog pin A0 for MQ135

// SW-420 Vibration Sensor
#define VIBRATION_PIN 5     // GPIO 5 for vibration sensor output

// Buzzer
#define BUZZER_PIN 23       // GPIO 23 for buzzer control

// --- Sensor Objects ---
HX711 scale;                // Initialize HX711
DHT dht(DHTPIN, DHTTYPE);   // Initialize DHT sensor

// --- Threshold Values ---
float weightThreshold = 50.0;  // Example weight threshold in kg
float tempThreshold = 35.0;    // Temperature threshold in °C
float humidityThreshold = 70.0; // Humidity threshold in %
int airQualityThreshold = 300;  // Air quality threshold (arbitrary ADC value)

// --- Setup Function ---
void setup() {
  // Initialize serial communication
  Serial.begin(115200);

  // Initialize DHT sensor
  dht.begin();

  // Initialize HX711 load cell
  scale.begin(HX711_DT, HX711_SCK);
  scale.set_scale();  // Calibrate your load cell and set the scale factor here
  scale.tare();       // Reset scale to zero

  // Initialize pins
  pinMode(VIBRATION_PIN, INPUT);    // Vibration sensor as input
  pinMode(BUZZER_PIN, OUTPUT);      // Buzzer as output

  // Ready message
  Serial.println("Beehive Monitoring System Initialized!");
}

// --- Loop Function ---
void loop() {
  // --- Read DHT22 Sensor Data ---
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  if (isnan(temperature) || isnan(humidity)) {
    Serial.println("Failed to read from DHT22 sensor!");
  } else {
    Serial.print("Temperature: ");
    Serial.print(temperature);
    Serial.println(" °C");
    Serial.print("Humidity: ");
    Serial.print(humidity);
    Serial.println(" %");
  }

  // --- Read Load Cell Data ---
  float weight = 0.0;
  if (scale.is_ready()) {
    weight = scale.get_units(10);  // Average of 10 readings
    Serial.print("Weight: ");
    Serial.print(weight);
    Serial.println(" kg");
  } else {
    Serial.println("HX711 not ready");
  }

  // --- Read MQ135 Air Quality Sensor ---
  int airQuality = analogRead(MQ135_PIN);
  Serial.print("Air Quality: ");
  Serial.println(airQuality);

  // --- Read Vibration Sensor ---
  int vibration = digitalRead(VIBRATION_PIN);
  Serial.print("Vibration Detected: ");
  Serial.println(vibration == HIGH ? "Yes" : "No");

  // --- Trigger Buzzer on Threshold Breach ---
  if (temperature > tempThreshold || 
      humidity > humidityThreshold || 
      weight > weightThreshold || 
      airQuality > airQualityThreshold || 
      vibration == HIGH) {
    Serial.println("ALERT! Threshold Breached!");
    digitalWrite(BUZZER_PIN, HIGH);  // Turn buzzer ON
    delay(1000