im currently using 4 LDR sensor modules for my project which is using digital output.The LDR will then send digital output to arduino and send digitalwrite.The problem is when digitalread2 is HIGH all the output becomes high.
const int ldr_pin1 = A0;
const int ldr_pin2 = A1;
const int ldr_pin3 = A2;
const int ldr_pin4 = A3;
const int led_pin1 = 10;
const int led_pin2 = 11;
const int led_pin3 = 12;
const int led_pin4 = 13;
void setup() {
// put your setup code here, to run once:
pinMode(ldr_pin1,INPUT);
pinMode(led_pin1,OUTPUT);
pinMode(ldr_pin2,INPUT);
pinMode(led_pin2,OUTPUT);
pinMode(ldr_pin3,INPUT);
pinMode(led_pin3,OUTPUT);
pinMode(ldr_pin4,INPUT);
pinMode(led_pin4,OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if( digitalRead( ldr_pin1 ) == 1){
digitalWrite( led_pin1,HIGH);
}
else{
digitalWrite( led_pin1 , LOW);
}
if( digitalRead( ldr_pin2 ) == 1){
digitalWrite( led_pin2,HIGH);
}
else{
digitalWrite( led_pin2 , LOW);
}
if( digitalRead( ldr_pin3 ) == 1){
digitalWrite( led_pin3,HIGH);
}
else{
digitalWrite( led_pin3 , LOW);
}
if( digitalRead( ldr_pin4 ) == 1){
digitalWrite( led_pin4,HIGH);
}
else{
digitalWrite( led_pin4 , LOW);
}
}
I found LDR sensors on WokWi that are probably the same as the one TO is using.
If you start the simulation and click on a LDR, you can change the LUX number. At a certain value the LEDs go on and off. In this example they turn on at a low LUX value (< 100lux), high values turn them off.