This project is connected to mains. Never touch the relays when the project is powered on. Keep mains wires far away from other parts of the project.
PresentationWhen I go on holidays, I try to discourage robbers by simulating a presence at home with lamps that light up in the evening and in the morning. It's easy to do that with a simple time switch, but the lights always turn on and off at the same hour. Moreover, the lighting time must be adjusted to the season to prevent the lamp from lighting up during the day when the nights are short.
So I made an automatic presence simulator with an Arduino board and an LDR (light dependent resistor) cell. The program knows whether it is day or night thanks to the LDR. It can control two lamps but it is possible to control a larger number of lamps...if you live in a big castle.
The circuit is quite simple.
The lamps are connected to mains through relays controlled by two digital outputs of the Arduino board (2 and 3).
The LDR is connected directly to the +5 V, and to the ground through a 10 kohms resistor. The result of this voltage divider is applied to an analog input of the Arduino board (A5). The system is enclosed in a transparent box so as the LDR can see the ambient light. But if a lamp is in the same room as the transparent box, the LDR may be disturbed when the lamp lights on. This is why a 3.5 mm jack is fixed on a side of the box to be able to connect a remote LDR while disconnecting the internal one.
The Arduino board is powered by an USB charger connected to the mains.
SoftwareDay/night detection
The LDR measures the brightness of the ambient light and provides the result to the program as a number from 0 (black) to 1024 (very bright). The difference between night and day is defined by a threshold. By experience, I found that a value of 200 was convenient for my case. But the transition between night and day is very slow, and a cloud can change the brightness during the transition. We will therefore use hysteresis. When the day is arising, we consider that the day is there when the brightness value is greater than the threshold. But when the day is falling, we wait for the brightness value to be under the threshold minus the hysteresis to decide that the night is there. My hysteresis has a value of 150, but you have to experiment in order to find your own values of threshold and hysteresis.
As a simple explanation, we can say that when the brightness is greater than the threshold, it is the day. When the brightness is lower than the threshold minus the hysteresis, it is the night. And when the brightness is between the threshold minus the hysteresis and the threshold, there is no change, it can be the night or the day according to the previous situation.
Lighting time
We want that a lamp lights up during a random time after the night has began and another random time before the day begins. Moreover, we want that the evening lighting begins a random time after the beginning of the night and that the morning lighting ends a random time before the end of the night. This can be shown in the following drawing:
There are four zones of interest in this scheme:
1) Latency: a random time between the beginning of the night and the evening lighting. We will limit it between 0 and 30 minutes.
2) Evening lighting: a random duration between 30 minutes and 2 hours.
3) Morning lighting: a random duration between 30 minutes and 2 hours.
4) Anticipation: a random time between the end of the morning lighting and the beginning of the morning. We will limit it between 0 and 30 minutes.
We will call limit 1 the beginning of evening lighting, limit 2 the end of evening lighting, limit 3 the beginning of morning lighting and limit 4 the end of evening lighting. At the beginning of each evening, the program calculates new random values for the four limits and for each lamp. They are seen as delays since the beginning of the evening.
There is a problem: it is easy to calculate delays for the evening lighting, but the morning must be anticipated since the lamp must be switched on then off before the beginning of the morning. How can we know when the morning will happen? The solution is to use the LDR to measure the length of one night, and to assume that the next night will be the same length, which is almost true unless you live beyond the polar circle. For the first night, we have no measurement, so we will assume it is 12 hours length.
Functional test
To check that the system is working properly, a test is launched for 10 seconds after powering up. During this test, if the LDR is illuminated, lamp 0 is on and lamp 1 is off. Otherwise, it's the opposite. We can therefore check that everything is working by alternately lighting and darkening the LDR.
Possible improvements
As is, the system is operational. Here are, however, some ideas for improving it.
For example, you could light a lamp on for a few minutes in the middle of the night to simulate an urgent need.
Or you could replace a lamp by a Neo Pixel strip controlled by an Arduino board to simulate the variations of the light produced by a TV set.
And finally, you could connect the system in order to keep contact with it during your holidays.
ConclusionAnd now, if you're robbed, it's not my fault, but you will have tried.















Comments