How to Build DIY Door Sensors for your Home Automation Project

As a DIY person, you are probably comfortable with the idea of protoboards placed in conspicuous locations of your house.

But…What happens when not everyone in your household shares your passion for experimentation or your cat develops a fetish for stealing cables (this is my case…)

Door sensors are paramount for any Home Automation System and you need something that you can Set and Forget and that gets the approval from every member of the family, especially the cat.

On this tutorial, you will learn how to create door and window sensors that run on batteries for long periods of time and don’t look like explosive artifacts hanging from your door.

This post is meant to be the second part  DIY Home Automation Sensors: The Ultimate Guide. I would advise you to check that one first, you will need some familiarity with the MySensors library for this project.

 

DIY Door Sensors: Features

Here is a brief outline of the features that your door sensors will have:

  • The components used to build the door sensors are low-cost. I don’t want to go bankrupt over this project.
  • It runs on batteries for about a year. We want something that we can set and forget.
  • It reports battery level to my home automation system. You don’t want to discover that it ran out of battery when you needed it the most.
  • It communicates wireless with my Home Automation System.

 

DIY Door Sensors:  Requirements

Before you start this tutorial, there is some basic infrastructure and knowledge that you need to acquire:

  • You need a Home Automation Controller that will receive the updates from your door sensors. I would recommend you OpenHab.
  • MySensors also needs an MQTT broker connected to your Home Automation Controller.
  • You also need to build a MySensors Gateway which will relay the messages to the MQTT broker above.
  • If you are starting with Arduino you should check the Arduino for Dummies Guide, it will help you get up to speed.

 

DIY Door Sensors: Bill of Materials

Here you can find the list of components that you will need for the door sensors project.

Most of the items come in packages with several units so you will be able to build several sensors with them.

Unit Description Comments
1 Arduino Pro Mini 3.3v 8mhz
1 DC/DC Step up booster In: 0.8 – 3.0 Out:3.3v
1 NRF24L01+ 2.4GHz Wireless RF
1 Capacitor throughhole 0,1uF
1 Capacitor throughhole 4,7uF
1 Pinheaders/Jumpers Optional. Cables can be soldered directly to the board.
1 Screw terminal 5,08 spacing Optional. Cables can be soldered directly to the board.
2 Resistor throughole 1M
1 Resistor throughole 470k
1 Battery Case Holder
1 FTDI Programmer You need one of these to program the Arduino Pro Mini if you don’t have one already.
1 Door Sensor or Reed Switch
1 EasyPCB for MySensors

 

 Arduino Pro Mini 3.3v 8mhz

Arduino is a very powerful micro-controller with lots of features,  but low power consumption is not one of them.

One of the requirements of the project is to be very efficient when it comes to battery consumption and for that reason, I want to give you a few tips to reduce the Arduino power consumption.

  • Use the Arduino 3.3v version, the power consumption is significantly lower than the 5v version and this project doesn’t require 5V anyway.
  • Remove the voltage regulator and the power led. This mod will significantly extend the battery life of your sensor.  If this sounds like gibberish to you don’t worry, I will leave a video at the end of the tutorial that explains how to desolder those two components.

I should say that this is not required for the DIY Door Sensor to function correctly but it is very relevant if you want your node to run on batteries for a long period of time.

 

NRF24l01 (Radio)

The NRF24l01 is an inexpensive component that adds the wireless feature to our nodes. It works on 2.4 GHz so the range is not astonishing but sufficient for an average Home Automation Project.

Let me know if you have range issues, or live in a big house, there are a few things that can be done.

 

DC/DC Step up booster

Without getting into unnecessary jargon here, there are 3 parameters you should pay attention to:

  • You will be using 2 AA batteries connected in series. This amounts to 3.3V more or less (depending on the batteries)
  • The Arduino needs at least 2.9 Volts to perform correctly.
  • The radio needs at least 1.9 Volts to communicate.

