Saturday, February 12, 2011

Staircase Light Timer

Introduction

We have a timer in our staircase that controls the light after dark: when someone presses a button (a button is located on each floor) the lights light up for a preset period. Lately, the timer in our staircase light has started to behave weirdly - no matter what settings we used, the ON time kept shrinking. By now, not even the fittest can make it after dark from one button to the next while the lights are on. Instead of buying a new timer (for about €5 :-) ) I decided to make it a learning project for myself, as I have been learning PIC micros for a while now. This way, while learning, I also create something useful - for a price that is about 10-15 times higher... The difference is the price of education :-)

Materials

Most of the hardware parts I purchased online, through eBay although I am sure all the parts are usually available in any local hardware/electronics/DIY shop. The problem form me is that there are no such shops in the vicinity of my home.

This is a non-exhausting list of the components used (apart of the electronic components in the circuit). I may have missed an item or two, but the whole list is fairly obviuos.

1 * plastic box (12 * 15 * 6 cm approx) with metallic parts for wall mounting as the project box. Since dedicated project boxes are ridiculously expensive in my area and/or the shipment is very expensive from abroad, I had to settle for this box that is sold as housing for electrical installations. It's not airtight and it has a big window on the front which I had to cover with a scrap piece of plexi glass I had lying around. It would certainly look better in a "proper" project box, but it does the job and in normal operation it will be out of sight anyway.

3 * pushbuttons for testing purposes (up, down, test)

Assorted cables, connectors as necessary

Circuit

At the heart of the circuit is Microchip's 16F628A microcontroller. I chose this microcontroller because it is cheap and I had used this previously for other experiments and had some already.

The 16 (red) indicator LEDs are driven from 3 pins of the PIC using SIPO shift registers (74HC595).

There are also a green LED (power indicator) and a blue LED (state of the relay).

Since most components I used are SMT, I also included an ICSP header on the PCB so that I could program and reprogram the PIC after it has been soldered to the board.

The schematics can be downloaded from here:

Since I designed the circuit I have changed my mind about a number of things. This is due to my inexperience with the PIC microcontroller. If we need another timer controller in the future, I promise I will correct all the inefficiencies in the circuit, the software and the mechanical layout. For now it is working just fine.

Software

As one of my first projects (learning PIC micros as we speak :-) ) the software is not very elegant, to say the least. There are no interrupts used, the microcontroller never sleeps, constantly polling the buttons, etc. I am fully aware of all the shortcomings of it and in any future versions it will be improved. As for now, I was happy that is was working for the first time I put it together.

The software was written in assembly, using Microchip's MPLAB (it's not the most user friendly IDE I have seen, but it's free). The compiled code for the 16F628A can be downloaded from here: stairlight_v1.0.

Operation

When powering up the device first does self check - it lights up all the indicator LEDs one by one, then turns them off one by one. Then for a few seconds it switches on the staircase lights just to show that the relay works OK and all the connections are satisfactorily.

After the initial self test the device enters a waiting mode. In this mode the user may change the length of the ON time for the lights by pressing the UP and/or DOWN buttons. With each push one indicator LED gets switched on/off. There are 16 LEDs in total. At the moment it is programmed so that each LED means 15 seconds of ON time. This means the lights can be configured to be on for 15 seconds to 4 minutes in 15 second steps. For most scenaria this should be OK, but it can be changed in the firmware.

The user may also press any of the LIGHTS ON buttons in the staircase. This switches on the relay in the circuit (and all the lights in the staircase that it switches) and a control LED (blue). The top indicator LED will start blinking. After 15 seconds the blinking LED stays off and the one next to it (in the direction of 0) starts blinking. This procedure keeps repeating until all the indicator LEDs are exhausted. At this point all the indicator LEDs (until the preset interval) light up, and the relay switches off - switching off the lights in the staircase.

During the countdown process the user can press any of the LIGHTS ON buttons in the staircase resetting the countdown timer to expand the time the lights are on.

Extra stuff

The same circuit with only a slight modification in firmware can be used in various other applications that require similar timing, such as delayed switch off of car/boat/etc. cabin light, car wiper intervalled operation, camera interval shooting, etc.

Possible improvements, future plans...

... or what I would do differently if I had to do it again - in no particular order:

I would incorporate the relay on the PCB - this would make the whole device look less cluttered.

When the microcontroller is in waiting mode, it should sleep and it could be waken up by an interrupt when a button is pressed.

I would use a cooler project box ;-)

Update

With the original 12VDC power supply the regulator 7805 IC needed a very large heatsink which I really didn't like. So I was looking around what else I can do. A few days later I found on eBay a 5VDC and 12VDC power supply in one and a compact size. It's the one that are used for external hard disks or DVD drives. I just removed all cables and the enclosure that I don't need, and bypassed the power supply part of my circuit. Now the device works with its own switching mode power supply.

Support my Efforts

Support the development of my future projects!

If you find my project useful, please consider supporting my projects and articles with a couple of euros/dollars. Even the smallest amount helps me create new projects which I will share with the community completely free of charge.

You can tell me which project you would like your support to help, otherwise it will be used for a random project. However, don't feel obliged to do so, I'm already happy to know you are interested in my article. In any case, your help is much appreciated!

Referral links

Maintaining this blog takes time and money. I don't intend to make money on this blog, but I would like to break even. One way of helping me is that you use the below referral links when you want to sign up for any of the below services anyway. This doesn't cost you anything, it helps me a bit and in most cases you also get some discount.