# How can I light one led from two different sensors?

I want to light one led with two sensors, the first one is a PIR and the other one is a Photocell sensor.

The idea is to turn on the led if the PIR sensor detects movement, and also to turn the led on if the room gets darker(detected by the photocell).

I managed to make a code, but with TWO led's, a different one for every sensor.

``````int ledPin = 13;
int ledPin1=12;
const int ldrPin = A0;
int inputPin = 2;
int pirState = LOW;
int val = 0;

void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(inputPin, INPUT);
pinMode(ldrPin, INPUT);
}

void loop() {

if(ldrStatus<=400)
{
digitalWrite(ledPin, HIGH);
delay(500);
Serial.println("LED ON");
}
else digitalWrite(ledPin, LOW);
if (val == HIGH) {
digitalWrite(ledPin1, HIGH);
if (pirState == LOW) {
Serial.println("Motion detected!");
pirState = HIGH;
}
} else {
delay(5000);
digitalWrite(ledPin1, LOW);
if (pirState == HIGH){
Serial.println("Motion ended!");
pirState = LOW;
}
}
}
``````
• Where does `pirState` come from? It hasn't been defined or initialized. And what signal is on `inputPin`? The general logic for doing something depending on either of two conditions is `if( condition1 || condition2 ){ // do something here }` Commented Jan 5, 2018 at 22:29
• To make it easier you could add two boolean variables 'dark' and 'motion'. Make it true if it is dark and false for light and the other one true for motion and false for no motion from the PIR sensor. Then you can do the if-statement as @JRobert wrote about.
– Jot
Commented Jan 5, 2018 at 22:32
• pirState is from the PIR Sensor, i was thinking that it should always be on low if there is no movement, I don't know if its the right way to do that but for now works. And the inputPin is also from the pir sensor. Commented Jan 6, 2018 at 10:11
• your problem is that you do not have the program clear in your mind ... you do not want `I want to light one led with two sensors` .... you want `monitor two sensors and light one LED` .... you said that you have done it with two LEDs, so just modify your code so that both sensors have to satisfy a goal before the LED is activated Commented Jan 7, 2018 at 22:46

``````int ledPin = 13;

int ldrPin = A0;

int PIR_Pin = 2;

void setup()

{

Serial.begin(115200);

pinMode(ledPin, OUTPUT);

pinMode(PIR_Pin, INPUT);

Serial.println("Started Monitoring!");
}

void loop()
{

boolean lightStatus = ( analogRead(ldrPin) <= 400 ) ;
digitalWrite(ledPin,  lightStatus);

if(lightStatus) Serial.println("Light Triggered!");

{
Serial.println("Motion Triggered!");
digitalWrite(ledPin, HIGH);
delay(5000);
digitalWrite(ledPin, LOW);
}
}
``````

To keep it simple serial code is removed, if needed can be added easily that should not be a big issue. Still this can be done in much more efficient way. This is just one solution.

Note: haven't complied/error checked the code as i am writing this on a phone. Hope it should work as intended, errors if any, shall be minor ones. Comment below if any issues.

Update 1: added code to send triggered status over serial comm.

Update 2: corrected "Boolean" to be as "boolean"

• Yes, it works. Now I just need to print some messages so I can figure out which sensor turned the led on, because I will have 8 sensors in total, 4 of each. Thank you for the help! Commented Jan 11, 2018 at 10:13
• @gollum updated answer with code for serial comm. Do compile n test. Commented Jan 11, 2018 at 10:30
• I got an error: "Boolean was not declared in this scope". @25mhz Commented Jan 11, 2018 at 15:34
• Try "bool" it should work Commented Jan 11, 2018 at 15:35
• That should be "boolean" my bad, dint check that. Commented Jan 11, 2018 at 15:38