Wednesday, January 06, 2016

The Automenorah Quest

Well, it has been a long time but I have been doing some traveling. Lately, I returned to an electronics project I had been away from for a while and, well, things took off. I want to document my experience so I an posting here. Not sure if anyone will ever read this but I want to get it down while things are fresh in my mind. Also, this project has been running through my mind for a while like an ear-worm and hopefully this will get it out and onto (virtual) paper so I can concentrate on other things.

So this started many years ago when we were still living in a stick-built house in Connecticut (for the record we have been full-time RVers since 2009 and now have a vacation home in Sarasota Florida as our only residence other than the motorhome we still own). Back then, during the Jewish festival of Hannukah, we would be out fo the house until after sundown and, in celebration, would put an electric Menorah in our front window (it has nine lights on it and the lights are lit one per night plus one that is always lit, to commemorate the eight days of the festival). We would put the Menorah on a timer and would loosen all but the lights to be lit for the specific night so that the right number would come on and shine starting at sundown.

This worked well enough, but we always had to remember to tighten another light down each evening after the Menorah turned off (with the timer) or in the morning before leaving for the day. As an electrical engineer and as avid electronic hobbyist, I realized there may be a better, automatic way to get the lights to come on in order at the right times. Thus began my quest for the "Automenorah".

I sat down and started doing a little designing and realized several things. First, I would need a stable, fairly accurate timing pulse. One based on the 60 Hertz signal in household current might work, if I reduced the voltage, rectified it, and generated a square wave from it to give me a 60/second clock. Also, I would need to have a way to control lights operating on household current with a logic circuit. I considered some sort of FET circuit or perhaps a relay circuit. Besides those hurdles, I had to design the logic circuit which would turn the lights on in the right order and off at the end of the festival and turn them on and off at the right times as well.

My vision was a fairly simple design, where the menorah would be placed on a timer to come on with a single candle lit at sundown on the first night of the festival and then the timing circuit would turn all fo the lights off a few hours later and turn on more lights each night 24 hours after the first lighting, adding to the number of lights, until the end of the festival, when all would be turned off and not come on again. A simple switch could be used to adjust the length of time the lights stayed on and a push button could be used to light more lights as necessary (in case the menorah was plugged in later in the festival than the first night). My initial designs were based on discrete logic circuits (NAND, NOR, counters, shift registers, etc.) and was proceeding but nothing of substance was being completed.

At one point I showed what I was working on to my youngest son, who was studying computer engineering in college, and he asked if I had considered replacing all of my discrete logic with a programmable microcontroller. I said no and asked him for some suggestions (this shows how long I had been out of engineering, since I had not looked into them to see what might be available, how they worked, or how to use them). He put me onto the ATMEL line and I purchased a few ATMEGA32 controllers for some experimentation. It was eye opening and fun to be going down this new path. One thing it did was allow me to solve the timing aspect because the microcontroller uses a crystal controlled signal generator for its internal processing needs and all the timing I needed could be performed in software.

Eventually I was able to create working automenorah based on this microcontroller. It used a small power supply and digitally controlled reed switches to direct line current to the lights. It worked using my original design except that it was hardcoded to be on for 7 hours. This worked well but eventually I was able to find LED lights that I liked instead of the 120V lights. I also found some discounted electric menorahs on sale after the holiday and bought up two of them. I went to work on one modifying it to fit the LED lights and to also individually wire each light (except the Shamus, which was wired to light #1. They are always on together). I had also found the Arduino line of microcontroller boards, which have an on-board programmer and plug right into the computer USB port. This made programming much simpler because I did not have to mount the microcontroller chip in a different place to program and then move it to the circuit board for testing.

The Arduino board allowed me to build everything on a separate board and then plug it into the Arduino for operation. Programming is written in C++ and the USB connection allows for printing information back to the computer for debugging purposes. I was also able to program in the length of lights-on time and make changes as I desired. This was good and I set out this year to create a second one (mainly because I had kind of haked up the menorah to mount the lights and wanted to see if I could start over and do a better job of modifying the lights. I also found some new LED lights I wanted to use).

As I was working on this new menorah, I discovered a real-time clock circuit based on the DS3231RTC chip that could be used with the Arduino board. I did some experimenting with one and liked the results. This allowed me to modify the program to use real time keeping to determine lights on and lights off times. Once I had that capability, I though "why not program in a perpetual calendar so the menorah will always know when Hannukah starts from one year to the next"? Before this, I had hardcoded 12 years of Hannukah starting dates. Once I had a Hebrew calendar built into the code, I realized I could add another feature to the menorah to make it double as a 7-candle menorah to mark Shabbat. I had the day of the week available from the clock so this was somewhat trivial. However, this meant I had to make the menorah work around the calendar year, not only within the narrow time frame of Hannukah (which is right around the solstice and sunset does not change significantly from day to day.

So the next hurdle was finding code to compute sunset times. This worked out pretty well, but I also had to add code to adjust around the calendar for daylight saving time. The DS3231RTC does not have this capability. I was able to generate some code myself to detect when DST is in effect and adjust the sunset times accordingly around the calendar year.I had also found that the ATMEGA chip used on the Arduino boards had EEPROM built in. I was able then to store flags to indicate whether DST was in effect and whether to light the menorah on Shabbat or not. Sunset time is computed and the lights-off time is stored in one of the alarm locations on the DS3231RTC chip.

The code had grown to the point where it would no longer fit on the older Arduino board I had, which used the atmega168 chip. Also, this card had a problem with its serial printing capability that made it miss print when trying to generate long lines of print out (I am doing that with the automenorah code so as to monitor the RTC clock settings and the lights-on/off times). I was just going to give up on that card and had purchased two newer cards for use in future projects, but then I thought of perhaps buying just a new atmega328 chip to replace the older chip on the board. This fixed the serial print problem and also gave me enough storage on the board to go ahead and use it in the older automenorah it was in previously (with all the new code including the Shabbat code after I modified the wiring to divorce the Shamus light from light #1, since they are not used together in Shabbat mode).

I am happy with how this project has turned out. I am considering building two more and sending them to my sons for their use. The only difference I will have to make in their's is the hardcoded latitude, longitude and time zone in the code used to compute the sunset times. The only way I could get away with eliminating this would be to embed a GPS chip (which would be possible except for making sure the antenna could find the satellites) but this might be a bit of overkill. As it is, mine are coded for Sarasota, FL and their's would have to be set for Boston, MA and Portland, OR.

No comments: