Problem in running two sensor in arduino

Try this:

#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

const byte flamePin = 10; // choose the input pin (for Fire sensor)
const byte smokePin = 11; // choose the input pin (for smoke sensor)
const byte buzzer   = 13; // choose the pin for the Buzzer
const byte G_led    = 8;  // choose the pin for the Green LED
const byte R_led    = 9;  // choose the pin for the Red Led

byte flame_value;         // variable for reading the sensorpin status
byte smoke_value;         // variable for reading the sensorpin status

//**************************************************************************************
void setup()
{
  pinMode(flamePin, INPUT_PULLUP);
  pinMode(smokePin, INPUT_PULLUP);  
  
  pinMode(buzzer, OUTPUT); // declare Buzzer as output
  pinMode(R_led, OUTPUT); // declare Red LED as output
  pinMode(G_led, OUTPUT); // declare Green LED as output

  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("   WELCOME To   ");
  lcd.setCursor(0, 1);
  lcd.print("  Fire Detector ");
  delay(2000);
  lcd.clear();
  
} //END of setup()

//**************************************************************************************
void loop()
{
  flame_value = digitalRead(flamePin); // Digital input value
  smoke_value = digitalRead(smokePin); //Digital input value


  if (flame_value == LOW || smoke_value == LOW )   // check if the Fire variable is High
  {

    lcd.setCursor(0, 0);
    lcd.print("  alert  ");
    lcd.setCursor(0, 1);
    lcd.print("  fire  ");

    digitalWrite(buzzer, HIGH); // Turn LED on.
    digitalWrite(R_led, LOW);  // Turn LED on.
    digitalWrite(G_led, HIGH);   // Turn LED off.
    delay(100);
  }
  
  else   // check if the Fire variable is Low
  {
    lcd.setCursor(0, 0);
    lcd.print("...CONDITIONS...");
    lcd.setCursor(0, 1);
    lcd.print(".....Normal.....");
    digitalWrite(buzzer, LOW); // Turn LED off.
    digitalWrite(R_led, HIGH);  // Turn LED off.
    digitalWrite(G_led, LOW); // Turn LED on.

  }
  delay(100);
  
} //END of loop()

Hi,
Why have you got 10K resistors in series with each of the signal lines?

Tom... :grinning: :+1: :coffee: :australia:

If the 10ks are still there when connected to D10 and D11, do not use them.

See post #21

@LarryD @TomGeorge
It worked :smiley:

This is the Final code

#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

int flamePin = 10; // choose the input pin (for Fire sensor)
int smokePin = 11; // choose the input pin (for smoke sensor)
int buzzer = 13; // choose the pin for the Buzzer
int G_led = 8; // choose the pin for the Green LED
int R_led = 9; // choose the pin for the Red Led
int ESP = 12; //Pin to activate ESP


int flame_value; // variable for reading the sensorpin status
int smoke_value; // variable for reading the sensorpin status



void setup() {
  Serial.begin(115200);
  pinMode(flamePin, INPUT_PULLUP); // declare sensor as input
  pinMode(smokePin, INPUT_PULLUP); //declare sensor as input

  pinMode(buzzer, OUTPUT); // declare Buzzer as output
  pinMode(R_led, OUTPUT); // declare Red LED as output
  pinMode(G_led, OUTPUT); // declare Green LED as output
  pinMode(ESP, OUTPUT); //Pin 12 as output

  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("   WELCOME To   ");
  lcd.setCursor(0, 1);
  lcd.print("  Fire Detector ");
  delay(2000);
  lcd.clear();
}



