LDR for chicken door not working as expected

Hi All,

I have set up an automatic chicken door which works via a LDR.

My problem is that the door is closing to early when it’s still light.
For the variable daythreshold which at present is 400 I have had it as low as 175
and the door still closes at the same time.

LDR used -
Cadmium Sulphide (CdS) light dependent resistor cell. -
(Similar to Philips ORP12- Dark Resistance:
min10M- Light Resistance:
48k - 140k- Max Dissipation:
30mW- Rise Time: (0~63%) 40mS- Decay Time: (100~37%) 10mS- Dimensions: 5.1mm x 4.3mm ±0.2- Lead spacing: 3.4mm)

Current resistor value is 410K

I have included the code.

// Chicken Coop Code 22/08/2017

// darkthreshold 20 This has nothing to do with controlling the darkness setting
//daythreshold = 400
// Define pins & variables

const int EnMotorPin = 9;          // enable motor  - pin 9  PWM
const int CloseDoorPin = 8;  // direction close motor - pin 8
const int OpenDoorPin = 7;   // direction open motor  - pin 7

const int photocellPin = A0;                 // photocell connected to analog 0
int photocellVal;             // analog reading of the photocel
bool daylight = true; // daylight status
int darkthreshold = 20;
int daythreshold = 400;  // sets hysteresis limits to stop door jittering

int systemState = 0;
int SPEED = 220;

// Reed switches top and bottom of coop door

int bottomSwitchPin = 2;
int topSwitchPin = 4;
bool topSwitchState; // true if door is up
bool bottomSwitchState; // true if door is down
void setup() {

  Serial.begin(9600);
  pinMode(bottomSwitchPin, INPUT);
  pinMode(topSwitchPin, INPUT);
  pinMode(EnMotorPin, OUTPUT);
  pinMode(CloseDoorPin, OUTPUT);
  pinMode(OpenDoorPin, OUTPUT);
}

void loop() {
  systemState = 0;
  photocellVal = analogRead(photocellPin); // read inputs
  if (photocellVal > daythreshold)
  {
    daylight = true;
  }
  if (photocellVal < darkthreshold)
  {
    daylight = false;
  }
  topSwitchState = digitalRead(topSwitchPin);
  bottomSwitchState = digitalRead(bottomSwitchPin);
  //  read system status
  if (daylight == true && topSwitchState == true)
  {
    systemState = 0;   // motor stop
  }
  if (daylight == true && topSwitchState == false)
  {
    systemState = 1;  // drive door up
  }
  if (daylight == false && bottomSwitchState == true)
  {
    systemState = 0;    // motor stop
  }
  if (daylight == false && bottomSwitchState == false)
  {
    systemState = 2;  // drive door down
  }
  DispVals();
  switch (systemState) {
    case 0: // motor stop
      digitalWrite(CloseDoorPin, LOW);
      digitalWrite(OpenDoorPin, LOW);
      analogWrite(EnMotorPin, 0);
      break;
    case 1:  //drive door up
      digitalWrite(CloseDoorPin, LOW);
      digitalWrite(OpenDoorPin, HIGH);
      analogWrite(EnMotorPin, SPEED);
      break;
    case 2:  // drive door down
      digitalWrite(CloseDoorPin, HIGH);
      digitalWrite(OpenDoorPin, LOW);
      analogWrite(EnMotorPin, SPEED);
      break;
    default:
      digitalWrite(CloseDoorPin, LOW);
      digitalWrite(OpenDoorPin, LOW);
      analogWrite(EnMotorPin, 0);
      break;
  }
}

void DispVals()
{
  Serial.print("topSW ");
  Serial.print(topSwitchState);
  Serial.print("\t");
  Serial.print("botSW ");
  Serial.print(bottomSwitchState);
  Serial.print("\t");
  Serial.print("PhotoVal ");
  Serial.print(photocellVal);
  Serial.print("\t");
  Serial.print("daylight ");
  Serial.print(daylight);
  Serial.print("\t");
  Serial.print("systemState ");
  Serial.print(systemState);
  Serial.print("\t");
  if (systemState == 0)
  {
    Serial.println("\t door stopped");
  }
  if (systemState == 1)
  {
    Serial.println("\t door driving up");
  }
  if (systemState == 2)
  {
    Serial.println("\t door driving down");
  }
}

Can someone please have a look at it and advise where it’s going wrong.

It’s pretty simple I want the door to open at first light and close when it’s dark.

Thanks in advance
Tony

You need to reduce darkthreshold, not daythreshold, to change the close time.

MarkT:
You need to reduce darkthreshold, not daythreshold, to change the close time.

I have done that as well right down to 2

What do your Serial print statements tell you is happening?

Sounds like you have the LDR and resistor inverted, if you want voltage to go UP with light level, connect LDR to 5v and resistor to GND.

Here is the wiring attached

Not sure it can be inverted?

Hi,
Ops circuit.

I think this is clearer.


Tom... :slight_smile:

Thanks TomGeorge :slight_smile:

Today I took a reading via the serial monitor and found the following -

No light 0 to 3
Med light 220 to 235
High light 330 to 365 (using a touch light pointed directly at LDR)

So is this a hardware issue LDR ?

Is the LDR not sensitive enough ?

At present when darkthreshold is set to 2 it still closes too early
and I would expect the high reading to be much higher.

If it is a LDR issue what LDR should I use to get better readings ?

Thanks
Tony

I would use LM339 comparator front ends and feed them to the digital inputs on the controller.

.

What is the photocellVal in the morning when the door should open?
""""""""""""""""""""""""""""""""""" evening """"""""""""""""""""""""""" close?
""""""""""""""""""""""""""""""""""" middle of the day?

Update -

I have replaced the LDR with the following -

(Cadmium sulphide (CdS) light dependent resistor cell.

  • Dark resistance: 0.5M ohm min.
  • Light resistance: 2.8K ohm min 8.4K ohm max.
  • Max dissipation: 100mW
  • Rise time: (0-63%) 50mS
  • Decay time: (100 - 37%) 40mS
  • Dimensions: 8.5mm x 10.14mm
  • Lead spacing: 7.68mm.

Prior to installing I did a simple stand alone LDR test to see what
reading I can expect to see.

Then uploaded the amended sketch.

This evening the door did close 30 minutes later but still not when it was dark.
I will bring down the darkthreshold again till I get the correct LDR reading.

Tomorrow morning I will see when the door opens and do the same exercise
if need be.

Finally it looks like things are moving in the right direction.

It seems it was down to using the wrong LDR :slight_smile:

Will report back with the progress, hopefully this is sorted once and for all.

Cheers
Tony

This evening the door did close 30 minutes later but still not when it was dark.
I will bring down the darkthreshold again till I get the correct LDR reading.

Tomorrow morning I will see when the door opens and do the same exercise
if need be.

You don't need to wait all day to re-run the same test you ran earlier, where you got readings from the sensor in darkness, in moderate lighting, and in bright lighting.

I have learnt the hard way - I did lots of testing
before the door went live and believed it all worked.

Then encountered all the above issues.

So I am happy to do real time testing on site.

Tony

Hi All,

Quick update - It works

By upgrading the LDR did the trick.

I can report all working as expected.

Thanks to all for all the help giving.

Cheers
Tony