At home, I wanted to install lights along the fence to create an ambiance in my backyard. I wanted to have the lights turn on at sunset and turn off a few hours later, entirely automatically. The last requirement was to be able to control the system remotely. I also wanted the option to create a "light show" for special occasions, such as holidays or parties.

Hardware
For this project, I used a Raspberry-Pi Zero W that I wasn't using for anything else. Since it runs Linux, I could use the network stack and use NTP1 to synchronize the internal clock. I could also use python for my program. Since the final box would be installed outdoors in my backyard, using ssh to investigate any problems would be a huge plus.
The lights controller is built entirely around off the shelf components:
- Raspberry Pi Zero-WH
- Relay Board
- Buck converter
- Screw Terminal Strip Blocks
- Heavy-duty waterproof box
- Plus an sd-card and some hardware
Hardware-wise this project was easy to build. Where I had more fun was on the software side. To write the software, I used python. I also used greenlets. Greenlets provide concurrency. Several parts of the code can run simultaneously but not in parallel. Think of lightweight threads.

Software
The core of this program is a CronTab
class responsible for running Tasks
or Events
. Tasks are jobs that run only once at a specific time. Events are recurrent jobs.
In the following example, the Task
will turn the lights on at sunset. The lights are turned off at 10:30 daily. The function light_show is called daily at the top of the hours at 9, 10, and 11 PM.
cron = CronTab(
Task(lights.on, sun.sunset.minute, sun.sunset.hour),
Event(lights.off, 30, 23),
Event(light_show, 0, [21, 22, 23], lights=lights)
)
To determine the sunset time, I use an online service called sunset sunrise. This service offers a free API that provides sunset and sunrise times for a given latitude and longitude. There is a limit on the number of requests you can make to the service. Our lights project only makes a handful of requests per week, which is way under the quota.
To run this program, create a JSON configuration file stored on /etc/lights.json
. The fields latitude
and longitude
are used to determine the sunset time. The field ports
contains the GPIO2 pin controlling each light.
Example:
{
"local_tz": "America/Los_Angeles",
"ports": [ 9, 11, 0, 5, 6, 13, 19, 26 ],
"latitude": 37.4650,
"longitude": -122.2475
}
You can find the full program on my GitHub account.
The next version of this program will read the sequencing of the lights from a configuration file.