How to control a desk lamp by USB

How to control a desk lamp by USB

Using a Yocto-PowerRelay to switch on a desk lamp from a computer is quite easy. However, to have to power on a computer to switch a lamp isn't really convenient. This week, we present a wiring allowing you to control a lamp both from a computer and from a simple switch, followed by a very handy application: a desk lamp controlled by a screen saver.


To command a lamp from two switches, you need a wiring called "two-way switch". This wiring uses actual switches rather than interrupters. Each time you move a switch, you inverse the lamp state. This type of wiring is commonly used for lighting in houses.

Two-way switch wiring
Two-way switch wiring

You can easily transpose this wiring diagram to a situation where you want to control a lamp both manually and from a computer. You only need to replace one of the switches by a relay, for example a Yocto-PowerRelay. But there is a catch: changing the state of a switch or a relay only inverses the state of the lamp. Therefore, the computer can't deduce the lamp state from the relay state. A solution to this issue consists in deducing the lamp state from the presence or absence of tension in the circuit, with the help of a Yocto-Volt for example.

Computer version with the help of a Yocto-PowerRelay and a Yocto-Volt
Computer version with the help of a Yocto-PowerRelay and a Yocto-Volt


You now know how to build a presence simulator which can drive a lamp, while keeping this lamp manually controllable at the same time. But we wanted to build something a little more sophisticated. Has it ever happened to you to leave your office with the computer on and to forget to come back to turn it off?

Implementation
It is quite easy to ensure that a desktop computer switches itself off after a while. The most recent operating systems even have a setting for this. But who is going to switch off the light?

The idea is therefore to program a screen saver to switch off the desk lamp when it starts and, by the same token, to turn the computer off after another while. Naturally, you must still be able to switch the lamp on and off manually.

Can we hack this lamp to drive it by USB and keep its manual switch?
Can we hack this lamp to drive it by USB and keep its manual switch?


This may look easy at first, but this adds a small issue. We can't use a Yocto-PowerRelay in the two-way switch. When the computer shuts down, the Yocto-PowerRelay isn't powered anymore, which brings the relay back to idle state. The lamp therefore switches back on.

We can solve this issue by inserting a powered USB hub between the computer and the Yocto-PowerRelay. But it seems ridiculous to consume power to keep a lamp switched off.

Another solution is to use a Yocto-Relay to drive a large latched relay. Latched relays have the particularity of keeping their state when they are not powered. They are generally driven by impulsion: an impulsion between two contacts puts the relay in a given state, an impulsion between two other contacts puts the relay in the inverse state.

How bistable relays work
How bistable relays work


The last remaining question is where to find the power to drive the latched relay. Answer: at the only location where there is some power, that is on the USB bus. However, to use power from the USB bus to drive an inductive charge such as a relay is rather risky: you risk having surges propagating themselves on the USB bus. Commuting the relay may also require too much power, to the point of briefly lowering the tension on the bus and to cause a temporary disconnection of the modules. We must therefore protect the USB bus with some diodes and capacitors.

The complete diagram
The complete diagram


To do this the smart way, we decided to put all the electronics inside the lamp stand. Therefore we must fit in the stand a bistable relay , a Yocto-Relay, a Yocto-Volt, a Micro-USB-Hub, and all the wiring. The power supply of the lamp we selected consists of a large transformer which uses up all the space available in the stand. We therefore replace it with an electronic power supply, somewhat smaller. Actually, this is where the real challenge of the project lies: we must find a design compact enough to make everything fit into the lamp stand.

The original power supply
The original power supply


The  Yoctopuce version: quite ugly, but all this jumble will by hidden in the lamp stand
The Yoctopuce version: quite ugly, but all this jumble will by hidden in the lamp stand


Programming a screen saver able to both turn off the computer and drive a desk lamp is of no great interest in the framework of this article. You can nevertheless download it here. It is written in Delphi, the sources are available as well. Here is the final result:

  


Pretty cool, huh? :-)

Add a comment No comment yet Back to blog












Yoctopuce, get your stuff connected.