Interfacing LM35 and MQ2 Gas Sensor with Arduino Uno

Hello, I made a project that can detect temperature and smoke using LM35 and MQ2 gas sensor . But after uploading the code my LM35 didn’t function properly and continuously giving wrong value but my MQ-2 gave correct values.
Here is my code-

 #include<MQ2.h>
float vout;
float temp;
int gs= A0;
int smoke;
MQ2 mq2(gs); 
void setup() 
{ 
  Serial.begin(9600);
  mq2.begin();
 }

 void loop()
 {

smoke =mq2.readSmoke();
vout=analogRead(A1) ; 
  temp= (vout*500)/1023 ;
 Serial.print("Temperature in Degree Celsius = ");
 Serial.print(temp);
 Serial.println();
 Serial.print("SMOKE: ");
Serial.println(smoke);
 delay(1000);
 }

Here are my circuit and output. The temperature value supposed to be 30-35 degree celcius but… -

Capture2

What should I do to solve this LM35 problem?? Please help. I have been trying to solve this for last 3 days.Please help!

Print the value of vout. You may be getting integer overflow - try changing that 500 to 500.0.

1 Like

Well, today I have tried this again and you know what I have got all correct values. I don’t know what just happened- I have used same connection and same code. Thank you allCapture3

Strictly speaking, it’s not the same code. What do you get if you run the stuff you posted in #1?

#include<MQ2.h>
float vout;
float temp;
int gs= A0;
int smoke;
MQ2 mq2(gs); 
void setup() 
{ 
  Serial.begin(9600);
  mq2.begin();
 }

 void loop()
 {

smoke = mq2.readSmoke();
vout = analogRead(A1); ; 
  float volt =  (vout*500)/1023;
  temp = volt; 
  Serial.print("Vout: ");
  Serial.println(vout);
 Serial.print("Temperature in Degree Celsius = ");
 Serial.print(temp);
 Serial.println();
 Serial.print("SMOKE: ");
Serial.println(smoke);
 delay(1000);
 }

I just print vout and the rest are the same. That’s why I said the same code

Capture4


This is the output of my first code that’s given in my post. Thank you all :smiling_face_with_three_hearts: :heartbeat:

The 161mA current drawn by the 31Ω heater in the MQ2 gas sensor is dragging the AREF voltage down and causing the LM35 error. After the sensor warms up to operating temperature and the heater cuts off, the LM35 reading goes back to normal.

1 Like

Thanks a lot. I was searching the reason behind it and thanks for explaining the matter to me :heartbeat: :heartbeat: :heartbeat:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.