Control a Lamp Based on the Position of a Door

This blog describes how to make a system that turns on a lamp when a door is opened. If you haven’t already, watch this video below to get an idea of what the system will do:

The video shows a system that turns on a lamp when a door is opened. First we sense the position of the door using a sensor. Then we use the output of that sensor to control the power to a lamp using a relay. Using the right components, you can build this system easily. This instructable gives you complete details on how to get the components and wire them.

Step 1: Sensing the Position of the Door

First we need to sense the position of the door. An easy way to do this is by using a Hall Effect sensor. These sensors react to a magnetic field. In other words, placing a magnet near a Hall Effect sensor changes its output.

Step 2:

When the magnet is mounted to the door and the sensor mounted to the door frame, we can detect when the door is opened or closed.When the door is opened, the magnet moves away from the sensor and when the door is closed the magnet is moves towards the sensor.

The sensor's output therefore matches the position of the door.

The Hall Effect sensor shown in the picture above is an inexpensive, easy to use device and is available at The exact link for it is below:

We'll go over the exact wiring for this sensor later in the instructable.

Step 3: Controlling the Lamp

Next we need to control the power to the lamp. Using a relay board is one of the easiest way to control the power to any device. A relay is like a switch that you can control electronically. The relay board that we'll use has a built-in relay driver that allows a very small signal - such as the output of the hall effect sensor to control the relay.

Here is the link to the relay module's product page.

Step 4:

When a small signal is input to the blue connector, the driver on board powers the relay. This, when wired correctly can control the power to any device via the green connector.

Please note that using a relay board with high voltages/high currents can be dangerous!! In this project we are switching the AC voltage into the lamp - extreme care should be taken while building this project. Do not attempt to build this project if you are unfamiliar or uncomfortable working with AC voltages

Step 5: Wiring the Project

The wiring for this project is extremely simple. It is especially easy because the relay board supplies power to the sensor. You will need:

1) About 7 feet of 22AWG hookup wire that you can buy at your local Radio shack / Home depot
2) A long extension cable that you can buy at Home depot. Make sure to buy one that can handle the power to your floor lamp
3) Magnets from Radioshack / Home depot
4) A lamp
5) A relay board from ezmodule (link: )
6) A hall effect sensor from ezmodule (link: )
7) A 100 ohm resistor you can buy at ezmodule (link: )

Step 6: How to Wire the Project

Before we begin, make sure there is no power running through the components, and that you have taken sufficient safety measures like safety glasses etc.

Lets start from the left side. The sensor requires between 3.3V to 6VDC to operate. The relay board we use here has convenient pads for 6V power ( look at bottom left corner of relay board as shown in picture above). We'll use these pads to power the hall effect sensor.

Step 7:

The 100 ohm resistor can be attached to the relay module's blue connector as shown in the picture below:

Step 8:

The two pins of the hall effect sensor should be connected to the relay module using  the 22AWG cable. This way, there will be sufficient slack on the wires when the sensor is attached to the door frame.

Now attach the sensor and magnet to the door frame and door respectively:

Step 9: AC Wiring

Next we'll wire the AC side. Take the extension cable and locate the wire that carries the line voltage. If you're using a plug in the u.s.a., the pin that carries line voltage is the shorter one as shown in the picture below:

Step 10:

Next cut and strip the wire connected to the pin carrying the line voltage as shown in pictures below ( note that you should cut just the wire that will carry the line voltage only, not both wires):

Step 11: Connect to Relay Board

Now connect these two stripped wires into the NC and COM lines on the green connector on the relay board as shown below:

Step 12: Plug the Floor Lamp Into Extension Cable

Next connect the power plug of the floor lamp into the socket end of the extension cable:

Step 13: Plug Into an AC Outlet (safety First!)

At this point, you should insulate all components from any metal objects and people. When this project is powered up, AC voltages will be present in several sections of the components and therefore care should be taken to isolate all items from any metal objects and people.

Make sure all connections are correctly wired, and that there are no loose ends, or loose wires. Now power up the system by connecting the AC line to a wall socket:

Step 14: System Operational!

At this point, you should be able to change the position of the door and see that the lamp switches on and off. If the system doesn't perform as expected, power it off first before you handle any wire or component. Make sure you power off by pulling out the plug from the wall socket.

Since magnets come in different shapes, sizes and strengths, you might have to adjust the position and mounting angle of the magnet to get it to work properly.

I hope you have fun with this project!



    • Paper Contest

      Paper Contest
    • Build a Tool Contest

      Build a Tool Contest
    • Sweet Treats Challenge

      Sweet Treats Challenge

    4 Discussions


    5 years ago

    Thanks! I should be making more soon.


    5 years ago on Step 4

    nice article.. if you can not find half sensor, you can change that sensor with Refrigerator Door Switches. But must modify you door..