void loop() {
  flame_value = digitalRead(flamePin); // Digital input value
  flame_value = digitalRead(flamePin); // Digital input value
  smoke_value = digitalRead(smokePin); //Digital input value
  smoke_value = digitalRead(smokePin); //Digital input value
  Serial.print("flame_value = ");
  Serial.print(flame_value);
  Serial.print("\t smoke_value = ");
  Serial.println(smoke_value);


  if (flame_value == LOW || smoke_value == LOW ) { // check if the Fire variable is High

    lcd.setCursor(0, 0);
    lcd.print("...CONDITIONS...");
    lcd.setCursor(0, 1);
    lcd.print(".....Normal.....");
    digitalWrite(buzzer, LOW); // Turn LED off.
    digitalWrite(R_led, HIGH);  // Turn LED off.
    digitalWrite(G_led, LOW); // Turn LED on.
    digitalWrite(ESP, LOW); //Turn ESP off

  }
  else { // check if the Fire variable is Low

    lcd.setCursor(0, 0);
    lcd.print("  alert  ");
    lcd.setCursor(0, 1);
    lcd.print("  fire  ");

    digitalWrite(buzzer, HIGH); // Turn LED on.
    digitalWrite(R_led, LOW);  // Turn LED on.
    digitalWrite(G_led, HIGH);   // Turn LED off.
    digitalWrite(ESP, HIGH); //Turn ESP on
    delay(250);



  }
  delay(250);
}

When nothing is detected the buzzer if off the lights are green.
both off

When only flame sensor detects flame the value turns 0, the buzzer is on and the lights get red

When only smoke sensor detects smoke the value turns 0, the buzzer is on and the light goes red

The Fire alarm system is running completely fine.

But can the values be inverted, like 0 when nothing detected and 1 when its detecting ?

No! No! Actually the simulator was not working without the resistors(I also don't know why), but I am not using 10k resistors anywhere in the circuit.

Why ?
You must be able to think in negative and positive logic when it comes to electronics.


Try the post #21 example also.

Do you understand why you had to use the DO pin on the sensors ?

And can you tell what does INPUT_PULLUP do?

INPUT_PULLUP places a ~50K resistor from 5V to the input pin in question.


Note, in this example, we use ALARM
if (flame_value == ALARM || smoke_value == ALARM )

#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

#define ALARM         LOW

const byte buzzer   = 13; // choose the pin for the Buzzer
const byte flamePin = 10; // choose the input pin (for Fire sensor)
const byte smokePin = 11; // choose the input pin (for smoke sensor)
const byte G_led    = 8;  // choose the pin for the Green LED
const byte R_led    = 9;  // choose the pin for the Red Led

byte flame_value;         // variable for reading the sensorpin status
byte smoke_value;         // variable for reading the sensorpin status

//**************************************************************************************
void setup()
{
  pinMode(flamePin, INPUT_PULLUP);
  pinMode(smokePin, INPUT_PULLUP);  
  
  pinMode(buzzer, OUTPUT); // declare Buzzer as output
  pinMode(R_led, OUTPUT); // declare Red LED as output
  pinMode(G_led, OUTPUT); // declare Green LED as output

  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("   WELCOME To   ");
  lcd.setCursor(0, 1);
  lcd.print("  Fire Detector ");
  delay(2000);
  lcd.clear();
  
} //END of setup()

//**************************************************************************************
void loop()
{
  flame_value = digitalRead(flamePin); // Digital input value
  smoke_value = digitalRead(smokePin); //Digital input value

  if (flame_value == ALARM || smoke_value == ALARM )   // check if the Fire variable is High
  {

    lcd.setCursor(0, 0);
    lcd.print("  alert  ");
    lcd.setCursor(0, 1);
    lcd.print("  fire  ");

    digitalWrite(buzzer, HIGH); // Turn LED on.
    digitalWrite(R_led, LOW);  // Turn LED on.
    digitalWrite(G_led, HIGH);   // Turn LED off.
    delay(100);
  }
  
  else   // check if the Fire variable is Low
  {
    lcd.setCursor(0, 0);
    lcd.print("...CONDITIONS...");
    lcd.setCursor(0, 1);
    lcd.print(".....Normal.....");
    digitalWrite(buzzer, LOW); // Turn LED off.
    digitalWrite(R_led, HIGH);  // Turn LED off.
    digitalWrite(G_led, LOW); // Turn LED on.

  }
  delay(100);
  
} //END of loop()

For an analog input we sometimes read the input twice.

For digital inputs we read them once.


Do you understand why you had to use the DO pin on the sensors ?


BTW, simulators are garbage !


:sleeping: :sleeping_bed:

@LarryD

#21 code also run perfectly. I have to only swap the content of IF part and ELSE part.

#27 code is also working perfect I have to just do the same swapping.

Thank you so much @LarryD @TomGeorge for giving my project your precious time.:blush::blush:

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