Skip to main content
Associate
December 27, 2024
Solved

Blackpill using Arduino IDE

  • December 27, 2024
  • 3 replies
  • 1042 views

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.

 

Best answer by STTwo-32

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.

3 replies

MBertocchi
Associate II
December 27, 2024

This is only STM32 Community! Not Arduino.

weiiii22Author
Associate
December 27, 2024

BLACKPILL is stm32 also cannot ask?

STTwo-32
STTwo-32Answer
Technical Moderator
December 27, 2024

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
Chief III
December 27, 2024

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.