Using multiple digital input

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);
   }
   

}

CODE

Check pins against HIGH and LOW, not 1 and 0;

Also, you can try this:

    digitalWrite( led_pin1, digitalRead( ldr_pin1 ) );

Try this to see if you are getting good values, add printing to everywhere and see.

   if( digitalRead( ldr_pin1 ) == 1){
      Serial.println("                                  ldr_pin1 is 1"); 
      digitalWrite( led_pin1,HIGH);
   }
   else {
      Serial.println("                                              ldr_pin1 is 0");
      digitalWrite( led_pin1 , LOW);
   }

a7

Ummm… what were you doing to get that output?

It is time to draw a schematic and post it.

Hand drawn is easiest. Show all your parts, how they are connected and from where x they get power to operate.

Draw, snap a picture and post it.

TIA

a7

Post the datasheet.

Normally LDRs are not "simply" HIGH or LOW, but they change their resistance depending on the incidence of light.

Accordingly, they should either be read out with the ADC Conwerter or at least be connected to a Schmitt trigger to generate HIGH and LOW signals.

Otherwise, to switch LEDs on or off, depending on which LDR pin has a HIGH signal, the following code should work.


struct LdrState {
  bool isHigh;
  const byte ldrPin;
  const byte ledPin;
};

struct LdrState ldrs[] { {false,6,10}, {false,7,11}, {false,8,12}, {false,9,13} };

void setup() {
  // put your setup code here, to run once:

  for (auto &ldr : ldrs) {
    pinMode(ldr.ledPin,OUTPUT);
    pinMode(ldr.ldrPin,INPUT);
  }
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  
  for (auto &ldr : ldrs) {
    if ( digitalRead( ldr.ldrPin ) && !ldr.isHigh) {
      digitalWrite( ldr.ledPin, HIGH);
      ldr.isHigh = true;
    }
    else if (ldr.isHigh) {
      digitalWrite( ldr.ledPin, LOW);
      ldr.isHigh = false;
    }
  }
}
1 Like

nice interlocking, I like it :+1:

@paulpaulson Thanx :-).

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.

These modules typically have a digital and analog output pin. The digital pin is connected to a trim pot and a comparator to produce HIGH or LOW.

1 Like

Thank you for the explanation. In the meantime, as written in #9, I had found such modules and experimented with them in a simulation.

I guess this one?

Here are a few explanations to be found... wokwi-photoresistor-sensor Reference | Wokwi Docs

i actually wanted to do this project but with 4 ldr sensor module like in this video

The Video

The code from the video

const int ldr_pin = 7;
const int led_pin = 13;
void setup() {
  // put your setup code here, to run once:
    pinMode(ldr_pin,INPUT);
    pinMode(led_pin,OUTPUT);
    Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
   if( digitalRead( ldr_pin ) == 1){
      digitalWrite( led_pin,HIGH);
   }
   else{
      digitalWrite( led_pin , LOW);
   }
   
   Serial.println( digitalRead( ldr_pin ));
   delay(100);
}

Good, then you got a working solution in posting #7. And if you look at the simulation linked in posting #9, you can see how it works.

1 Like

Hello yabedibedoo

Try this simple classless object oriented multi-IO sketch to gain the knowledge how to.

constexpr byte ldrPins[]{A0,A1,A2,A3}; 
constexpr byte ledPins[]{8,9,10,11}; 
enum ledLDR {One,Two,Three,Four}; 
struct MULTIIO
{
  byte ldr;
  byte led;
};
MULTIIO multiIos[] {
  {ldrPins[One],ledPins[One]},
  {ldrPins[Two],ledPins[Two]},
  {ldrPins[Three],ledPins[Three]},
  {ldrPins[Four],ledPins[Four]},
};
void setup()
{
  for (auto multiIo:multiIos) pinMode(multiIo.ldr,INPUT_PULLUP);
  for (auto multiIo:multiIos) pinMode(multiIo.led,OUTPUT);
}
void loop() 
{
   for (auto multiIo:multiIos) digitalWrite(multiIo.led,!digitalRead(multiIo.ldr));
}



1 Like

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