This circuit is designed around an Arduino Nano microcontroller and includes a variety of sensors and output devices. The sensors include a DHT11 temperature and humidity sensor, a GP2Y1010AU0F dust sensor, and an MQ135 gas sensor. Output devices include a buzzer and three LEDs (red, yellow, and green) for visual alerts. A 1.3" OLED display is used to show sensor readings and alert statuses. The circuit is intended to monitor environmental conditions and provide visual and audible alerts based on predefined threshold values for gas levels, dust density, and temperature.
// Include necessary libraries
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <Wire.h>
#include <DHT.h>
// Pin definitions
#define MQ135_PIN A0
#define DHT_PIN 2
#define BUZZER_PIN 7
#define GREEN_LED_PIN 4
#define YELLOW_LED_PIN 5
#define RED_LED_PIN 6
#define DUST_LED_PIN 3
#define DUST_SENSOR_PIN A1
// DHT sensor setup
#define DHT_TYPE DHT11
DHT dht(DHT_PIN, DHT_TYPE);
// OLED display setup
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // OLED display
// Threshold values for alarms
const int MQ135_THRESHOLD = 300; // Example value for gas level
const int DUST_THRESHOLD = 150; // Example value for dust density
const float TEMP_THRESHOLD = 30.0; // Example value for temperature in °C
void setup() {
// Initialize Serial Monitor
Serial.begin(9600);
// Initialize pins
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(YELLOW_LED_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(DUST_LED_PIN, OUTPUT);
// Initialize DHT sensor
dht.begin();
// Initialize OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
// Start with LEDs and buzzer off
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(YELLOW_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
}
void loop() {
// Read gas level from MQ-135
int mq135_value = analogRead(MQ135_PIN);
Serial.print("Gas level: ");
Serial.println(mq135_value);
// Read dust density from GP2Y1010AU0F
digitalWrite(DUST_LED_PIN, LOW); // Turn on IR LED for dust sensor
delayMicroseconds(280);
int dust_value = analogRead(DUST_SENSOR_PIN);
delayMicroseconds(40);
digitalWrite(DUST_LED_PIN, HIGH); // Turn off IR LED
Serial.print("Dust density: ");
Serial.println(dust_value);
// Read temperature and humidity from DHT11
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
Serial.print("Temperature: ");
Serial.println(temperature);
Serial.print("Humidity: ");
Serial.println(humidity);
// Display data on OLED
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("Gas Level: ");
display.println(mq135_value);
display.print("Dust: ");
display.println(dust_value);
display.print("Temp: ");
display.print(temperature);
display.println(" C");
display.print("Humidity: ");
display.print(humidity);
display.println(" %");
display.display();
// LED and buzzer alerts based on thresholds
if (mq135_value > MQ135_THRESHOLD || dust_value > DUST_THRESHOLD || temperature > TEMP_THRESHOLD) {
digitalWrite(RED_LED_PIN, HIGH); // Turn on red LED for danger
digitalWrite(BUZZER_PIN, HIGH); // Sound buzzer
} else if (mq135_value > MQ135_THRESHOLD / 2 || dust_value > DUST_THRESHOLD / 2) {
digitalWrite(YELLOW_LED_PIN, HIGH); // Turn on yellow LED for caution
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
} else {
digitalWrite(GREEN_LED_PIN, HIGH); // Turn on green LED for safe condition
digitalWrite(YELLOW_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
}
// Small delay before next loop
delay(2000);
}
This code is designed to read sensor data, display it on an OLED screen, and activate LEDs and a buzzer based on predefined threshold values. It includes initialization for the DHT11 sensor and the OLED display, as well as continuous monitoring and alerting in the main loop.