Problem with multiple LDR sensor readings

Hi there,
I'm a beginner in this field. I have no problem setting up one LDR sensor.
However, I faced a problem when I tried to set up 4 LDR sensors and get those readings.
I found there is a reading issue, one shows high reading than the others in the dark, after I finished the circuit.
Here is my circuit and my code. Please help me find where the problem is.
Thanks

PS. plz ignore the LED control part.

String command;

#define R1Sensor A1 //Sensor on LED_1
#define R2Sensor A2 //Sensor on LED_2
#define R3Sensor A3 //Sensor on LED_3
#define R4Sensor A4 //Sensor on LED_4

#define LED_1 2 // The far right LED
#define LED_2 3 
#define LED_3 4
#define LED_4 5 // The far left LED

int sensorValue1 = 0;
int sensorValue2 = 0;
int sensorValue3 = 0;
int sensorValue4 = 0;

int TimeInterval = 1000; //unit: ms

// Function of countdown message
void CountDown(int x){
  Serial.println("Wait for " + String(x) + " seconds");
  for (int j = x; j >= 0; j--){
        Serial.println(j);
        delay(1000);
  }
}


void setup() {
  Serial.begin(9600);
  pinMode(LED_1, OUTPUT);
  pinMode(LED_2, OUTPUT);
  pinMode(LED_3, OUTPUT);
  pinMode(LED_4, OUTPUT);

  delay(TimeInterval);

// Commands of user instruction  
  Serial.println("Type Command (r1, r2, r3, r4, blank)");
}



