Skip to main content
Associate
Dec 27, 2024 12:39 PM UTC
Solved

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.

 

This topic has been closed for replies.
STTwo-32
STTwo-32Best answer
ST Technical Moderator
Dec 27, 2024 7:18 PM UTC

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.

4 replies

MBertocchi
Associate II
Dec 27, 2024 12:41 PM UTC

This is only STM32 Community! Not Arduino.

weiiii22Author
Associate
Dec 27, 2024 12:49 PM UTC

BLACKPILL is stm32 also cannot ask?

STTwo-32
STTwo-32Best answer
ST Technical Moderator
Dec 27, 2024 7:18 PM UTC

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.
MM..1
Super User
Dec 27, 2024 7:44 PM UTC

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.