This circuit is designed to interface an Arduino Leonardo with an LCD screen and an MQ-5 gas sensor, with the addition of a buzzer for alerting purposes. The Arduino Leonardo serves as the central processing unit, reading the analog output from the MQ-5 sensor to detect the presence of gases and displaying the sensor value on the LCD screen. If the gas concentration exceeds a certain threshold, the buzzer is activated as an alarm.
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define Buzzer 3
#define Sensor A0
void setup() {
Serial.begin(9200);
lcd.init();
lcd.backlight();
pinMode(Buzzer, OUTPUT);
Serial.println("MQ5 Heating Up!");
delay(20000);
}
void loop() {
int value = analogRead(Sensor);
lcd.setCursor(0, 0);
lcd.print("VALUE:");
lcd.print(value);
lcd.print(" ");
if (value > 400) {
digitalWrite(Buzzer, HIGH);
lcd.setCursor(0, 1);
lcd.print("GAS DETECTED!");
} else {
digitalWrite(Buzzer, LOW);
lcd.setCursor(0, 1);
lcd.print(" ");
}
}
LiquidCrystal_I2C
library, which is used to control the LCD screen via the I2C protocol.setup()
function, the serial communication is started, and the LCD screen is initialized with its backlight turned on. The buzzer pin is set as an output, and a delay is included to allow the MQ-5 sensor to heat up.loop()
function continuously reads the analog value from the MQ-5 sensor and displays it on the LCD. If the value exceeds 400, the buzzer is turned on, and a warning message is displayed on the LCD. If the value is below the threshold, the buzzer is turned off, and the warning message is cleared.