I already pre- heated the MQ-2 all the components in my circuit is functional. The code seems to work but the sensor is not detecting lpg gas or smoke. I even changed the sensor used a new one. Currently the arduino is directly connected to my laptop for power supply.
int red_led=3;//indicates gas
int green_led=2;//indicates normal
int smokeA0 = A0;//indicates sensor is connected to A5
int buzzer=4;
int sensorThres=400;//The threshold value
void setup() {
pinMode(red_led,OUTPUT);//red led as output
pinMode(green_led,OUTPUT);//green led as output
pinMode(buzzer,OUTPUT);
pinMode(A0,INPUT);//sensor as input
Serial.begin(9600);//starts the code
}
void loop() {
int gas_avalue=analogRead(smokeA0);//reads sensor value
if (gas_avalue > sensorThres) {//sees if it reached threshold value
digitalWrite(red_led, HIGH);//turns on red led
digitalWrite(green_led, LOW);//turns off green led
digitalWrite(buzzer, HIGH);//turn buzzer on
}
else//if it hasn't reached threshold value
{
digitalWrite(red_led, LOW);//turns red led off
digitalWrite(green_led, HIGH);//turn green led on
digitalWrite(buzzer, LOW);//turn buzzer off
}
delay(100);//delay 0.1 sec
}
Please post the loop() function showing how you tried to print it. Have you checked that the baud rate of the Serial monitor matches that of the Serial.begin() ?
I am actually new to this so I didnt understand however I updated the code
int red_led=3;//indicates gas
int green_led=2;//indicates normal
int smokeA0 = A0;//indicates sensor is connected to A5
int buzzer=4;
int sensorThres=400;//The threshold value
void setup() {
pinMode(red_led,OUTPUT);//red led as output
pinMode(green_led,OUTPUT);//green led as output
pinMode(buzzer,OUTPUT);
pinMode(A0,INPUT);//sensor as input
Serial.begin(9600);//starts the code
}
void loop() {
int gas_avalue=analogRead(smokeA0);//reads sensor value
if (gas_avalue > sensorThres) {//sees if it reached threshold value
digitalWrite(red_led, HIGH);//turns on red led
digitalWrite(green_led, LOW);//turns off green led
digitalWrite(buzzer, HIGH);//turn buzzer on
Serial.println("GAS LEAKING");
}
else//if it hasn't reached threshold value
{
digitalWrite(red_led, LOW);//turns red led off
digitalWrite(green_led, HIGH);//turn green led on
digitalWrite(buzzer, LOW);//turn buzzer off
Serial.println("NORMAL");
}
delay(100);//delay 0.1 sec
}
In serial monitor it prints "Normal"
Is this what you are referring to?
No. What I had in mind was printing the value returned by the sensor before you test it. Like this
void loop()
{
int gas_avalue = analogRead(smokeA0); //reads sensor value
Serial.print("sensor value : "); //print the sensor value
Serial.println(gas_avalue);
if (gas_avalue > sensorThres)
{ //sees if it reached threshold value
etc, etc