As you probably know, the voltage on a battery drops with its usage. This means that as soon as the voltage of the battery drops below 2.9v your node will be rendered useless.

The solution to the problem above is called Boost Converter. A boost converter is a fancy name for a component that keeps the same output voltage regardless of the input. There is a minimum input voltage depending on the model that you are using, this one is 0.8v.

Obviously, the booster doesn’t do this for free, in order to step up the voltage, it has to step down the current.

Adding a boost converter to the circuit will help to squeeze all the juice out of your batteries, prolonging the life of your node.

There is a caveat here, the boosters input noise into the circuit and the radio (NRF24L01) doesn’t like that. For that reason, the radio needs to be powered directly from the battery.

Don’t worry about this, the PCB that we will use has this covered.

 

Capacitors

We have two capacitors on the node that will carry out different tasks:

  • 4.7 uF: This one will be connected between the power supply and the radio (NRF24l01). It provides stability and avoids failure in the communications.
  • 0.1 uF: This capacitor will have a similar function but for the voltage measurer. I will talk about this later.

Pinheaders/Jumpers

Pinheaders and Jumpers are optional and I generally don’t put them in. I like to solder the components to the PCB directly.

If you are thinking of reusing the same node for something else in the future, you may consider adding them.

 

Screw terminal 5,08 spacing

They are optional but I like to use the screw terminals for the battery. In my experience, the power supply is less stable when I don’t use them.

 

FTDI Programmer

The Arduino Pro Mini does not have a USB input which means that in order to program it, you need an FTDI programmer or another Arduino.

I find the first option to be the easiest one.

 

Battery Holder

This is just a piece of plastic to hold your 2AA batteries. You can get creative and build one yourself but honestly…for what they cost, I would buy them.

 

EasyPCB for MySensors

The EasyPCB has been one of my biggest discoveries lately. It is a PCB created by sundberg84 that leverages the main functionalities described at the MySensors forum.

It basically reduces the time to solder a node to a few minutes, leaving you with a quality sensor that doesn’t look like a monstrosity surrounded by cables.

Here is a video where Sunberg84 explains the features of the PCB.

 

 

Sundberg releases new revisions of the PCB every now and then so when you receive them at home, the components might be arranged slightly differently.

 

Door Sensors

Arduino DIY Door Sensors: Door Sensor

The reed switch is the bread and butter of the door sensors project. When you receive it at home, it will look like the picture above.

They can come in two modes, normally opened and normally closed, being normally opened the most usual one.

 

How do they work?

 

Arduino DIY Door Sensors: Reed Switch

 

I have borrowed this image from RandomNerdTutorials, great site in case you didn’t know about it.

As you can see, the door sensor has two terminals, these two terminals will be disconnected (switch opened) when there isn’t a magnet closed to the switch. At the moment you approach the magnet to the switch, it closes connecting the two terminals together.

How can the microcontroller (Arduino) leverage this?

Very simple, you will be connecting one of the terminals to a digital input and the other one to VCC or GND.

When the door/window is closed, the switch and the magnet are together closing the switch and communicating the two terminals.

This will connect our digital input to GND or VCC, depending how you wired things up. When the situation changes, your node will send a message to the microcontroller to notify that the door is opened.

If you haven’t used door sensors before, I would suggest you play around with it first.

 

Wire Things Up

There are many ways you can connect the Door Sensor but my recommendation is to connect it according to the following diagram.

 

Arduino DIY Door Sensors: Door Sensor Connection Diagram

 

You might be wondering why there is a resistor between D3 and the Arduino VCC output. It is called pull-up resistor and it is intended to provide a default value to the D3 input.

This means that when the switch is opened (your door or window is opened) the default value will be HIGH since we have a pull-up resistor connected to D3. On the contrary, when you close the door, the reed switch will close pushing the LOW status to D3.

Is the resistor mandatory? Not really…

