2024-12-27 04:39 AM - last edited on 2024-12-27 11:40 AM by MM..1
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.
Solved! Go to Solution.
2024-12-27 11:18 AM
Hello @weiiii22 and welcome to the ST Community.
If you are using an STM32 with the STM32duino, you can report your problems in this forum: https://www.stm32duino.com/
Best Regards.
STTwo-32.
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2024-12-27 04:41 AM
This is only STM32 Community! Not Arduino.
2024-12-27 04:49 AM
BLACKPILL is stm32 also cannot ask?
2024-12-27 11:18 AM
Hello @weiiii22 and welcome to the ST Community.
If you are using an STM32 with the STM32duino, you can report your problems in this forum: https://www.stm32duino.com/
Best Regards.
STTwo-32.
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2024-12-27 11:44 AM
Ohh absurd replies...
int sensorValue = analogRead(gasSensorPin);
have no way to get if value is valid or sensor is connected. Control this is your job as designer.
Exist more ways , most simple is use range of valid values and all other remove as invalid etc.