Reference Pages:
Owners of chickens often wail about having to get up to let their loved ones out to forage - and about having to abandon dinner to round their loved ones up and into the safety of their coop at the end of the day.
After finding a couple of drawer runners abandoned by the verge and partnering them up with a small electric motor in my junk-box, I decided to noodle out an electronic circuit that would open and shut a coop door at dawn and dusk.
Testing the limit-switches that stop the coop door motor when the door is fully open or fully closed
In the above video, the door lifts as light falls on the light-dependent resistor, Simulatiing dawn. The door lifts until a magnet mounted on it closes a reed switch near the top of the vertical rails. When my hand covers the LDR again - simulating nightfall - the circuit waits five seconds before lowering the door until the magnet closes a reed switch mounted near the bottom of the vertical rails. The 'closing' delay is to sound an alert to warn the hens that the door is about to close, though, outside of this test, it would usually be set for ten minutes or so.
The exercise turned out to be a revelation in electronic design principles.
Here's how.
The dawn-dusk piece is pretty easy - just use an adjustable light-level detector. That part of the circuitry could be a standard combination of light-dependent resistor and op-amp comparator. More challenging was the problem of having the motor change direction depending on whether it is dawn or dusk; whether the door needs opening or closing and sensing where the door is in its travel.
Click here to read more
Click here to see less
The logic would need to prevent the motor running after the door reached the end of its travel - otherwise the motor would likely burn out. It seemed to me I'd need logic that would determine where the door is and to deliver the correct logic output pair until the door reached the correct location: fully open or fully closed.
The problems with that scenario are that there are any number of places the door can be between open and closed that are hard to sense cheaply and rather harder to design position-assessment logic around cheaply. Also, if we care about knowing which end-travel sensors are in which position all the time, then we are having to use three-input logic/truth tables, which are slightly newer to me than two-input logic/truth tables.
I batted that around for three days or so. One of the problems I considered was that building in logical fault sensing was much more complicated if we cared about the door's position. I spent some time considering if the H-bridge's 'enable/disable' pin could be hacked to help out with this.
The breakthrough came when I realised that it doesn't matter whether the system knows where the door is; what matters is where the door is *not* when the light level changes up (dawn) or down (dusk).
Click here to read more
Click here to see less
That revelation reveals that I only needed to create logic around two conditions:
Two reed switches - one at the top of the door's travel and one at the bottom - would provide the position sensing if the door was fitted with magnets that would trip the reed switches. The reed switches' *logic* output could be pre-set by biasing each switch with resistors.
Click here to read more
Click here to see less
With the revelation behind me, I started to look at the logic I'd need, focusing on the chips that are cheap or that I already had in my junk box. The comparator piece was easy: use a 741 or an LM393 along with three 1k resistors from the junkbox. I took an LDR from a discarded night-light.
After some scratching around I realised that the reed switch logic could be simplified if the logic conditions we look for were reduced to:
Light sensor = high + lower reed switch = high (1 + 1)
and
Light sensor = low + upper reed switch = low (0 + 0)
because the first (1 + 1) can be handled by an AND gate and the second (0 + 0) can be handled by an NOR gate. That is: if the reed switches and their voltage bias were judiciously arranged, the 1 + 1 AND condition could handle the 'door opening' condition and the 0 + 0 NOR condition could handle the 'door closing' condition.
Click here to read more
Click here to see less
This logic is shown in the truth tables below:
Door opens
| Light Sensor | Upper Switch | Action | Notes |
|---|---|---|---|
| 1 | 1 | Door opens | Light level went high; AND Gate triggers |
Door closes
| Light Sensor | Lower Switch | Action | Notes |
|---|---|---|---|
| 0 | 0 | Door closes | Light level went low; NOR Gate triggers |
Click here to see complete truth table
Click here to hid complete truth table
| Light Sensor | Upper Switch | Lower Switch | Action | Notes |
|---|---|---|---|---|
| 0 | 0 | 0 | Door close starts | Dusk, close starts after 555-determined delay |
| 0 | 0 | 1 | Door closing | Door has begun to close. Fault condition if prolonged |
| 0 | 1 | 1 | Door closed | This condition should last through night |
| 0 | 1 | 0 | Fault condition | |
| 1 | 1 | 1 | Door open starts | Dawn, open starts immediately |
| 1 | 0 | 1 | Door opening | Door has begun to open. Fault condition if prolonged |
| 1 | 0 | 0 | Door open | This condition should last through day |
| 1 | 1 | 0 | Fault condition | |
Click here to read more
Click here to see less
A little more scratching around revealed that I could use three NOR gates to create an AND gate, which meant that I could use the four NOR gates in the CMOS 4001 in my junkbox to handle all the logic. Two-input logic gates being more common than three-input logic gates, particularly in my junk-box.
So far, the cost of this project was nil.
Click here to read more
Click here to see less
The next step was to figure out how to give the chickens some warning that the door was going to close each night. After all, you don't want your agricultural assets clustered around a closed coop door as the foxes close in.
What I needed was to delay the operation of the H-bridge - and possibly the operation of the CMOS logic gates - when the light level was detected as changing from high to low. During the delay, a piezo-buzzer should sound to warn the hens of the coop door's imminent closure.
Click here to read more
Click here to see less
The difficulty with this trick is that we want to detect only low-going light level changes, not the high-going changes. That is, we only care to insert this delay at dusk, when the light level is falling, and not at dawn, when the light level is rising.
Fixing this proved easy: it's possible to configure a 555-based monostable like the Maplin kit to trigger only on low-going edges by sandwiching a capacitor between two pull-up resistors at the trigger pin (pin 2) of the 555. A couple of diodes inserted after the output stage of the light-level detector and the monostable isolated their respective outputs from each other so that only the 'high' output of either sub-circuit made its way to the inputs of the 4001's NOR gate.
Breadboard tests proved the viability of the design.
Click here to read more
Click here to see less
Careful simulation of lighting and switching showed that the logic and electronics through to the ping 3 and 4 outputs of U3 CMOS 4001 worked correctly.
Testing around U4 L293D showed that its logic power supply on pin 16 Vcc1 and Enable pin 1 was at 2.10 volts instead of the 5.1V theoretically delivered by the combination of 5.1V zener D3 and 1k resistor R14.
Given that this logic power supply had been tested delivering 5.1V before U4 L293D was inserted, it seemed likely that the issue was that the 1k resistor R14 was dropping too many volts at the current required by the L293D's logic power supply Vcc2.
So, let's work out what we need:
Logic Supply Voltage VCC1: 2.14V not 5.1V as it should be
Output Supply Voltage VCC2 in use: 12.5V
Voltage drop is:
6.39V
over 1kR
Logic supply current ICC1 is = 0.0064 6.4mA
According to TI's L293D datasheet, ICC1 is min: 13mA and max (in this application): 35mA
Calculate new R14 resistance:
12.5 - 5.1 = 7.4
At max I<subL</sub> current 7.4 / 0.035 = 211R
At min I<subL</sub> current 7.4 / 0.013 = 569R
Use 200R or close
Calculate resistor wattage rating: 7.4 * 0.035 = 0.26W
Click to read more
Click to see less
Case 1:
Night-time, door closed, 0 + 1 presented to the AND gate; 0 + 1 presented to the NOR gate
Light Sensor = 0 Upper Switch = 1 | | | || || Lower Switch = 1
Case 2:
Dawn breaks, light sensor output goes high, 1 + 1 presented to the AND gate; 1 + 1 presented to the NOR gate
Light Sensor = 1 Upper Switch = 1 | | | || || Lower Switch = 1
Case 3:
Day-time, light sensor output has gone high, AND gate high output has begun to pull door open, lower switch opens and goes low, 1 + 1 presented to the AND gate; 1 + 0 presented to the NOR gate
Light Sensor = 1 Upper Switch = 1 | | || || | Lower Switch = 0
Case 4: Day-time, light sensor output high, door fully open, upper switch closes and goes low, 1 + 0 presented to the AND gate; 1 + 0 presented to the NOR gate
Light Sensor = 1 Upper Switch = 0 || || | | | Lower Switch = 0
Case 5:
During daytime, light sensor output high, door fully open, 1 + 0 presented to the AND gate; 1 + 0 presented to the NOR gate
Light Sensor = 1 Upper Switch = 0 || || | | | Lower Switch = 0
Case 6:
Dusk, light sensor output goes low, door fully open, 0 + 0 presented to the AND gate; 0 + 0 presented to the NOR gate; NOR gate output goes high
Light Sensor = 0 Upper Switch = 0 || || | | | Lower Switch = 0
Case 7:
Dusk, light sensor output low, NOR gate beginning to close door, upper switch opens and goes high, 0 + 1 presented to the AND gate; 0 + 0 presented to the NOR gate; NOR gate output goes high and begins to close door
Light Sensor = 0 Upper Switch = 1 | || || | | Lower Switch = 0
Case 8:
Night-time, light sensor output low, NOR gate completes door-closing, lower switch closes at completion of door-closing and goes high, 0 + 1 presented to the AND gate; 0 + 1 presented to the NOR gate; NOR gate output goes low again as door closes. Same as case 1.
Light Sensor = 0 Upper Switch = 1 | | | || || Lower Switch = 1
Discussion