Arduino has an internal pull-up resistor that you can use to set the default value to HIGH, however, this resistor has a value of 20K and the one in the diagram above has a value of 1M.

Why do you care about that?

Mr. Ohm can shed some light on this…

As you know,  I = V/R

20K Resistor:

I = 3.3v/20000Ω  = 0.000165 A = 165 uA

1M Resistor:

I = 3.3v/1000000Ω  = 0.0000033 A = 3.3 uA

If we used the pull-up resistor included in the Arduino board the node would draw more current than it should.

Don’t worry too much about this, the EasyPCB already has a placeholder for this resistor.

 

Battery Measurer

This is, in my opinion, the cooler part of the project. I am talking about the functionality that will report the status of the battery to your Home Automation Controller. This capability makes the sensor more reliable knowing that you will be notified when the battery is about to expire.

The EasyPCB already has all the placeholders to easily build this functionality but if you want to know more about it I strongly recommend you to visit  Battery Powered Sensors at the MySensors forum.

 

DIY Arduino Door Sensors:  Soldering the PCB

Less talking and more building…It is time to start soldering the board.

In order to give you an idea of how things are going to look, here is a picture of the before and after soldering process.

 

Arduino DIY Door Sensors: Before and After PCB

 

You only need to solder the components in the picture above and they are all labeled on the EasyPCB, having said that, I think it is worth sharing a few tips to help you throughout the process.

  1. If your Arduino came without the pinheads soldered to the board (which is usually the case) solder the pinheads of the FTDI Connector pointing to the reset button, it will help you fit things better on the PCB. If you forget to do this, you can bend them backward afterward.
  2. You need to connect the two terminals where it says BAT. You can either use a jumper or just solder them together. I generally solder them since I always power my nodes using 2 AA batteries and the radio works perfectly with it.
  3. If you are using the 5V version of the Arduino Pro Mini you would need to solder the other jumper (REG) and use a voltage regulator. For this tutorial is best to use the Arduino 3.3v
  4. I follow a particular soldering the components, this is not mandatory but I think it makes the process quicker and easier.
    1. Solder the two terminals of the BAT jumper.
    2. Add the resistors into the through-hole placeholders.
    3. Place the Arduino and the radio on the PCB.
    4. Add the capacitors, the screw terminal and the booster.
  5. Remeber that the capacitors that you are using for this tutorial have polarity. This means that every leg has its place.  The negative leg is the one with the stripe (usually white) on the body of the capacitor. You will see that the PCB is labeled with a g on the negatieve side.
  6. The reed switch, as I explained to you before, should go between D3 and GND.  You can use pinheads or solder the cables directly to the PCB, I generally act based on my level of laziness that day…

And here it is!! the DIY Door Sensor!

Arduino DIY Door Sensors: Door Sensor Finished

 

 

DIY Arduino Door Sensor:  The Enclosure

Having nice enclosures is part of the beautification process so I have designed an enclosure that you can use to put your sensor in.

Here you have a link to Thingiverse in case you want to get it printed.

There is space to put the reed switch inside of the enclosure or pass the cables through a hole.

 

 

Arduino DIY Door Sensors: Enclosure

DIY Door Sensor:  The Code for Arduino

I will walk you through the relevant parts of the code but you will be able to find the full version in the GitHub Repository.

 

The main things you need to pay attention to here are:

  • DOOR_SENSOR_PIN tells Arduino where you have connected the door sensor. If you followed the diagram it will be 3.
  • MY_NODE_ID: This identifies your node in the network of sensors. It should be unique for every node (sensor) that you have.
  • CHILD_ID_DOOR and CHILD_ID_VOLTAGE identify sensors within the node. This will make more sense once we create the items in OpenHab.

  • VMIN and VMAX are parameters to calculate the percentage of the battery. I set VMIN as 1.9v since it is the lowest voltage that the radio can deal with. VMAX is the voltage that the 2AA batteries in series will normally have out of the box.
  • I am measuring the battery using one of the analog inputs (A0) and the possible values will go from 0 to 1023. In order to transform that number (0-1023) into a voltage, we use the  VBAT_PER_BITS which is basically 3.33v/1024.

