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

Wi-Fi Controlled Environmental Monitoring System with Dual Stepper Motor Valve Actuation

View and Edit Project In Cirkit DesignerImage of Wi-Fi Controlled Environmental Monitoring System with Dual Stepper Motor Valve Actuation

Circuit Documentation

Summary

The circuit in question is designed to control two 28BYJ-48 stepper motors using ULN2003A breakout boards and to monitor environmental conditions using an MQ-135 air quality sensor and a KY-015 DHT11 temperature and humidity sensor. The brain of the circuit is a NodeMCU V3 ESP8266 microcontroller, which is responsible for processing sensor data, controlling the stepper motors, and managing WiFi connectivity. A resistor is also included in the circuit for interfacing with the DHT11 sensor.

Component List

28BYJ-48 Stepper Motor

ULN2003A Breakout Board

MQ-135 Sensor Air Quality

NodeMCU V3 ESP8266

Resistor

KY-015 DHT11

Wiring Details

28BYJ-48 Stepper Motor

ULN2003A Breakout Board

MQ-135 Sensor Air Quality

NodeMCU V3 ESP8266

Resistor

KY-015 DHT11

Documented Code

NodeMCU V3 ESP8266 Code

#include <DHT.h>
#include <Stepper.h>
#include <ESP8266WiFi.h>

#define DHTPIN 2        // DHT11 data pin
#define DHTTYPE DHT11   // DHT11 sensor

// Stepper Motor Pins for Motor 1 (right valve)
#define IN1_R 8
#define IN2_R 9
#define IN3_R 10
#define IN4_R 11

// Stepper Motor Pins for Motor 2 (left valve)
#define IN1_L 4
#define IN2_L 5
#define IN3_L 6
#define IN4_L 7

// MQ135 Gas Sensor Pin
#define MQ135_PIN A0 

// Stepper Motor configurations
const int STEPS_PER_REV = 2048; // Steps per revolution
Stepper motorRight(STEPS_PER_REV, IN1_R, IN2_R, IN3_R, IN4_R);
Stepper motorLeft(STEPS_PER_REV, IN1_L, IN2_L, IN3_L, IN4_L);

// DHT11 Sensor Setup
DHT dht(DHTPIN, DHTTYPE);

// WiFi Credentials
const char* ssid = "Your_SSID";
const char* password = "Your_PASSWORD";

float temperature;
int humidity;
int mq135Value;

// Steps for opening and closing the valve (30 degrees rotation)
const int valveOpenSteps = 171;

// Valve State Tracking
bool rightValveOpen = false;
bool leftValveOpen = false;

void setup() {
  Serial.begin(9600);
  
  // Initialize DHT11 sensor
  dht.begin();

  // Initialize stepper motors
  motorRight.setSpeed(10);
  motorLeft.setSpeed(10);

  // Initialize MQ135 sensor
  pinMode(MQ135_PIN, INPUT);

  // Initialize WiFi connection
  connectToWiFi();
}

void loop() {
  // Read temperature and humidity from DHT11
  temperature = dht.readTemperature();
  humidity = dht.readHumidity();
  
  // Read gas value from MQ135
  mq135Value = analogRead(MQ135_PIN);
  
  // Output sensor values to the serial monitor
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" C");

  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println(" %");

  Serial.print("MQ135 Gas Sensor Value: ");
  Serial.println(mq135Value);
  
  // Valve control logic based on temperature
  if (temperature >= 10 && temperature <= 35) {
    Serial.println("Normal temperature, opening right valve...");

    if (!rightValveOpen) {
      closeLeftValve();   // Ensure left valve is closed
      openRightValve();   // Open right valve
    }
    
  } else {
    Serial.println("Abnormal temperature, opening left valve...");

    if (!leftValveOpen) {
      closeRightValve();  // Ensure right valve is closed
      openLeftValve();    // Open left valve
    }
  }

  delay(2000); // Delay for 2 seconds before next reading
}

// Function to connect to WiFi
void connectToWiFi() {
  Serial.println("Connecting to WiFi...");
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting...");
  }

  Serial.println("Connected to WiFi");
}

// Function to open the right valve (rotate 30 degrees)
void openRightValve() {
  motorRight.step(valveOpenSteps);
  rightValveOpen = true;
  Serial.println("Right valve opened (30 degrees).");
}

// Function to close the right valve (rotate back to 0 degrees)
void closeRightValve() {
  if (rightValveOpen) {
    motorRight.step(-valveOpenSteps);
    rightValveOpen = false;
    Serial.println("Right valve closed (0 degrees).");
  }
}

// Function to open the left valve (rotate 30 degrees)
void openLeftValve() {
  motorLeft.step(valveOpenSteps);
  leftValveOpen = true;
  Serial.println("Left valve opened (30 degrees).");
}

// Function to close the left valve (rotate back to 0 degrees)
void closeLeftValve() {
  if (leftValveOpen) {
    motorLeft.step(-valveOpenSteps);
    leftValveOpen = false;
    Serial.println("Left valve closed (0 degrees).");
  }
}

Note: The code provided is for the NodeMCU V3 ESP8266 microcontroller and includes the logic for reading sensor data, controlling stepper motors, and managing WiFi connectivity. The code is structured with setup and loop functions, as well as additional functions for WiFi connection and valve control.