I am using two MQ-x analogue gas sensors to measure concentration of gases... however, I would like to detect if gas sensor is connected to Arduino or not.
How to do that?
For example, I am also using DHT-22 sensor for humidity and temperature and I can detect if sensor is present (or reading failed) with this simple code:
float humidity = dht.readHumidity();
float temperature1 = dht.readTemperature();
if (isnan(humidity) || isnan(temperature1)) {
// sensor is not ready or not present
}
Is it possible to do something similar with MQ-x gas sensors?
#include <SoftwareSerial.h> // import the serial library
const int MQxPin = A0; // GAS sensor is connected to pin A0
int gasvalue; // variable to store raw reading from gas sensor
void setup()
{
Serial.begin(9600); // initialize serial port (9600 bps)
}
void loop()
{
// Read the gas value:
gasvalue = analogRead(MQxPin1);
Serial.println(gasvalue);
}
Connect a 1M resistor between your analog pin and GND.
That's big enough to not affect the readings of your sensor (the normal load resistor is around 10k), and it will pull down your pin to 0V if the sensor is not connected. So an ADC reading of 0 tells you the sensor is not connected, as the normal output of this sensor should be somewhere between 1 and 4V.