#include <Wire.h>
#include <BH1750.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Pin Definitions
const int buttonPin = 13; // Pin where the button is connected
const int ledPin = 12; // Pin where the LED is connected
const int buzzerPin = 14; // Pin where the buzzer is connected
const int oneWireBus = 2; // GPIO pin for DS18B20 data
const int irSensorPin = 4; // GPIO pin for IR sensor signal
const int mq7Pin = 5; // GPIO pin for MQ7 analog output (ADC)
// Thresholds
const float tempThreshold = 30.0; // Temperature threshold (Celsius)
const int lightThreshold = 50; // Light intensity threshold (lux)
const int gasThreshold = 400; // CO concentration threshold (arbitrary value based on the sensor's output range)
// Set up the OneWire and DallasTemperature instances for DS18B20
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);
// Set up the BH1750 light sensor
BH1750 lightMeter;
// Variable to store button state
int buttonState = 0;
float temperatureC = 0.0;
uint16_t lux = 0; // Light intensity in lux
int irState = 0; // Variable to store IR sensor state
int mq7Value = 0; // Variable to store MQ7 sensor reading (analog value)
void setup() {
// Initialize the LED, buzzer, and button pin as output/input
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Use internal pull-up resistor
pinMode(irSensorPin, INPUT); // Set IR sensor pin as input
// Initialize both the LED and buzzer as OFF
digitalWrite(ledPin, LOW);
digitalWrite(buzzerPin, LOW);
// Start the serial communication
Serial.begin(115200);
// Start the DS18B20 sensor
sensors.begin();
// Start the BH1750 sensor
Wire.begin(); // Start I2C communication
if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
Serial.println(F("BH1750 started successfully"));
} else {
Serial.println(F("Error initializing BH1750"));
}
}
void loop() {
// Read the state of the button
buttonState = digitalRead(buttonPin);
// Read the temperature from the DS18B20 sensor
sensors.requestTemperatures(); // Request temperature reading
temperatureC = sensors.getTempCByIndex(0); // Get the temperature in Celsius
// Read the light intensity from the BH1750 sensor
lux = lightMeter.readLightLevel(); // Get light level in lux
// Read the state of the IR sensor
irState = digitalRead(irSensorPin); // HIGH when no obstacle, LOW when an obstacle is detected
// Read the gas concentration from the MQ7 sensor (analog value)
mq7Value = analogRead(mq7Pin);
// Display the readings in the Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.print(" °C, Light: ");
Serial.print(lux);
Serial.print(" lux, IR State: ");
Serial.print(irState == LOW ? "Object detected" : "No object");
Serial.print(", CO Level (MQ7): ");
Serial.println(mq7Value);
// Button logic
if (buttonState == LOW) {
// Turn the LED and buzzer ON if the button is pressed
digitalWrite(ledPin, HIGH);
digitalWrite(buzzerPin, HIGH);
}
// Temperature threshold logic
else if (temperatureC > tempThreshold) {
// Turn the LED and buzzer ON if temperature exceeds the threshold
digitalWrite(ledPin, HIGH);
digitalWrite(buzzerPin, HIGH);
}
// Light threshold logic
else if (lux < lightThreshold) {
// Turn the LED and buzzer ON if light intensity is below the threshold
digitalWrite(ledPin, HIGH);
digitalWrite(buzzerPin, HIGH);
}
// IR sensor logic
else if (irState == LOW) {
// Turn the LED and buzzer ON if the IR sensor detects an object
digitalWrite(ledPin, HIGH);
digitalWrite(buzzerPin, HIGH);
}
// Gas sensor logic
else if (mq7Value > gasThreshold) {
// Turn the LED and buzzer ON if CO concentration exceeds the threshold
digitalWrite(ledPin, HIGH);
digitalWrite(buzzerPin, HIGH);
}
else {
// Turn the LED and buzzer OFF if none of the conditions are met
digitalWrite(ledPin, LOW);
digitalWrite(buzzerPin, LOW);
}
// Small delay to avoid excessive readings
delay(500);
}