This circuit is designed to monitor environmental conditions using various sensors and respond accordingly. It includes an Arduino UNO microcontroller, temperature sensor (NTC), light-dependent resistor (LDR), gas sensor (MQ-2), ultrasonic sensor (HC-SR04), servo motor, piezo buzzer, and an LED. The system reads sensor values and triggers an alarm or controls the servo motor based on predefined thresholds.
#include <Servo.h>
// Pin Definitions
#define LDRpin A2 // LDR Sensor Pin
#define MQ2pin A4 // MQ-2 Gas Sensor Pin
#define TEMPpin A0 // Temperature Sensor (KY-028) Pin
const int TRIG_PIN = 3; // Ultrasonic Sensor TRIG pin
const int ECHO_PIN = 13; // Ultrasonic Sensor ECHO pin
const int SERVO_PIN = 11; // Servo Motor pin
const int BUZZER_PIN = 7; // Buzzer pin
const int LED_PIN = 10; // LED pin
// Thresholds
const int DISTANCE_THRESHOLD = 10; // Ultrasonic Distance Threshold in cm
const int TEMP_THRESHOLD = 50; // Temperature Threshold (raw value)
const int GAS_THRESHOLD = 200; // Gas Threshold (raw value)
const int LDR_THRESHOLD = 100; // LDR Threshold (raw value)
// Global Variables
Servo servo; // Servo Motor Object
float distance_cm; // Ultrasonic Sensor Value
float tempValue; // Temperature Sensor Value
float gasValue; // MQ-2 Gas Sensor Value
int LDRValue; // LDR Sensor Value
void setup() {
Serial.begin(9600); // Initialize Serial Monitor
// Pin Modes
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(LDRpin, INPUT);
pinMode(MQ2pin, INPUT);
pinMode(TEMPpin, INPUT);
servo.attach(SERVO_PIN);
servo.write(90); // Initial state: Servo closed
// Ensure initial states
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED_PIN, LOW);
}
void updateUltrasonic() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
float duration_us = pulseIn(ECHO_PIN, HIGH);
distance_cm = 0.017 * duration_us;
if (distance_cm <= 0 || distance_cm > 400) {
distance_cm = 400; // Out of range
}
}
void updateSensorValues() {
tempValue = analogRead(TEMPpin);
gasValue = analogRead(MQ2pin);
}
bool isAlarmConditionActive() {
return (tempValue < TEMP_THRESHOLD || gasValue > GAS_THRESHOLD);
}
void playAlarm() {
while (true) {
digitalWrite(LED_PIN, HIGH); // Turn on the LED
Serial.println("Alarm is active!");
// Check sensor values before each pattern
updateSensorValues();
if (!isAlarmConditionActive()) {
Serial.println("Conditions returned to normal - stopping alarm");
stopAlarm();
break;
}
// Pattern 1
for (int i = 0; i < 80; i++) {
digitalWrite(BUZZER_PIN, HIGH);
delay(1);
digitalWrite(BUZZER