void loop() {

// Receive user command and print it
  if (Serial.available()) {
    command = Serial.readStringUntil('\n');
    command.trim();
  Serial.print("Command:");
  Serial.println(command);
  delay(TimeInterval);

// blank command: turn off all LEDs and show all sensors readings five times
    if (command.equals("blank")){
      digitalWrite(LED_1,LOW);
      digitalWrite(LED_2,LOW);
      digitalWrite(LED_3,LOW);
      digitalWrite(LED_4,LOW);

      for (int i = 1; i <= 5; i++){
        delay(TimeInterval);
        Serial.print(i);
        Serial.println(" Recording");
        
        sensorValue1 = analogRead(R1Sensor);
        Serial.print("Sensor1:");
        Serial.println(sensorValue1);
        
        sensorValue2 = analogRead(R2Sensor);
        Serial.print("Sensor2:");
        Serial.println(sensorValue2);
      
        sensorValue3 = analogRead(R3Sensor);
        Serial.print("Sensor3:");
        Serial.println(sensorValue3);
      
        sensorValue4 = analogRead(R4Sensor);
        Serial.print("Sensor4:");
        Serial.println(sensorValue4);
      
        Serial.println("==================================");
      }
      Serial.println("Type Command (r1, r2, r3, r4, blank)");
    }


// r1 command: turn on LED_1, wait 10 seconds, and show R1Sensor readings five times
    else if (command.equals("r1")){
      digitalWrite(LED_1,HIGH);
      digitalWrite(LED_2,LOW);
      digitalWrite(LED_3,LOW);
      digitalWrite(LED_4,LOW);
      
      CountDown(10);
      for (int i = 1; i < 6; i++){
        delay(TimeInterval);
        Serial.print(i);
        Serial.println(" Recording");
        
        sensorValue1 = analogRead(R1Sensor);
        Serial.print("Sensor1:");
        Serial.println(sensorValue1);
        Serial.println("==================================");
      }
      Serial.println("Type Command (r1, r2, r3, r4, blank)");
    }

// r2 command: turn on LED_2, wait 10 seconds, and show R2Sensor readings five times
    else if (command.equals("r2")){
      digitalWrite(LED_1,LOW);
      digitalWrite(LED_2,HIGH);
      digitalWrite(LED_3,LOW);
      digitalWrite(LED_4,LOW);
      
      CountDown(10);
      for (int i = 1; i < 6; i++){
        delay(TimeInterval);
        Serial.print(i);
        Serial.println(" Recording");
        
        sensorValue2 = analogRead(R2Sensor);
        Serial.print("Sensor2:");
        Serial.println(sensorValue2);
        Serial.println("==================================");
      }
      Serial.println("Type Command (r1, r2, r3, r4, blank)");
    }

// r3 command: turn on LED_3, wait 10 seconds, and show R3Sensor readings five times
    else if (command.equals("r3")){
      digitalWrite(LED_1,LOW);
      digitalWrite(LED_2,LOW);
      digitalWrite(LED_3,HIGH);
      digitalWrite(LED_4,LOW);

      CountDown(10);
      for (int i = 1; i < 6; i++){
        delay(TimeInterval);
        Serial.print(i);
        Serial.println(" Recording");
        
        sensorValue3 = analogRead(R3Sensor);
        Serial.print("Sensor3:");
        Serial.println(sensorValue3);
        Serial.println("==================================");
      }
      Serial.println("Type Command (r1, r2, r3, r4, blank)");
    }

// r4 command: turn on LED_4, wait 10 seconds, and show R4Sensor readings five times
    else if (command.equals("r4")){
      digitalWrite(LED_1,LOW);
      digitalWrite(LED_2,LOW);
      digitalWrite(LED_3,LOW);
      digitalWrite(LED_4,HIGH);

      CountDown(10);
      for (int i = 1; i < 6; i++){
        delay(TimeInterval);
        Serial.print(i);
        Serial.println(" Recording");
        
        sensorValue4 = analogRead(R4Sensor);
        Serial.print("Sensor4:");
        Serial.println(sensorValue4);
        Serial.println("==================================");
      }
      Serial.println("Type Command (r1, r2, r3, r4, blank)");
    }

// off command: turn off all LEDs and continuously show all sensor readings 
    else if (command.equals("off")){
        while(true){
          sensorValue1 = analogRead(R1Sensor);
          Serial.print("Sensor1:");
          Serial.println(sensorValue1);
          
          sensorValue2 = analogRead(R2Sensor);
          Serial.print("Sensor2:");
          Serial.println(sensorValue2);
        
          sensorValue3 = analogRead(R3Sensor);
          Serial.print("Sensor3:");
          Serial.println(sensorValue3);
        
          sensorValue4 = analogRead(R4Sensor);
          Serial.print("Sensor4:");
          Serial.println(sensorValue4);
        
          Serial.println("==================================");
          delay(TimeInterval);
        }     
    }

// on command: turn on all LEDs and continuously show all sensor readings 
    else if (command.equals("on")){
        digitalWrite(LED_1,HIGH);
        digitalWrite(LED_2,HIGH);
        digitalWrite(LED_3,HIGH);
        digitalWrite(LED_4,HIGH);        
        while(true){
          sensorValue1 = analogRead(R1Sensor);
          Serial.print("Sensor1:");
          Serial.println(sensorValue1);
          
          sensorValue2 = analogRead(R2Sensor);
          Serial.print("Sensor2:");
          Serial.println(sensorValue2);
        
          sensorValue3 = analogRead(R3Sensor);
          Serial.print("Sensor3:");
          Serial.println(sensorValue3);
        
          sensorValue4 = analogRead(R4Sensor);
          Serial.print("Sensor4:");
          Serial.println(sensorValue4);
        
          Serial.println("==================================");
          delay(TimeInterval);
        }     
    }

// show "Bad command" message if receiving an command is not in the command list
    else {
      Serial.println("Bad command");
      Serial.println("Type Command (r1, r2, r3, r4, blank)");
    }

  }

}

Hello
Post a picture from the real world showing the wiring in detail.

You repeat code like this in different parts of the sketch.
Write it once, in a simple function

Hi,
Here are the details. It is messy. I tried to label as clear as possible.

