As someone looking to build DIY door sensors, you are probably familiar with the idea of protoboards placed in conspicuous locations of your house.
But…What happens if not everyone in your family shares your enthusiasm for experimentation or your cat develops a fetish for slipping wires (which is my case)?
Door sensors are paramount for any Home Automation System and you require something which you can Set and Forget, and which gets the approval from every member of the family, especially the cat.
With this short article, you will learn how to make door and window sensors that run on batteries for extended intervals and don’t seem like volatile artifacts dangling out of your door.
How To Build DIY Door Sensors
DIY Door Sensors: Characteristics
Here’s a brief outline of the features that your door sensors will have:
- The elements used to build the door sensors are low-cost. I really don’t want to go bankrupt within this project.
- It runs on batteries for about a year. We need something which we can set and forget.
- It assesses the battery level to my home automation system. You don’t want to find out that it ran out of battery when you needed it the most.
- It also communicates wirelessly with my House Automation System.
DIY Door Sensors: Requirements
There is an understanding and some tools that you have to acquire before you start this tutorial:
- You want a Home Automation Controller which will get the updates from the door sensors. I would advise that you use OpenHab.
- MySensors also needs an MQTT agent connected to your House Automation Controller.
- You also should build a MySensors Gateway that can relay the messages to the MQTT broker above.
- If you are starting with Arduino you ought to check the Arduino for Dummies Guide, it is going to help you get up to speed.
DIY Door Sensors: List of Materials
Here you may get the listing of components you will need for your door sensors project.
The majority of the things come in packages with various units so you will have the ability to build numerous sensors with them.
S/No | Unit | Description | Comments |
1. | 1 | Arduino Guru Mini | 3.3v 8mhz |
2. | 1 | DC/DC Measure booster | Inside: 0.8, 3.0 Outside:3.3v |
3. | 1 | NRF24L01+ 2.4GHz Wireless RF | |
4. | 1 | Capacitor through-hole | 0,1uF |
5. | 1 | Capacitor through-hole | 4,7uF |
6. | 1 | Pin headers/Jumpers Optional | Cables can be soldered directly to the board. |
7. | 2 | Resistor through-hole | 1M |
8. | 1 | Resistor through-hole | 470k |
9. | 1 | Battery Case Holder | |
10. | 1 | FTDI Programmer | You want these to plan the Arduino Guru Mini in case you don’t have one. |
11. | 1 | Door Sensor or Reed Switch | |
12. | 1 | EasyPCB for MySensors |
Arduino Pro Mini 3.3v 8mhz
Arduino has lots of features, but low power consumption isn’t one of these.
One of the necessities of the project is to be efficient when it comes to battery consumption and for that reason, I want to give you a couple of tips to reduce the Arduino energy intake.
Use the Arduino 3.3v version, the electricity consumption is significantly lower than the 5v version and this project doesn’t need 5V anyway.
Eliminate the voltage regulator and the energy LED. This mode will extend the battery life of your sensor. I’ll leave a video at the conclusion of the tutorial which explains how to desolder those two components. So, if it sounds like gibberish to you, don’t worry.
I should state that this is not necessary for your DIY Door Sensor to operate properly but it is extremely relevant if you would like your node to.
NRF24l01 (Radio)
The NRF24l01 is an inexpensive component that provides the wireless feature to our nodes. It works on 2.4 GHz, therefore, the range isn’t amazing but adequate for an average Home Automation Project.
Let me know if you have various issues, or reside in a house, there are a couple of things which may be done.
DC/DC Measure Up Booster
There are 3 parameters that you should pay attention to without getting into jargon here:
- You’ll be using 2 AA batteries connected in series. This amounts to 3.3V more or less (depending on the batteries)
- The Arduino requires at least 2.9 Volts to work effectively.
- The radio needs at least 1.9 Volts to transmit.
The voltage on a battery falls while it is being used as you probably know. This means that as soon as the voltage of the battery falls below 2.9V your node will soon be left useless.
The solution to the problem above is named Boost Converter. A boost converter is a fancy title for a tool that keeps the output voltage steady irrespective of the input. There is a minimal input voltage depending on the model that you’re using, this one is 0.8v.
Evidently, to step up the voltage, the booster doesn’t do so at no cost, it must step the current down.
Including a boost converter to the circuit can help squeeze all of the juice out of your batteries.
There is a caveat here, the boosters input sound into the circuit along with also the radio (NRF24L01) does not enjoy that. For that reason, the radio has to be powered directly from the battery.
Don’t be worried about it, the PCB which we’ll use has this.
Capacitors
We’ve got two capacitors on the node which can carry out jobs:
- 4.7 uF: This one will probably be attached between the power source and also the radio (NRF24l01). It prevents failure and gives stability.
- 0.1 uF: This capacitor is going to have a similar purpose but is needed because of its voltage measurer. I will discuss this later.
Pinheaders/Jumpers
Jumpers and pinheaders are optional and I do not put them in. I like to solder the parts to the PCB directly.
You might look at adding them if you’re currently considering reusing the identical node for something in the future.
Screw terminal 5.08 spacing
I love to use the screw terminals for the battery although they are optional. When I don’t use them, in my experience, the energy supply is unstable.
FTDI Programmer
The Arduino Guru Mini does not have a USB input that means that in order to program it, you will need a different Arduino or an FTDI programmer.
I find the option to be the one that is most easy.
Battery Vacuum
This is just a piece of plastic to maintain your 2AA batteries. You can get creative and build one yourself but frankly…for what they cost, I’d buy them.
EasyPCB for MySensors
EasyPCB was one of my biggest discoveries. It is a PCB created by Sundberg84 that ignites the main plugins explained here.
It reduces the need to solder a node leaving you with an excellent sensor that doesn’t look like a monstrosity.
Here is a video where Sunberg84 explains the features of this PCB.
Door Sensors
The 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, the normally opened one being the very common one.
How Do They Operate?
As you can see, the door detector has two springs, both of these terminals will be disconnected (switch opened) if there isn’t a magnet close to the switch. Once you bring the magnet into the mix, it stops connecting the two terminals together.
How Can the Microcontroller (Arduino) Leverage On This?
Straightforward. You will be connecting those terminals between one digital input and another.
When the door/window is closed, the switch and the magnet are closing the change and communicating with the two terminals.
This may connect our digital input to GND or VCC, determined by how you wired up things. When the situation changes, your node will send a message to the microcontroller to inform you that the door is opened.
If you haven’t used door sensors before, I would suggest you test it out.
Cable Matters Up
You might be asking yourself why is a resistor between D3 and the Arduino VCC output signal. It’s called pull-up resistor and it is intended to offer the D3 input with a default value.
It follows that if the switch is opened (your door or window is opened) the default value will be HIGH because we have a pull-up resistor connected to D3. On the contrary, when you shut the door, the switch will stop pushing on the minimal standing to D3.
Is the resistor mandatory? Not really.
Arduino and the one from the diagram as well have inner pull-up resistors which you can use to set the default value to HIGH with a value of 1M, respectively.
Why should you really 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 resistor contained in the Arduino board the node would draw more current than it should.
Do not fret a lot about this, the EasyPCB has a placeholder with this resistor.
Battery Measurer
This, in my estimation, is the cooler part of this project. I am discussing the functionality that will examine the status of the battery to your Home Automation Controller. This capability makes the sensor more dependable, helping you to know when the battery is going to die.
The EasyPCB already has all the placeholders to easily build this functionality but if you would like to understand more about it I strongly recommend that you visit Battery Powered Sensors in the MySensors section.
DIY Arduino Door Sensors: More on the PCB
Less talking and more building…it’s time to begin soldering on the board.
In order to give you an idea of how things are going to look, here’s a picture of the before and after the process.
You have to solder the elements in the picture above and they are tagged on the EasyPCB. Having said this, I believe it is worth sharing a few pointers that will assist you.
If your Arduino came without the pinheads soldered to the board, (that is usually the case) solder the pinheads of the FTDI Connector pointing into the reset button, it will allow you to place things better onto the PCB. If you don’t do that, you might bend them backward.
You need to join the two terminals at which it says BAT. You can either use a jumper or just solder them. I solder them and the radio works with it.
If you’re using the 5V version of this Arduino Pro Mini you would have to solder the other jumper (REG) and use a voltage regulator. For this tutorial, it is ideal to use the Arduino 3.3V.
I trace different soldering on the elements, this isn’t mandatory but I think that it makes the process faster and easier.
Solder the 2 terminals at the BAT jumper.
Insert the resistors to the through-hole placeholders.
Place the Arduino and also the radio on the PCB.
Add the capacitors, the screw terminal and the booster.
Remember that the capacitors that you’re using for this tutorial possess polarity. It follows that each leg has its own place. The side is the only one with the stripe (generally white) on the entire body of the capacitor. You will realize that the PCB is labeled with a G on the negative side.
The reed switch, as I explained to you before, ought to go between D3 and GND. You can use pinheads or solder the wires directly to the PCB, I typically act according to my level of laziness that afternoon.
DIY Arduino Door Sensor: The Enclosure
Having enclosures is a portion of the beautification process, so I have designed an enclosure that you can use to place your sensor into.
Here you have a URL to Thingiverse in case you want to have it printed.
There’s space to put the reed switch inside of the enclosure or move the wires through a hole.
DIY Door Sensor: The Code for Arduino
I will walk you through the important areas of the code but you will be able to find the entire version in the GitHub Repository.
#define MY_DEBUG#define MY_RADIO_NRF24 #define DOOR_SENSOR_PIN 3#define MY_NODE_ID 5#define CHILD_ID_DOOR 1#define CHILD_ID_VOLTAGE 2 |
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.
123456 | static const uint64_t UPDATE_INTERVAL = 1800000;int oldBatteryPcnt = 0;int oldValue = 0;float VBAT_PER_BITS = 0.0032551319648094;float VMIN = 1.9;float VMAX = 3.24; |
- 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.
12345678 | MyMessage msgDoor(CHILD_ID_DOOR,V_TRIPPED);MyMessage msgVBat(CHILD_ID_VOLTAGE,V_CUSTOM); void presentation() {present(CHILD_ID_DOOR, S_DOOR);} |
This section declares the MySensors Messages and presents them to the gateway.
123456789101112 | void setup(){ // use the 1.1 V internal reference#if defined(__AVR_ATmega2560__) analogReference(INTERNAL1V1);#else analogReference(INTERNAL);#endif pinMode(DOOR_SENSOR_PIN,INPUT);} |
Define the INTERNAL analog reference.
You wil use this for the voltage measurer.
1234567891011121314151617 | void getBatteryLevel(){ // Battery monitoring reading int sensorValue = analogRead(BATTERY_SENSE_PIN); delay(500); // Calculate the battery in % float Vbat = sensorValue * VBAT_PER_BITS; send(msgVBat.set(Vbat, 3)); int batteryPcnt = static_cast<int>(((Vbat – VMIN) / (VMAX – VMIN))*100.); if (batteryPcnt > 100) { batteryPcnt = 100; } sendBatteryLevel(batteryPcnt);} |
This function calculates the battery level and sends it to the controller.
123456 | void motion_on(){ bool tripped = digitalRead(DOOR_SENSOR_PIN) == LOW; send(msgDoor.set(tripped?”0″:”1″)); // Send tripped value to gw} |
The function motion_on() reads the value from the door sensor and sends it to the home automation controller, in my case OpenHab.
123456 | void loop() { sleep(digitalPinToInterrupt(DOOR_SENSOR_PIN),CHANGE, UPDATE_INTERVAL); motion_on(); getBatteryLevel();} |
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 once every two hours or when your window opens or closes.
Assessing Your Brand New Door Sensor
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 an example based on OpenHab which is the controller that I use.
Items File
Remember to replace the NODE ID and the SENSOR ID used in the items below, using the ones in your Arduino sketch.
123 | Contact mqtt_terrace_contact {mqtt=”<[mosquitto:mygateway1-out/5/1/1/0/16:state:OPEN:1],<[mosquitto:mygateway1-out/5/1/1/0/16:state:CLOSED:0]”} Number mqtt_terrace_voltage “Terrace Voltage [%.2f] V” {mqtt=”<[mosquitto:mygateway1-out/5/2/1/0/48:state:default]”}Number mqtt_terrace_battery “Terrace Battery [%.0f] %” {mqtt=”<[mosquitto:mygateway1-out/5/255/3/0/0:state:default]”} |
Sitemap
If you would also like to add your items to your sitemap, check this example below, it can give you an idea.
1234567891011 | Text label=”Security” icon=”camera” { Text item=mqtt_entrance_door icon=”frontdoor” label=”Entrance” Text item=mqtt_terrace_contact icon=”frontdoor” label=”Terrace” } Text label=”Battery” icon=”battery” { Text item=mqtt_terrace_battery icon=”battery” label=”Battery [%.0f]” Text item=mqtt_terrace_voltage icon=”energy” label=”Voltage [%.2f]” } |
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.
Frequently Asked Questions
How do I automate my home?
- Step 1: Set Up Smart Lights to Turn On/Off on Schedule.
- Step 2: Program Your Thermostat for Energy Efficiency.
- Step 3: Automate All Your Electronics, Appliances and Devices.
- Step 4: Install Smart Locks on Doors for Security.
- Step 5: Switch to Smart Smoke and Carbon Monoxide Detectors for Better Safety.
What are the advantages of home automation?
The Main Advantages
- Managing all of your home devices from one place. The convenience factor here is enormous.
- Flexibility for new devices and appliances.
- Maximizing home security.
- Remote control of home functions.
- Increased energy efficiency.
- Improved appliance functionality.
- Home management insights.