How to Control House Lights From Anywhere

Written by
on April 27, 2015

Notice: Undefined index: share in /var/www/html/ on line 49

In this article, How to Control House Lights From Anywhere, I am going to give a solution for the basic problem; How do you turn a light in your home on or off using your computer, phone or other online device? Also, I aim to control my home’s lights by voice commands from within the home.

The problem:

In a nutshell, the basic problem is, how do you control a light from a webpage? It could be either a webpage or an online app, but the concepts are similar. Also there is the voice control to think about. Voice operation is secondary because once I figure out how to turn the light on and off from my PC, then I can work in a voice control module from that point.

Since I want to control a light from a computer and through the internet, I think my best solution lies withing PHP as a starting point. So, basically I need to figure out how to control a light in my house with PHP. Once that is achieved, I can expand on that solution to do it from my PC, phone, the internet and via voice commands inside my home.

The next thing I did was asked myself: How can I get PHP to interact with the physical world? I had to think outside of the box and came up with using a PC or laptop based in my home as a controlling device. So then my problem becomes a little more focused. I had to find a solution to: “How do you control a light from a PC” Once that’s accomplished, you can communicate with that home-based PC via a network to control your home’s lights from anywhere with any device you like that can get online.

The Solution:

So, knowing that PHP is, for the most part, a web based language and a server-side language, I figured I would need another method of communicating with a hardware device to switch my house lights on and off. However, I thought that I could figure out what languages and technologies to use to control my lights from a PC and later find a way to communicate with the language/technologies I used from PHP.

Long story short, I decided upon using a micro-controller peripheral device known as the BASIC STAMP 2 as the light switch. Therein lies the first coding challenge.  The Basic Stamp 2 is basically a very small computer that was given the name “Stamp” because of it being the approximate size of a U.S. postage stamp. The Basic Stamp 2 uses a coding language named PBasic that is, as the name suggests, a very basic-like programming language. Being unfamiliar with PBasic, I spent the good part of a day learning how to do the basic coding necessary to turn a light on and off from the Stamp 2 Device. Next, I spent the majority of another whole day learning to communicate with the device through a USB port or serial cable from PHP. That’s why I am posting my findings online. If someone else ever has to do something similar or if I have to do it again in the future, they or I can simply reference this post on my website to figure it all out saving many hours of research and testing.

In summary, the solution I am using to control lights(and later other things) in my home, is a combination of PHP, PBasic, PC and the Basic Stamp 2. Here is the workflow:

Workflow for Creating a web-based Light Switch for your Home:

Basically there are three high-level steps to achieving my goal of controlling my home’s lights remotely. They are:

1) Create a circuit using the Basic Stamp 2 micro-controller that can turn a light on and off.

2) Write a program in PBasic to install on the Basic Stamp 2 that can control the circuit in the previous step.

3) Write a program in PHP that can send signals to the Basic Stamp 2 PBasic program letting it know to turn the light on or off.

Step by Step Low Level Instructions

The Step by Step Instructions for Building My Soon-to-be Famous Remote Lighting System are a lot more complex. Here are the step-by-step instructions to build the system I am employing in my smart home:

1) Obtain a device to interface with your PC in order to interact with the outside world(toggle a light). I decided to go with a micro-controller called “Basic Stamp 2” by Parallax. If you want to be able to follow this tutorial, I recommend you use the same one. Otherwise, some of the steps in this tutorial will not apply to your situation. I bought the Basic Stamp 2 HomeWork Board. I suppose you could use any implementation that has the same chip, but this tutorial is based upon using the exact version available on Parallax’s website here:

2) Set up your Basic Stamp 2. I’m not going to go into too much detail about setting up your Basic Stamp 2 because you can get everything you need from Parallax’s interactive “Getting Started” guide found at and also you could download the PDF or read it online at

3) Once you have the Basic Stamp 2 up and running and after you have tested it by writing a simple test program to make sure it works, you can create your light switch circuit. You will need a NPN transistor, a relay to trigger a high voltage (110 AC) light, a basic 470 Ω resistor and some wire leads in addition to a light of course. Here is a very basic switch circuit I got to work, but I must warn you that it doesn’t include the recommended NPN transistor that should go between the no. 14 pin and the relay or the 470 ohm resistor just mentioned. These could be important because most relays in general will throw back power surges to your micro-controller and fry it! I am the dangerous type, so I risked it. Just kidding, I found a reed relay that was considered safe, but it is definitely NOT recommended that you do the same and if you do, it is at your own risk… Here’s the circuit I used none-the-less. Just add the resistor and NPN transistor before the relay to be safe if you’re not sure about the risks:


