Why couldnt sensor2 generate any output please help

int led1=2;
void setup() {
Serial.begin(9600);
pinMode(led1,OUTPUT);// put your setup code here, to run once:
}

void loop() {
int sensorValue2=analogRead(A2);
if (sensorValue2>=600&& sensorValue2<=800){
Serial.println(sensorValue2);
digitalWrite(led1,HIGH);// put your main code here, to run repeatedly:
}
else{
Serial.println(sensorValue2);
digitalWrite(led1,LOW);
}
int sensorValue1=analogRead(A1);
if (sensorValue1>=600&& sensorValue1<=800){
Serial.println(sensorValue1);
digitalWrite(led1,HIGH);
}
else{
Serial.println(sensorValue1);
digitalWrite(led1,LOW);
}
delay(1000);
}

What type of sensor ? How is it wired ? Does sensor1 work ? What happens if you exchange the two sensors ? What do you see when you print the two values ?

Sensor1 generate output but Sensor2 doesnt if I change the names or input from Sensor1 to Sensor2 then Sensor2 generate output but not Sensor1. Is there a problem with the code?

Connect another Led (led2) at DPin-3 to monitor the status of sensor1’s signal. And then, upload the following sketch:

int led1 = 2;
int led2 = 3;

void setup() 
{
  Serial.begin(9600);
  pinMode(led1, OUTPUT); // put your setup code here, to run once:
  pinMode(led2, OUTPUT);
}

void loop() 
{
  int sensorValue2 = analogRead(A2);
  if (sensorValue2 >= 600 && sensorValue2 <= 800) 
  {
    Serial.println(sensorValue2);
    digitalWrite(led1, HIGH); // put your main code here, to run repeatedly:
  }
  else 
  {
    Serial.println(sensorValue2);
    digitalWrite(led1, LOW);
  }
  
  int sensorValue1 = analogRead(A1);
  if (sensorValue1 >= 600 && sensorValue1 <= 800) 
  {
    Serial.println(sensorValue1);
    digitalWrite(led2, HIGH);
  }
  else 
  {
    Serial.println(sensorValue1);
    digitalWrite(led2, LOW);
  }
  delay(1000);
}

What if i want to be the output be summation of both sensors reading

saquib_12: What if i want to be the output be summation of both sensors reading

Don't you think that it would be better to get both of them (whatever they are) before doing anything else ?

Please answer the questions in reply #1

I have tried connecting everything but problem still there led1 light up for sensor1 but not for the other one I have checked both the sensors they work perfectly. I think theirs a problem with code as it could not generate the summation of output

I think sensor2 actually generate output. But because sensor1 is read immediatly afterwards, it will overwrite the output of sensor2.

Use two leds, or you could put in a delay between readings.

To show sensor2 for 500ms and sensor1 for 1000ms, try this:

 delay(500);
  int sensorValue1 = analogRead(A1);

Edit: If you want the LED to show if either of the sensors are in the actual range...

  • Read both sensors in beginning of the loop
  • Test if sensor1 OR sensor2 is in actual range and write HIGH or LOW to led1 accordingly
  • Wait and repeat