As a cat owner of the geeky persuasion, I am a sucker for pet gadgetry. One such indulgence was the PetPorte Smart Cat Flap, which has a built in microchip reader to control feline access to your home, plus loads of built in features like timer and night modes to accommodate kitty’s curfew. It is more expensive than the alternative magnetic or infra-red fob cat flaps, but if you have escape-artist cats who lose their collars pretty much every time they leave the house, like I do, the cost difference soon balances out.
My experience with the product and company have been great, I do not hesitate to recommend it! Nevertheless I have one niggle – I just can’t get Night Mode to work satisfactorily. So rather than throw an otherwise excellent baby out with the bathwater, I hacked it instead.
Night Mode is for keeping your cat in at night. It uses a light dependent resistor (LDR) to detect the outside light level and decide if it is time to lock or unlock the flap in the outgoing direction. The problem in my case is the flap is fitted to the kitchen door, which being located on the side of the house is permanently in shade. Not only would the light level in the morning be sufficient to open the flap, but turning on the kitchen lights in the evening would unlock it! No amount of messing around with the light level seemed to fix the issue.
Enter the Arduino
Replacing the LDR with a sub-circuit which at the appropriate times switches between two resistances, one for a day-time light level and the other for night-time, would fool the flap into operating correctly in Night Mode. This can be achieved quite simply with an arduino, Real Time Clock (RTC) module, a few resistors and a transistor, all of which I had to hand.
Using a multimeter I measured the resistance of the LDR in the light (250Ω) and in the dark (25KΩ). This gives an idea of the resistances required to fool the flap into thinking it is day- or night-time and hence open or lock the flap.
The simplest sub-circuit I could think of to switch between two resistances was the following:
When the switch is open, the current has to flow through both R1 and R2 giving a total resistance of R1 + R2. When the switch is closed, the current flows through the switch branch rather than the R2 branch giving a total resistance of R1 (assuming the switch has zero or negligible resistance). The values or R1 and R2 were determined by whatever resistors I had at hand that were closest to the values I measured for the LDR.
A transistor is a type of switch which can be controlled using an output pin on an arduino. The arduino starter kit that I first bought came equipped with several P2N2222A transistors. Replacing the physical switch in the previous circuit with this gives the following:
When the arduino pin output is HIGH, the voltage at the base is sufficient that the transistor switch is ‘closed’ and current can flow between the collector and emitter, providing resistance R1 across the sub-circuit i.e. DAYTIME. When the arduino pin is LOW, the transistor switch is effectively ‘open’, providing resistance R1+R2 across the sub-circuit i.e. NIGHTTIME. R3 protects the transistor by limiting the current its base (this resistor came with the transistor in the arduino starter kit so I assume it’s the right value!).
It is possible to calculate the sunset and sunrise times for a given day and location, and fortunately this Sunrise/Sunset algorithm gives exact instructions on how to do this. The times repeat in a 4 yearly cycle, so I used these instructions to write a python script to calculate the sunrise and sunset times for every day in a 4 year period at my longitude and latitude, These are held in lookup arrays in the arduino memory.
Tying it all together, the arduino sketch gets the time and date from the RTC, looks up the sunrise and sunset times for that day, decides whether the cat flap should be locked or open and switches the transistor accordingly.
- Upgrade the RTC – the basic clock I am using is obviously losing time.
- Share the cat flap power supply – the cat flap is already powered with a 12V DC supply. It would free up a socket and neaten things up if it were possible to power the arduino off that same supply.
- Shrinkify the project – using an Arduino Uno to read a clock and switch one pin is a bit of overkill, so I have bought some ATTinys and it would be nice to shrink the project, especially if it would then all fit inside the cat flap housing.