Arduino newbie here.

I'm thinking about building an Arduino-based light switch to control the Philips Hue light system in my living room. I have standard wall light switches that power ceiling lamps, and I basically want all Hue lamps that are scattered around the room to be on when the switch is on, and off when the switch is off, so that I don't necessarily need the app or the Hue remote to turn the room's lights on.

My idea is to install an Arduino on the ceiling instead of a normal lamp, so that its power supply is switchable through the light switch.

I would configure it as a Hue remote control, e.g. with ArduinoHue; if all works well, this would control the Hue lamps directly via ZigBee or via WiFi through the bridge - in both cases without having to use additional wiring.

Whenever it gets power, I would have it turnOn() all lamps, and whenever its power supply is turned off, I would have it turnOff() all lamps. The first part seems simple to me, the second part - not so much.

So here's my question: Can I execute code in the event of the Arduino losing power, i.e. turn off my Hue lights with its last energy before shutting down? Is there maybe some hardware configuration that would allow me to detect the power off event and retain enough energy through a battery or capacitors to still run the code?

I have never worked with Arduino before, but I do have some colleagues that can help me out when there's trouble.

  • Bonus question: am I likely to run into noticeable delays when powering on because the device needs to boot/initialize first?
    – Jan
    Apr 8 '15 at 15:55
  • 2
    2 Options: Know, beforehand that it's going to lose power, or sense this so fast that it's able to execute the code. Use a 'backup' chargable battery that will power the arduino until the lights have been switched off. And then put the arduino in either sleep mode or just power it off.
    – Paul
    Apr 8 '15 at 16:47
  • Is it possible that the program gets disarranged in low power mode?? I did the same with a 1F capacitor to use with a RS 232 connection and it works so far. But sometimes it stops working at all. I guess that at low power, the variables in RAM or the program gets disarranged and fail, after low power and later on power on again. I use counters in the program. Or maybe its not a good idea to use RS232 (with MAX232) where one side always get turned off and on?
    – Mikel
    Feb 21 '18 at 8:52

You could do what you want as you describe it, but are making things harder for yourself that you need to. If you can access the circuit from the switch at or near the switch (which should be 'easy' in most cases) then your system does not have to be depowered when the lighting circuit is as you will have access to the mains AC voltage on either side of the switch.

If you do wish to depower the processor when the lights are turned off you can easily add either a backup battery or a capacitor that runs the processor for "long enough". You can also run a processor in "sleep" mode at extremely low power so that it is always running so have no 'wakeup time". And it is possible to power up a processor so that the delay is not noticeable for practical purposes.

However, as Hue Bulbs are Zigbee controlled, with provision ofr both iOS and Android control, you should be able to control them via Zigbee directly without any connection to their wiring.

Hue control

Hue specifications

  • Thanks! Your idea of accessing the circuit from the switch is intriguing, but won't work in my particular case: not enough room there - especially for the power supply.
    – Jan
    Apr 8 '15 at 18:03
  • How would I use the capacitor to achieve this? And would I have to throw additional hardware at the system in order to sense the main power going off, or could I do this through software only?
    – Jan
    Apr 8 '15 at 18:08
  • Add a diode to the incoming power. Add the large capacitor (or multiple in parallel) after this. Connect one of the arduino pins to before the diode, so you can measure if the input voltage is low (indicating power loss)(assuming a 5v power supply). But I think you'd need a very large capacitor for this.
    – Gerben
    Apr 8 '15 at 18:56

For the capacitor part, two one Farad supercapacitors in series (each 2.7 volts max) cost about a dollar, and there is one 10 Farad cap for two dollars each. I guess that one of these options would work. And the supercaps should last a very long time. I'm not an Electrical Engineer -- just a Joule Thief hobbyist, but I thought this would contribute to your design and wouldn't cost much.

As far as balancing the supercaps, since we're close to the 2.7 volt max, I personally would just put three of the supercaps in series with a voltage divider of three 10K resistors in series tied in, but they would probably not really be necessary.

Oh, and the supercaps I spoke about are actually relatively small (10mm dia x 32mm high for each 10F, and 8mm dia. x 13.5 mm high for the 1F supercaps). Found the 1F at $1.03 as 1572-1270-ND on Digikey. The 10F was $1.97 each for 1572-1287-ND also on Digikey.


Execute Code on power off?

that's like wanting to be alive while dead, or in sleep while awake, :)

the typical way is to wave up periodically; sometimes, run the mcu at very low speed.

the 2nd approach is quite doable on mcus with external r/c oscillator -> you can use a few gpio pins to affect the equivalent R in that r/c oscillator;

for crystal-based oscillator, your options are limited to pll, or fixed dividers if the system offers that option.

  • 2
    He clearly wants to perform actions when power to an external circuit is cut; this is easily achieved via batteries or supercaps, as other answers here have pointed out. WRT to low-speed/sleep modes, the usual ATmega328P has extensive built-in facilities for this; the datasheet devotes 20 pages to this (sections 13 and 14).
    – cjs
    Apr 2 '17 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.