Chicken Coop Photoresistor Issue

Hello Everyone.

I build a chicken coop door based on Dave Nave's blog. The door was working perfectly inside for a few weeks. Last weekend I finally moved the unit outside and now it's not working. For some reason, the photoresistor is not working.

I've tested the resistor and its resistance is low with light and high when I cover it. I can't figure this out...and I'm sure it's something really silly. This is the part that's not making sense- if I take the wires off the photoresistor and connect them, the door opens. If I leave the wires disconnected, the door closes.

With that being said, I'm thinking something in the code is wrong and I have to adjust the photosensor for outside light intensity?

I'm new to this and trying to figure it out.... I've posted the code below (from Dave's website).

// libraries

#include <OneWire.h> // load the onewire library for thermometer
#include <LiquidCrystal.h> // load the liquid crystal library

// print debug messages or not to serial
const boolean SerialDisplay = true;

// pins assignments

// temperature chip i/o
const int photocellPin = A0; // photocell connected to analog 0
const int enableCoopDoorMotorB = 7; // enable motor b - pin 7
const int directionCloseCoopDoorMotorB = 8; // direction close motor b - pin 8
const int directionOpenCoopDoorMotorB = 9; // direction open motor b - pin 9
const int bottomSwitchPin = 26; // bottom switch is connected to pin 26
const int topSwitchPin = 27; // top switch is connected to pin 27

// variables

// photocell
int photocellReading; // analog reading of the photocel
int photocellReadingLevel; // photocel reading levels (dark, twilight, light)

// reed switches top and bottom of coop door

// top switch

int topSwitchPinVal; // top switch var for reading the pin status
int topSwitchPinVal2; // top switch var for reading the pin delay/debounce status
int topSwitchState; // top switch var for to hold the switch state

// bottom switch

int bottomSwitchPinVal; // bottom switch var for reading the pin status
int bottomSwitchPinVal2; // bottom switch var for reading the pin delay/debounce status
int bottomSwitchState; // bottom switch var for to hold the switch state

// photocell reading delay
unsigned long lastPhotocellReadingTime = 0;
unsigned long photocellReadingDelay = 6000; // 1 minutes

// debounce delay
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 100;

// ************************************** the setup **************************************

void setup(void) {

Serial.begin(9600); // initialize serial port hardware

// welcome message
if (SerialDisplay) {
Serial.println(" Processes running:");
Serial.println(" Timer doReadPhotoCell every 1 minutes - light levels: open or close door");
}

// coop door

// coop door motor
pinMode (enableCoopDoorMotorB, OUTPUT); // enable motor pin = output
pinMode (directionCloseCoopDoorMotorB, OUTPUT); // motor close direction pin = output
pinMode (directionOpenCoopDoorMotorB, OUTPUT); // motor open direction pin = output

// coop door leds
pinMode (coopDoorOpenLed, OUTPUT); // enable coopDoorOpenLed = output
pinMode (coopDoorClosedLed, OUTPUT); // enable coopDoorClosedLed = output
digitalWrite(coopDoorClosedLed, LOW);

// coop door switches
// bottom switch
pinMode(bottomSwitchPin, INPUT); // set bottom switch pin as input
digitalWrite(bottomSwitchPin, HIGH); // activate bottom switch resistor

// top switch
pinMode(topSwitchPin, INPUT); // set top switch pin as input
digitalWrite(topSwitchPin, HIGH); // activate top switch resistor

}

// ************************************** functions **************************************

// operate the coop door

// photocel to read levels of exterior light

void doReadPhotoCell() { // function to be called repeatedly - per coopPhotoCellTimer set in setup

photocellReading = analogRead(photocellPin);

if ((unsigned long)(millis() - lastPhotocellReadingTime) >= photocellReadingDelay) {
lastPhotocellReadingTime = millis();

// set photocel threshholds
if (photocellReading >= 0 && photocellReading <= 3) {
photocellReadingLevel = '1';

if (SerialDisplay) {
Serial.println(" Photocel Reading Level:");
Serial.println(" - Dark");
}
}
else if (photocellReading >= 4 && photocellReading <= 120) {
photocellReadingLevel = '2';
if (SerialDisplay) {
Serial.println(" Photocel Reading Level:");
Serial.println(" - Twilight");
}
}
else if (photocellReading >= 125 ) {
photocellReadingLevel = '3';
if (SerialDisplay) {
Serial.println(" Photocel Reading Level:");
Serial.println(" - Light");
}
}
if (SerialDisplay) {
Serial.println(" Photocel Analog Reading = ");
Serial.println(photocellReading);
}
}
}

//debounce bottom reed switch

void debounceBottomReedSwitch() {

//debounce bottom reed switch
bottomSwitchPinVal = digitalRead(bottomSwitchPin); // read input value and store it in val

if ((unsigned long)(millis() - lastDebounceTime) > debounceDelay) { // delay 10ms for consistent readings

bottomSwitchPinVal2 = digitalRead(bottomSwitchPin); // read input value again to check or bounce

if (bottomSwitchPinVal == bottomSwitchPinVal2) { // make sure we have 2 consistant readings
if (bottomSwitchPinVal != bottomSwitchState) { // the switch state has changed!
bottomSwitchState = bottomSwitchPinVal;
}
if (SerialDisplay) {
Serial.print (" Bottom Switch Value: "); // display "Bottom Switch Value:"
Serial.println(digitalRead(bottomSwitchPin)); // display current value of bottom switch;
}
}
}
}

Please edit your post to add code tags ("</>" button). See the "How to use this forum" post.

Yes, you have to adjust the code to take into account light intensity.

Such sensors are not designed for outdoor use, moisture has got in... (or its been destroyed by sunlight?)
Silicon photodiode in a hermetically sealed enclosure is the way to go.

Thank for the feedback. I found a basic code last night and tried it out. What I found was the photoresistor was measuring (serial monitor) around 800-850 for light and 2 for complete darkness. My code had greater than 3 for dark. Going to try this out for a quick fix.

MarkT- Can you tell me if this sensor is similar to the one you're recommending?

https://www.amazon.com/Shine-Gold-Electronices-Photodiode-Detector/dp/B01JTYTIJG/ref=sr_1_2?ie=UTF8&qid=1535631943&sr=8-2&keywords=silicon+photodiode

People have been using solar cells from cheap garden lights for that.

Just connect the cell (+) to an analogue pin via a (~4k7) resistor, and (-) to ground.
No pull up.
See what values you get.
Leo..

Thanks, Leo. If I'm reading this correctly you're saying to use a solar cell, from a garden light, just get a cheapo from HD and take the cell out?

The only part that's throwing me off is the pull-up resistor. Please see the fritzing diagram, attached, i'm currently using one (1) 10K resistor on the ground. Replace the 10K with a 4.7 and see the difference?

Yep, the neighbours might also have one in their garden.

You don't need pull up with a solar cell, because it's producing it's own voltage.
But it's wise to have a 4k7 or 10k resistor in series with the positive wire of the cell,
because a solar cell is able to power the Arduino through the pin when the Arduino is off, and that could be unhealthy for the pin.

I see you also have resistors in series with the two switches. This is not correct.
Can just use a switch between pin and ground, with pull up enabled in pinMode().
Leo..

Yep, the neighbours might also have one in their garden.

Had one in their garden :o

Most red LED's will work like a tiny solar cell and generate voltage form the light (they are not very efficient). Add a glob of hot glue on the LED to collect light from a wide angle.

Leo,

Please review my marked up fritz, is this correct?

09282018130225.pdf (391 KB)

Yes, but you should ofcourse test individual parts of a setup separate first before adding them to your project.
So you know what values do you get at dusk/dawn light levels.
Leo..

Leo,

Finally had time to hook things up today. Readings at noon were 90 and when dark it's close to 475. I input the data but it's still not working? Post the sketch below for help?

// libraries


#include <SimpleTimer.h>                     // load the SimpleTimer library to make timers, instead of delays & too many millis statements
#include <OneWire.h>                         // load the onewire library for thermometer
// #include <Servo.h>                           // load the servo library
#include <LiquidCrystal.h>                   // load the liquid crystal library



// print debug messages or not to serial 
const boolean SerialDisplay = false;

/*
* Copyright 2016, David Naves (http://daveworks.net, http://davenaves.com)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, see 
 
*/
 
 /*
* I'm hoping that if you use/modify this code, you will share your
* coop project with me and the world (pictures, whatever)
* I'm big on sharing.
* Cheers,
* //D
*/
 


// pins assignments

// temperature chip i/o
const int photocellPin = A0;                 // photocell connected to analog 0
const int thermometer = 3;                   // thermometer DS18S20 on digital pin 3

const int relayHeat = 5;                     // heat lamp relay set to digital pin 5
const int relayFan = 6;                      // exhaust fan relay set to digital pin 6
const int enableCoopDoorMotorB = 7;          // enable motor b - pin 7
const int directionCloseCoopDoorMotorB = 8;  // direction close motor b - pin 8
const int directionOpenCoopDoorMotorB = 9;   // direction open motor b - pin 9
const int bottomSwitchPin = 26;              // bottom switch is connected to pin 26
const int topSwitchPin = 27;                 // top switch is connected to pin 27
const int coopDoorOpenLed = 40;              // led set to digital pin 40
const int coopDoorClosedLed = 41;            // led set to digital pin 41
// const int relayElectricFence = 43;        // heat lamp relay set to digital pin 43
const int relayInteriorLight = 45;           // interior lights relay set to digital pin 45


// variables


// photocell
int photocellReading;                            // analog reading of the photocel
int photocellReadingLevel;                       // photocel reading levels (dark, twilight, light)

// reed switches top and bottom of coop door

// top switch

int topSwitchPinVal;                   // top switch var for reading the pin status
int topSwitchPinVal2;                  // top switch var for reading the pin delay/debounce status
int topSwitchState;                    // top switch var for to hold the switch state

// bottom switch

int bottomSwitchPinVal;                // bottom switch var for reading the pin status
int bottomSwitchPinVal2;               // bottom switch var for reading the pin delay/debounce status
int bottomSwitchState;                 // bottom switch var for to hold the switch state

// SimpleTimer objects
SimpleTimer coopPhotoCellTimer;


// debounce delay
long lastDebounceTime = 0;
long debounceDelay = 100;


// temperature check delay
long lastTempCheckTime = 0;
long TempCheckDelay = 600000;           // 10 minutes


// interior lights twighlight delay
long lastTwilightTime = 0;
long TwilightDelay = 300000;           // 5 minutes


// temperature chip i/o
OneWire ds(thermometer);               // thermometer on digital pin 3

// chicken cam servo 
// Servo chickenCamServo;              //  servo object to control chickenCam
// int chickenCamServoPos = 0;         // chickenCamServoPosition var

// lcd
LiquidCrystal lcd(38, 37, 36, 32, 33, 34, 35);   // lcd pin assignments
int backLight = 13;                              // pin 13 controls backlight
/*
LCD Pin	 > arduino Pin
 
 lcd pin 1 VSS >	    gnd arduino pin
 lcd pin 2 VDD >	    +5v arduino pin
 lcd pin 3 VO(contrast) >   330 ohm resistor to gnd arduino pin
 lcd pin 4 RS	            arduino pin 38
 lcd pin 5 R/W	            arduino pin 37
 lcd pin 6 Enable	    arduino pin 36
 lcd pin 7  -	 
 lcd pin 8  -	 
 lcd pin 9  -	 
 lcd pin 10 -	 
 lcd pin 11 (Data 4)    >   arduino pin 32
 lcd pin 12 (Data 5)    >   arduino pin 33
 lcd pin 13 (Data 6)    >   arduino pin 34
 lcd pin 14 (Data 7)    >   arduino pin 35
 lcd pin 15 Backlight + >   arduino pin 13 (built-in resistor)
 lcd pin 16 Backlight   >   arduino gnd pin
 */




// ************************************** the setup **************************************

void setup(void) {

  Serial.begin(9600); // initialize serial port hardware


  // welcome message
  if(SerialDisplay){
    Serial.println(" Processes running:");
    Serial.println(" Timer readPhotoCell every 10 minutes - light levels: open or close door");
  }
  // coop hvac

  pinMode(relayHeat, OUTPUT);   //set heat lamp relay output
  pinMode(relayFan, OUTPUT);    //set exhaust fan relay output

  // coop door  

  // coop door motor
  pinMode (enableCoopDoorMotorB, OUTPUT);           // enable motor pin = output
  pinMode (directionCloseCoopDoorMotorB, OUTPUT);   // motor close direction pin = output
  pinMode (directionOpenCoopDoorMotorB, OUTPUT);    // motor open direction pin = output

  // coop door leds
  pinMode (coopDoorOpenLed, OUTPUT);                // enable coopDoorOpenLed = output
  pinMode (coopDoorClosedLed, OUTPUT);              // enable coopDoorClosedLed = output

  // coop door switches
  // bottom switch
  pinMode(bottomSwitchPin, INPUT);                  // set bottom switch pin as input
  digitalWrite(bottomSwitchPin, HIGH);              // activate bottom switch resistor

  // top switch
  pinMode(topSwitchPin, INPUT);                     // set top switch pin as input
  digitalWrite(topSwitchPin, HIGH);                 // activate top switch resistor  

  // interior lights relay
  pinMode(relayInteriorLight, OUTPUT);
  digitalWrite(relayInteriorLight, HIGH);

  // electric fence relay
  // pinMode(relayElectricFence, OUTPUT);                   // set electric fence relay as output

  // timed actions setup
  coopPhotoCellTimer.setInterval(600000, readPhotoCell);   // read the photocell every 10 minutes



  // servo for interior chicken web cam  
  //  chickenCamServo.attach(11);                          // chickenCamServo on pin 11



}

// ************************************** functions **************************************

// coop hvac

void doCoopHVACHeat() {

  float temperature = getTemp();                    // create temperature variable
  float tempF = (temperature * 9.0)/ 5.0 + 32.0;    // convert celcius to fahrenheit
  if(SerialDisplay){
    Serial.print(" Coop Temperature:");             // print out coop temperature 
    Serial.println(tempF);                          // print out the temperature 
  }

  if ((millis() - lastTempCheckTime) > TempCheckDelay) {    // check temperature every 10 minutes

    // if cold, turn on heat lamps
    if (tempF <= 40) {                                      // if temp drops below 40F turn on heat lamp(s) relay
      digitalWrite(relayHeat, HIGH); 
    }
    else if (tempF > 40) {
      digitalWrite(relayHeat, LOW);                        // if temp remains above 40F turn off heat lamp(s) relay
    }
  }
}



// if hot, turn on cooling fans

void doCoopHVACCool() {

  float temperature = getTemp();                    // create temperature variable
  float tempF = (temperature * 9.0)/ 5.0 + 32.0;    // convert celcius to fahrenheit
  if(SerialDisplay){
    Serial.print(" Coop Temperature:");             // print out coop temperature 
    Serial.println(tempF);                          // print out the temperature 
  }

  if ((millis() - lastTempCheckTime) > TempCheckDelay) {    // check temperature every 10 minutes
    if (tempF >= 83) {                                      // if temp rises above 85F turn on cooling fan(s) relay
      digitalWrite(relayFan, HIGH);
    }

    else if (tempF < 83) {
      digitalWrite(relayFan, LOW);
    }
  }
}

The code is too long, i've provided the photosensor code below.

// operate the coop door

// photocel to read levels of exterior light

void readPhotoCell() { // function to be called repeatedly - per coopPhotoCellTimer set in setup

  photocellReading = analogRead(photocellPin);
  if(SerialDisplay){
    Serial.print(" Photocel Analog Reading = ");
    Serial.println(photocellReading);
  }


  //  set photocel threshholds
  if (photocellReading >= 800 && photocellReading <= 378) {
    photocellReadingLevel = '1';

    if(SerialDisplay){
      Serial.print(" Photocel Reading Level:");
      Serial.println(" - Dark");
    }
  }  
  else if (photocellReading  >= 375 && photocellReading <= 301){
    photocellReadingLevel = '2';
    if(SerialDisplay){
      Serial.print(" Photocel Reading Level:");
      Serial.println(" - Twilight");
    }
  }  
  else if (photocellReading  >= 300 ) {
    photocellReadingLevel = '3';
    if(SerialDisplay){
      Serial.print(" Photocel Reading Level:");
      Serial.println(" - Light");
    }
  }
}

//debounce bottom reed switch

void debounceBottomReedSwitch() { 

  //debounce bottom reed switch
  bottomSwitchPinVal = digitalRead(bottomSwitchPin);       // read input value and store it in val
  // delay(10);

  if ((millis() - lastDebounceTime) > debounceDelay) {    // delay 10ms for consistent readings

    bottomSwitchPinVal2 = digitalRead(bottomSwitchPin);    // read input value again to check or bounce

    if (bottomSwitchPinVal == bottomSwitchPinVal2) {       // make sure we have 2 consistant readings
      if (bottomSwitchPinVal != bottomSwitchState) {       // the switch state has changed!
        bottomSwitchState = bottomSwitchPinVal;
      }
      if(SerialDisplay){
        Serial.print (" Bottom Switch Value: ");           // display "Bottom Switch Value:" 
        Serial.println(digitalRead(bottomSwitchPin));      // display current value of bottom switch;
      }
    }
  }
}



// debounce top reed switch
void debounceTopReedSwitch() {

  topSwitchPinVal = digitalRead(topSwitchPin);             // read input value and store it in val
  //  delay(10);

  if ((millis() - lastDebounceTime) > debounceDelay) {     // delay 10ms for consistent readings

    topSwitchPinVal2 = digitalRead(topSwitchPin);          // read input value again to check or bounce

    if (topSwitchPinVal == topSwitchPinVal2) {             // make sure we have 2 consistant readings
      if (topSwitchPinVal != topSwitchState) {             // the button state has changed!
        topSwitchState = topSwitchPinVal;
      }
      if(SerialDisplay){
        Serial.print (" Top Switch Value: ");              // display "Bottom Switch Value:" 
        Serial.println(digitalRead(topSwitchPin));         // display current value of bottom switch;
      }
    }
  }
}

How can a photocellReading be >=800 and <= 378 at the same time.

&& means "AND", do you mean "OR"

if (photocellReading >= 800 || photocellReading <= 378) {

Leo..

Leo,

Still learning, it's all new to me so the mistake above is due being green. The sketch is uploaded and I'll check back in this evening.

Greg

Do you have the solar cell connected backward? You should see a higher reading with more light.

Same result. Before I pull the board apart and check for loose connections, how can I check to see what's working and what's not? I guess what I'm asking is can I run code for the motor, sensors, etc. and get the result on the serial monitor? Is this a good way to diagnose?

What are you using for a light detector now? How is it connected?

Outsider,

Using a cell from one of my garden lights. I don't believe it's hooked up wrong when I pulled the sensor off I

made sure to note the polarity however it could be reversed on the controller.