Blackpill using Arduino IDE
Good day all, I would like to ask, can analogRead be used in the Arduino IDE to read the reading from the MQ-2 gas sensor? When I unplug the sensor, the reading should be 0, but the LCD still shows a value. How can I fix it?
Here are my code,
#include <LiquidCrystal_I2C.h>
#define gasSensorPin PA0
#define buzzerPin PB2
LiquidCrystal_I2C lcd(0x27,16,2);
void setup() {
// put your setup code here, to run once:
lcd.init();
lcd.backlight();
Serial.begin(9600);
pinMode(gasSensorPin,INPUT);
pinMode(buzzerPin,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int sensorValue = analogRead(gasSensorPin); // Read the analog value from the gas sensor
// Display the readings on the LCD
lcd.setCursor(0, 0);
lcd.print("Gas Sensor:");
lcd.print(sensorValue);
if(sensorValue>15){
digitalWrite(buzzerPin,HIGH);
}
else{
digitalWrite(buzzerPin,LOW);
}
delay(1000);
}
thanks.