Sounds like a good idea.
I'll try to simplify the sketch step by step.
Thanks

How much higher? Please show some example output from serial monitor. Please do not post screenshots of the serial monitor, copy text from serial monitor and paste that into your post between code tags.

LDR are not high-precision sensors and there will always be some difference in the readings from similar LDRs in the same light.

Did you try swapping the fixed value resistors around, and swapping the analog pins that the LDRs are connected to? This might indicate if it is truly the LDR that is giving a higher reading.

Hi,
Here is the text from the serial monitor.
Normally I found the Sensor4 reading is higher, around 900.
This time, Sensor 1 also shows higher reading.
All the resistors I used are 100k ohm.
I tried to leave only Sensor1 connected.
However, I still got some numbers on the serial monitor from Sensor2, Sensor3, and Sensor4.

Type Command (r1, r2, r3, r4, blank)
Command:blank
1 Recording
Sensor1:906
Sensor2:17
Sensor3:0
Sensor4:745
==================================
2 Recording
Sensor1:1012
Sensor2:24
Sensor3:0
Sensor4:753
==================================
3 Recording
Sensor1:1009
Sensor2:20
Sensor3:0
Sensor4:757
==================================
4 Recording
Sensor1:1018
Sensor2:19
Sensor3:0
Sensor4:760
==================================
5 Recording
Sensor1:1023
Sensor2:24
Sensor3:0
Sensor4:759
==================================
Type Command (r1, r2, r3, r4, blank)

When "only sensor1 connected", are the 100K still connecting all the analog pins to ground?

If you disconnect the sensors and the 100K from the analog pins, then the pins will be "floating" and you could get any value from them, it will be random.

"only sensor1 connected" means that I unplug the wires from the Arduino board. Only sensor1 wire connects to A1 pin.

I know what you mean and test it by myself. If there is now input of the analog pins, I still will get some random values on the serial monitor. I'll ignore these values. Thanks for your reply.

However, I still cannot figure out why some of the sensor readings show high values.
If the circuit has no problem, maybe replacing LDR components can solve this issue?

image

Buy a prototyping board similar to this:

Are those resistors Bn Bk Bk Gold ?

And

Bn Bk Bk Or ?


Use a DMM to check that wires are making good connections.

You have not yet told us what happened when you followed my suggestions from reply #8.

I think it's Bn Bk Bk Og Bn?

okay, will use DMM to check the connections

Use your DMM to check the resistance.

BN BK BK OR is = 100k

BN BK BK GOLD = 10 Ω to low for LEDs


If the LEDs are illuminating the LDR it’s best that the LEDs be white in colour.

A weird thing happened.
The readings became normal as I expected.
Here are the sensor values before swapping analog pins.
Sensor1 > A1
Sensor2 > A2
Sensor3 > A3
Sensor4 > A4

Command:blank
1 Recording
Sensor1:23
Sensor2:106
Sensor3:23
Sensor4:143
==================================

Here are sensor values after swapping analog pins.
Sensor1 > A2
Sensor2 > A3
Sensor3 > A4
Sensor4 > A1

Command:blank
1 Recording
Sensor1:152
Sensor2:41
Sensor3:128
Sensor4:35
==================================

Interesting! Now reconnect the pins back to the original connections and swap the resistors.

Before swapping resistors (all 100k)
Sensor1 > resistor1
Sensor2 > resistor2
Sensor3 > resistor3
Sensor4 > resistor4

Command:blank
1 Recording
Sensor1:65
Sensor2:165
Sensor3:57
Sensor4:196
==================================

After swapping resistors
Sensor1 > resistor2
Sensor2 > resistor3
Sensor3 > resistor4
Sensor4 > resistor1

Command:blank
1 Recording
Sensor1:935
Sensor2:69
Sensor3:13
Sensor4:800
==================================

High values coming back. :frowning:

High values coming back.

Check all wiring for continuity.