cancel
Showing results for 
Search instead for 
Did you mean: 

Blackpill using Arduino IDE

weiiii22
Associate

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions

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.

View solution in original post

4 REPLIES 4
MBertocchi
Associate III

This is only STM32 Community! Not Arduino.

weiiii22
Associate

BLACKPILL is stm32 also cannot ask?

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

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.