zondag 17 augustus 2014

Pi-files: Frontdoor light switch based on local sunset/sunrise

I have a couple of front door LED lights which I would like to switch on automatically during the evening/night. The two conventional methods that are commonly available had their disadvantages:


A timer switch is the easiest and cheapest solution, but doesn't take into account day light savings. Besides that, in Einhoven, the Netherlands where I live in december the sun sets around 16:30 while in June it doesn't get dark before 22:00. A simple timer doesn't take that into account either.

Since you only want the lights on when it gets dark, instead of time you can also use a light sensor to distinguish day and night. You have these front door lights that have this built in. The problem that I had with these devices is that they start bouncing ('flickering') around sunset/sunrise. They constantly turn on/off which causes damage to the LED lights I was using. This cost me already several expensive led lights.

Since I already have a Raspberry Pi installed in the hall way (used for the doorbell alert) I might as well hook up the front door lights. You can find on the Internet all sorts of timetables for the sunset/sunrise, so my Rpi should be able to use that same information as well. A small search on the world wide web led me to a Python package called PyEphem. This package can be used to do astronomical calculations and can tell you for example where a certain object (e.g. the sun) is with respect to the observer (which is me standing in my front yard). Very conveniently it also can calculate for you the upcoming sunset and sunrise. Isn't that exactly what I need?!?

The LED lights are powered with a 12V AC-DC transformer. I put together a small electronics board to be able to use the GPIO's 3V3 of the Rpi to switch the 12V LEDs. I used a IRL3103 N-FET. This is not really a deliberate choice but like most of my transistor/FET choices based on what I have lying around in the lab. Be aware that the N-FET needs to be able to switch with a Gate-Source voltage of 3V3. For example an IRF540 (which I used for a motion detector for a 12V ledbar) doesn't switch with this voltage (Here I solved it by using a smaller BS170 N-FET and use that to switch a IRF 9540 P-FET). I added a manual switch to override the automatic switch in case I just want to turn on the lights. Parallel to the front door LEDs is a test led with a resistor onboard to be able to test the functionality without having to connect the actual front door lights.




I use GPIO pins 18 and 22 to switch the two LED's. You can switch LED's with only one output, but I thought it is a nice additional feature to switch them separately to be able to also create a spooky program for Halloween or something like that.
After including the PyEmphem package I have to indicated where the observer is located. I used itouchmap which can translate an address to a latitude and longitude coordinates. Being an observer in Eindhoven means you are located at [51.5, 5.5]. The PyEphem package calculated the next sunrise and sunset and returns these values. I do this every minute and compare the two; if the next sunset if further away in time than the next sunrise, it means it is night. The other way around means daytime. The LED lamps are switched based on the day/night status.

The code can be found here (link):






Geen opmerkingen:

Een reactie plaatsen