Reading multiple analog input

Hi everyone, I am coding to read from flame sensor and temperature sensor attached to analog input Arduino, but the result from temperature sensor was incorrect and unstable (sometimes 29 celcius, sometimes 40 celcius), while flamse sensor voltage result no problem. Can anyone help me to identify which part of coding doing wrong? Will be very much appreciated!! Thank you!

p/s attached picture below shows the temperature was inconstant..

//declare variables
int buzzer = 8;
int led = 7;
float tempC;
float voltage;
int tempPin = A0; // Temp sensor plugged analog pin 0
int flamePin = A2; //flame sensor plugged analog pin 2

// write setup function
void setup ()
{
  Serial.begin(9600);
  
  pinMode(flamePin, INPUT); //analog flame sensor 
  pinMode (led, OUTPUT);  //led pin
  pinMode(buzzer, OUTPUT);  //buzzer pin
  pinMode(tempPin, INPUT); //analog temperature sensor
}

void loop ()
{
  //print temperature reading
  tempC = analogRead(tempPin); // taking the temp pin reading and setting it equal to tempC variable
  tempC = (5.0*tempC*100.0)/1024.0; // will convert the analog input to a temperature in celcius
  Serial.print((byte)tempC); //will output the converted temperature to pc


  //print flame voltage reading
  voltage = analogRead (flamePin);
  voltage = (5.0*voltage)/1024.0;    
  Serial.println ((byte)voltage);

//if temperature exceed, if flame exist, there is fire
  if (tempC>35[img]http://[img][img][/img][/img][/img])
  {
      if (voltage <= 3)  //there is flame
      {
        digitalWrite (led, HIGH);
        digitalWrite (buzzer, HIGH);
      }
       else  //No flame detected
      {
        digitalWrite (led, LOW);
        digitalWrite (buzzer, LOW);
      }
  }  
  else  //No flame detected
   {
     digitalWrite (led, LOW);
     digitalWrite (buzzer, LOW);
   }
    

  delay (3000);
}

rough.PNG

It might help if you tell us what sensors you connected to the Arduino and how you've done that (wiring diagram).

Edit your post and insert code tags, the forum system is manipulating code otherwise (as you probably can see).

Hi thanks for your reply, i have added the code tag. I apologized I don't have the circuit diagram right now. Basically, I'm using buzzer, led as analog ouput, LM35 (temperature sensor) and flamse sensor as analog input. I want to switch on led and buzzer if temperature exceed value I set and there is flame detected.

I am using Flame sensor from Cytron-Cytron.io - Simplifying Digital Making

and temperature sensor LM35. The problem is not fix yet. The temperature result was inaccurate when adding flame sensor together. Please someone help me.

Create a circuit diagram and post it. A reasonably sized photograph of a pencil and paper drawing is OK.

Hi thanks for looking this. My circuit diagram as attached. Sorry for the ugly drawing. Thanks!

Second attachment is the Flame Sensor Module Getting Started Guide. The temperature value seems crash with the flame sensor value, and I not sure why! sigh,

scan.PNG

FlameSensorModuleGettingStartedGuide.docx (137 KB)

 Serial.print((byte)tempC); //will output the converted temperature to pc

Why do you convert the calculated float into an integer for the output?

What's the '01' in the screen shot and why is that gone in the source code?

  if (tempC>35[img]http://[img][img][/img][/img][/img])

Correct that error too.

Hi thanks for the reply, 01 indicate my xbee 1. I deleted the coding but forgot to change the picture. But it doesn't influence the result. Well, I am trying the most basic coding, and yet the result of temperature also not correct.

// write setup function
void setup ()
{
  Serial.begin(9600);
}

void loop ()
{
  int b= analogRead(A1);  //LM35 sensor
  Serial.println(b);  

  delay (3000);
  
  
  int a=analogRead(A0); //flame sensor
  Serial.println(a);
  
  delay(3000);
}

1019... is flame analog value; 61... is temperature analog value
The value obtained is not constant.

tt.PNG

How is the Arduino powered? Try putting a 0.1µF capacitor between A0 and GND to filter possible spikes.

When I put capacitor, problem fix!! Thanks for the idea! Appreciated so much!

someone can help me out how to transmit a multisensor signal value through xbee to main coordinator

First don't need to config the analog pins as input

void setup ()
{
Serial.begin(9600);

pinMode(flamePin, INPUT); //analog flame sensor
pinMode (led, OUTPUT); //led pin
pinMode(buzzer, OUTPUT); //buzzer pin
pinMode(tempPin, INPUT); //analog temperature sensor
}

and double check wiring the connection.

Remove the two lines.

i had the same problem some time ago, i could fix it using this code

nt safeAnalogRead(int pin)
{
 int x = analogRead(pin);  // make an initial reading to set up the ADC
 delay(10);                // let the ADC stabilize
 x = analogRead(pin);      // toss the first reading and take one we will keep
 delay(10);                // delay again to be friendly to future readings
 return x;
}