In the above image, you can see that pin 14 connects to the relay’s positive while the negative connects to the Vss or ground.  The two pins on the reed relay the are in the center on either end are the pins that the higher voltage, 110 vac travels through. Those two pins make a connection when current is applied to the two pins on either side of the relay that are connected to pin 14 and the Vss ground. In the above image, the white and green alligator clips lead to the 110 volt ac power supply and the golden wires lead to a 110 volt light fixture.  Also, in the above image you see a nine pin connector and the one I used has a USB connection. The wiring is the same though. UPDATE: I later added the NPN transistor. See the section below titled “Controlling a 9 or 12 volt relay with 5 Volts” to learn how to do it properly if you don’t have an ultra-sensitive reed relay.

4) Write the PBasic code. Now that you have the circuit, it’s time to write the PBasic program to control it and test it. Here is the code I used to control a single light. Note that the code requires input which will eventually come from a PHP script over a USB cord, but for now you can test it by typing 1 or 2 on your keyboard as long as you keep your debug terminal running while using the editing software for the Basic Stamp 2. Type the following PBasic code into your Basic Stamp editor file and save it as a .bs2 file before downloading it to the device:

‘ {$STAMP BS2}
‘ {$PBASIC 2.5}

‘ This program accepts the numbers 1 & 2 as commands to turn light on off.
‘ Other values produce an error msg in debug term.

‘ ——– Pin Assignments ——–

‘ ——– Constant Assignments ——–

‘ ——– Variable Assignments ——–
Command VAR Byte
Counter VAR Nib


GOSUB GetInput

‘ Receives input from some external source
GOSUB TakeAction

TakeAction: ‘ Determines what to do based on result of GetInput
SELECT Command
CASE “1”
CASE “2”

DEBUG CR, “Turning on light one.”

DEBUG CR, “Turning off light one.”
LOW 14

DEBUG CR, “Please select one or two to turn the light on or off!”

That’s it, now test the code by clicking the run icon in the editor then typing 1 or 2 on your PC’s keyboard to turn the light on or off that is connected to your circuit from the previous step. If it doesn’t work, check to be sure that your computer is using the same com port and baud rate that is set in the program and if not, change the settings in the above file accordingly.

5) Write the PHP code. Next is time to write the PHP code that will interact with the serial port, handing down information to the Basic Stamp 2 device and telling it to turn the 110 volt AC light on and off. Here’s the code I wrote for my first test case that worked great because it is as simple as it gets. You can expand on this later as needed, but here is the basic working PHP code:

exec(“mode com3: BAUD=9600 PARITY=n DATA=8 STOP=1”);// to=off dtr=off rts=off
$fp =fopen(“com3”, “w”);
//$fp = fopen(‘/dev/ttyUSB0′,’r+’); //use this for Linux
//value of bedroom light
$onoff = $_POST[‘bdrm’];
if($onoff == “off”) fwrite($fp, “2”); //write string to serial
if($onoff == “on”) fwrite($fp, “1”); //write string to serial
echo “Light toggled!<br />”;
}//end if sbdrm is hit

Finally, we have the simple version of the HTML code:

<h1>Light Controls</h1>

<h2>Bedroom Light</h2>
<form method=”post” action=””>
<input type=”radio” id=”bdrm” name=”bdrm” value=”on”> On<br />
<input type=”radio” id=”bdrm” name=”bdrm” value=”off”> Off<br />
<input type=”submit” id=”sbdrm” name=”sbdrm” value=”GO!” />


Controlling a 9 or 12 volt relay with 5 Volts

It is often necessary to use either a 9 volt or 12 volt relay to trigger 120 vac, so I’ve added this section to explain how onto this tutorial for the sake of being complete.

Select  a relay that can switch on an off at least 120 vac(or 240 vac if you’re not using common U.S. home mains) that has a coil triggered with 9 volts if possible or 12 volts if that’s all you can find. The 9 volt triggered relay is a little easier to use with the basic stamp 2 module because you can get nine volts from the battery that supplies it’s power. If you use a 12 volt triggered relay you’ll need a completely separate 12 volt power supply to assist in triggering the relay. Here’s how you do it with a 120vac capable relay with a coil voltage of 9 volts using the basic Stamp 2 home work board or similar control module.

  1.  from pin 14, run a lead wire the center pin(base pin) on an 123ap NPN transistor like that found at
  2. The left pin(the emitter pin) of the NPN transistor goes directly to the vss ground on the top right of your breadboard.
  3. The right pin(the collector pin) of the NPN transistor goes to the negative coil pin of the 9 volt resistor.
  4. Then run a lead wire from the positive pin on the relay to the (+)vin at the top middle of the breadboard. This connection will have nine volts as it is from the nine volt battery.
  5. Now you should have a black and a white wire going to the plug of your mains light or 120vac light bulb. run the black wire through the relay and leave the white wire untouched, so that is is a solid, uncut wire still. The black wire runs through the relay’s other two pins. If your relay has more than four pins, consult it’s data sheet for correct wiring procedure. In fact, you should always consult the data sheet regardless.


Leave a Reply

Your email address will not be published. Required fields are marked *