This section declares the MySensors Messages and presents them to the gateway.

 

Define the INTERNAL analog reference.

You wil use this for the voltage measurer.

 

This function calculates the battery level and sends it to the controller.

 

The function motion_on() reads the value from the door sensor and sends it to the home automation controller, in my case OpenHab.

 

the main thing you need to pay attention to here is the function sleep. It tells Arduino to sleep all the time and only wake up under two circumstances:

  • Every X amount of milliseconds specified by the variable UPDATE_INTERVAL. In my case, it is 2 hrs.
  • Every time that there is an interruption coming from the DOOR_SENSOR_PIN.

This basically means that your node will be sleeping all the time and will only wake up every two hours or when your window opens or closes.

 

Testing your brand new Door Sensor

At this point, your door sensors should be up and running but how do you know everything worked?

I like to use a tool called MQTT.fx, it is a simple free tool to monitor your MQTT network.

Just subscribe to the topic mygateway1-out/MY_NODE_ID/#

Remember to replace MY_NODE_ID by the number you used in your Arduino sketch.

 

Arduino DIY Door Sensors: MQTT FX
LEAD Technologies Inc. V1.01

If you see messages showing up in MQTT.Fx you can consider the experiment a success 🙂

 

Adding the Door Sensors to your Home Automation Controller

The sensor that you just built will be compatible with any Home Automation controller that supports MQTT, which is pretty much all of them out there…

I am going to give the example for OpenHab which is the controller that I use.

Items File

You should already be familiar with this part from the MQTT Tutorial that I wrote a few months ago but here you have some details on how to add these particular door sensors to your current OpenHab implementation.

Remember to replace the NODE ID and the SENSOR ID used in the items below, using the ones in your Arduino sketch.

Sitemap

If you would also like to add your items to your sitemap, check this example below, it can give you an idea.

 

 

Once you have it configured it will look similar to the screenshot below.

Arduino DIY Door Sensors: OpenHab Sitemap

I hope you have enjoyed this tutorial on how to build DIY Door Sensors.

If you have questions on the post let me know in the comments. Also, if you think your friends might like this share it, please! It helps to get the word out there.

 

Additional Resources

Here you have a few additional resources that might help you with the content of the blog post.

The first resource is the MySensors website. I always go there when I am looking for inspiration 🙂

If you have started with the tutorial and you want to know how to remove the voltage regulator and the led from the Arduino board here is a neat video to do just that.

 

 

Lastly, for newbies to the Arduino Pro Mini, here there is a video on how to solder the pins to the board.

 

This Post Has 6 Comments

  1. riemy

    And what can you expect in terms of battery lifetime until you have to switch them?

  2. David C.

    Hi Riemy,

    I started using the first node around 9-10 months ago and it is still alive an well. Unfortunately, I was not monitoring the battery level yet. I have started monitoring a month and a half ago and the battery has dropped 4%.

    My expectation would be for the battery to last for about a year but we will see.

    I see by your email address that you have started a blog. What is it about?

  3. lucas

    If i had two doors with these sensors, how would i be able to tell wich door has opened ?

  4. David C.

    Hi Lucas,

    Each node will have a different id, you can define that with the parameter:

    #define MY_NODE_ID 5

    They will publish the events on different queues.

    OPENHAB:
    Contact mqtt_terrace_contact {mqtt=”<[mosquitto:mygateway1-out/{MY_NODE_ID}/1/1/0/16:state:OPEN:1],<[mosquitto:mygateway1-out/{MY_NODE_ID}/1/1/0/16:state:CLOSED:0]"}

  5. Jesús Emmanuel Lopez Orozco

    Hi, could u share the schematics of what u connect and where… I have a 3d print and would like to make it smaller the enclosure.

Leave a Reply