Tanishq
// Pin Definitions
const int gasSensorPin = A0; // MQ-2 sensor analog output
const int buzzerPin = 8; // Buzzer pin
const int ledPin = 9; // LED pin
const int batteryPin = A1; // Battery voltage monitoring pin
// Threshold Values
const int gasThreshold = 400; // Adjust based on your sensor's calibration
const float batteryLowVoltage = 3.5; // Low battery voltage threshold (in volts)
void setup() {
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // For debugging and monitoring
}
void loop() {
// Read gas sensor value
int gasValue = analogRead(gasSensorPin);
// Gas detection logic
if (gasValue > gasThreshold) {
digitalWrite(ledPin, HIGH); // Turn on LED
digitalWrite(buzzerPin, HIGH); // Turn on buzzer
Serial.println("Gas Detected!");
} else {
digitalWrite(ledPin, LOW); // Turn off LED
digitalWrite(buzzerPin, LOW); // Turn off buzzer
}
// Battery voltage monitoring
int batteryValue = analogRead(batteryPin);
float batteryVoltage = (batteryValue * 5.0) / 1023.0 * 2; // Adjust if using a voltage divider
Serial.print("Battery Voltage: ");
Serial.println(batteryVoltage);
if (batteryVoltage < batteryLowVoltage) {
Serial.println("Warning: Battery Voltage Low!");
}
delay(500); // Delay for stability
}
Comments
Post a Comment