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

Arduino UNO-Based Environmental Monitoring System with Gas, Temperature, Pressure, and Flow Sensors

View and Edit Project In Cirkit DesignerImage of Arduino UNO-Based Environmental Monitoring System with Gas, Temperature, Pressure, and Flow Sensors

Circuit Documentation

Summary

This circuit is designed to interface an Arduino UNO with various sensors including a gas sensor (MQ-2), a water flow meter (YF-S201), a temperature sensor (DS18B20), and a pressure sensor (BMP280). The Arduino UNO reads data from these sensors and outputs the readings via the serial monitor.

Component List

  1. Arduino UNO

    • Description: A microcontroller board based on the ATmega328P.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  2. MQ-2

    • Description: A gas sensor used to detect various gases including LPG, i-butane, propane, methane, alcohol, hydrogen, and smoke.
    • Pins: GND, VCC, ANALOG, Digital
  3. YF-S201 Water Flow Meter

    • Description: A sensor used to measure the flow rate of water.
    • Pins: SIG, GND, VCC
  4. DS18B20

    • Description: A digital temperature sensor.
    • Pins: signal, GND, vcc
  5. BMP280

    • Description: A barometric pressure sensor.
    • Pins: VCC, GND, SCL, SDA, CSB, SDO
  6. Resistor

    • Description: A 4700 Ohms resistor.
    • Pins: pin1, pin2
    • Properties: Resistance: 4700 Ohms

Wiring Details

Arduino UNO

MQ-2

YF-S201 Water Flow Meter

DS18B20

BMP280

Resistor

Code Documentation

// Include necessary libraries
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include <SoftwareSerial.h>

// Pin definitions
#define ONE_WIRE_BUS 2 // DS18B20 connected to pin 2
#define FLOW_SENSOR_PIN 3 // YF-S201 connected to pin 3
#define MQ2_PIN A0 // MQ2 gas sensor connected to A0

// Global variables for sensors
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature tempSensor(&oneWire);
Adafruit_BMP280 bmp;
volatile int flowPulseCount = 0;
float flowRate = 0;
unsigned long flowMillis = 0;
float MQ2_baseline = 0;

// Calibration constants
#define MQ2_CALIBRATION_TIME 60000 // 1 minute
#define FLOW_CONVERSION_FACTOR 7.5 // Adjust as per datasheet

void flowPulseCounter() {
  flowPulseCount++;
}

void setup() {
  Serial.begin(9600);

  // DS18B20 setup
  tempSensor.begin();

  // BMP280 setup
  if (!bmp.begin(0x76)) { // I2C address for BMP280
    Serial.println("Could not find BMP280 sensor! Check wiring.");
    while (1);
  }

  // YF-S201 setup
  pinMode(FLOW_SENSOR_PIN, INPUT);
  attachInterrupt(digitalPinToInterrupt(FLOW_SENSOR_PIN), flowPulseCounter, FALLING);

  // MQ2 setup
  pinMode(MQ2_PIN, INPUT);
  MQ2_baseline = calibrateMQ2();

  Serial.println("Setup complete!");
}

float calibrateMQ2() {
  Serial.println("Calibrating MQ2 sensor...");
  unsigned long startTime = millis();
  float totalValue = 0;
  int readings = 0;

  while (millis() - startTime < MQ2_CALIBRATION_TIME) {
    totalValue += analogRead(MQ2_PIN);
    readings++;
    delay(100);
  }

  float baseline = totalValue / readings;
  Serial.print("MQ2 baseline: ");
  Serial.println(baseline);
  return baseline;
}

void loop() {
  // Temperature reading (DS18B20)
  tempSensor.requestTemperatures();
  float temperature = tempSensor.getTempCByIndex(0);

  // Pressure and Altitude reading (BMP280)
  float pressure = bmp.readPressure() / 100.0; // Convert to hPa
  float altitude = bmp.readAltitude(1013.25); // Use sea level pressure in hPa

  // Flow rate calculation (YF-S201)
  if (millis() - flowMillis >= 1000) {
    noInterrupts();
    flowRate = (flowPulseCount / FLOW_CONVERSION_FACTOR); // Liters per minute
    flowPulseCount = 0;
    flowMillis = millis();
    interrupts();
  }

  // Gas concentration (MQ2)
  float gasValue = analogRead(MQ2_PIN);
  float gasConcentration = (gasValue / MQ2_baseline) * 100; // Percentage relative to baseline

  // Output readings to Serial
  Serial.print("Temperature (C): ");
  Serial.println(temperature);

  Serial.print("Pressure (hPa): ");
  Serial.println(pressure);

  Serial.print("Altitude (m): ");
  Serial.println(altitude);

  Serial.print("Flow Rate (L/min): ");
  Serial.println(flowRate);

  Serial.print("Gas Concentration (%): ");
  Serial.println(gasConcentration);

  delay(1000); // Delay to prevent overloading the serial output
}

This code initializes and reads data from the DS18B20 temperature sensor, BMP280 pressure sensor, YF-S201 water flow meter, and MQ-2 gas sensor. The readings are then output to the serial monitor.