Gate Alarm
Whys and Wherefores
Every morning, before letting our dog out in the back garden, my wife or I go outside to check our garden gates are bolted properly. Apart from frequently getting cold and wet, we're also concerned about the increase in dog theft and so want to know if anyone opens the gates.
The solution for one gate is simply to lock it. We can't do that with the other because occasional access is needed for deliveries.
This project aims to solve the problem of the second gate by providing a sensor that detects when the gate is open and sounds an alarm if so.
Requirements
The requirements for the alarm are:
- The gate sensor should only detect an open gate and should trigger the alarm when this occurs. It should not attempt to detect the gate being closed: this is entrusted to the humans!
- Related to 1, there must be a way of cancelling the alarm, effectively confirming that the gate has been closed.
- Since there are times when the gate needs to be left open, there should be a way of suspending, and re-activating, the alarm. However, should the gate be detected as open during a suspension then the alarm must be activated after the period of suspension has ended.
- There should be both audible and visual indications when the alarm is activated.
- The audible alarm should be able to be switched off, for example at night.
Design
The design has gone through a few iterations for deciding on the plan described here. Earlier plans are described on the Earlier Plans for the Gate Alarm.
There are three main components of the alarm system:
- A sensor mounted on the gate to detect when the gate opens.
- An alarm.
- A control unit.
Gate Sensor Unit
The unit that senses the gate being opened is based on reed switches and magnets. A case containing three reed switches in parallel is to be mounted offset from the gate post. A magnet is to be placed on the gate at the same height as the reed switch case. When the gate is opened (or closed) the magnet passes the reed switches which close an emit a pulse that can be detected by a microcontroller.
Earlier plans proposed using three reed switches to detect the direction of gate movement. This unit was built for testing. When that plan was rejected it was decided to re-purpose the unit with the reed switches wired in parallel to provide some redundancy in the sensor.
Here's a rough diagram of how the switch case and magnet are to be mounted:
There are two reasons that the reed switches are offset from the gate when closed:
- Since the reed switches are normally open, if they were mounted opposite the magnet when the gate is closed the switches would be closed and drawing power all the time that the gate is closed.
- A pulse will be generated as the magnet passes the reed switches when the gate is opened. This pulse can be detected by a microcontroller which can in turn trigger the alarm. Additionally, the only way that the pulse can generated is when the gate is open, regardless of the direction the gate is moving, potentially blowing around in the wind.
Note that there is no way for the gate sensor to detect the gate being closed. This is fail safe: someone will need to check that the gate has been closed and will reset the alarm manually.
Alarm
The alarm comprises two parts:
- A buzzer to provide the audible alarm.
- A red LED to provide the visual alarm.
Both the buzzer and the LED are triggered from a microcontroller via a MOSFET and a BJT respectively.
Control Unit
The control unit comprises the following:
-
LCD screen to display the current state of the alarm and to prompt for input.
-
A keypad that is used to control the alarm. The following commands are recognised:
Entry Meaning A positive number followed by #
Suspends the alarm for the given number of minutes. 0
followed by#
Cancels any active suspension of the alarm. #
Suspends the alarm indefinitely. *
Resets any active or suspended alarm to indicate that the gate has been closed. If the gate is opened while the alarm is suspended then the alarm enters a pending state: it will not sound while the suspension continues, but will sound once the suspension ends. Reseting the alarm while pending cancels the pending state.
-
A toggle switch to disable the buzzer.
-
A test switch to emulate the gate being opened.
-
A "heartbeat" LED that flashes every few seconds to indicate that the unit is operational.
Circuit Design
The complete circuit is described by the following diagram:
The circuit is based around the Arduino Nano. The pinout is given in the following table:
Nano Pins | Type | Connects To | Notes |
---|---|---|---|
D2 | Input | Gate sensor / test switch | Pin is taken low when the sensor switches or test switch close. There is a physical pull up resistor for the switches, so the Nano pin does not need to be configured to use its internal pullup resistor. There is no debouncing circuit: debouncing is done in software. |
D3 to D9 | Input | Keypad | The keypad is multiplexed to use 7 outputs: one for each of its 4 rows and one for each of the 3 columns. The signal is decoded in software. |
D10 | Output | Buzzer circuit | |
D11 | Output | Alarm LED | When the pin is taken high the transistor that powers the red LED is switched on. |
D12 | Output | Heartbeat LED | When the pin is taken high the transistor that powers the yellow LED is switched on. |
A4 | Output | I²C SCL | I²C clock signal for controlling the LCD. |
A5 | Input or Output | I²C SDA | I²C data signal for controlling the LCD. |
5V | - | 5V input | The Nano is powered from the 5V power supplyvia this pin, not via it's own USB socket. |
GND | - | Ground connection | Common ground connection with the power supply and every other assembly's ground. |
Other aspects of the circuit to note are:
- A 680µF electrolytic capacitor connects 5V to ground to smooth out the power supply.
- No microcontroller pins are used to power the LEDs or the buzzer. The former are powered from the 5V supply by BJTs and the later by a MOSFET.
- A 3.3V Zener diode is used to drop the voltage for the buzzer from 5V to 3.3V. The same diode is also used as a flyback diode for the buzzer.
Progress
Sensor Unit & Magnet
This unit was originally built as part of the earlier design and adapted to suit the revised design.
In the original build, the three reed switches were mounted inside an old 3 × AA battery box, modified to suit. The switches were soldered to perf board then mounted hard against one side of the case and held in place with hot glue. The switches were commoned at one end and the common wire and one additional wire per switch were brought out to four header pins outside the case. Each connection was duplicated inside the case to provide some redundancy in case a wire breaks. All the electrical parts were then covered in hot glue, which was also used to seal on the case lid. A block of wood on which to mount the case was screwed to the gate post and the case was drilled in three places to permit it to be screwed to the mounting block.
The unit was later modified by commoning the three switch terminals together so that all three switches operate in parallel:
The modified terminals and wires were covered in red heat shrink tubing. The two output wires were then connected to 2 5A screw terminals as can be seen in the following image:.
A redundant kitchen cupboard door magnet was tested on the gate and found to work. However some stronger neodymium magnets have now been acquired and may be used instead.
Control Unit & Alarm
The control unit and alarm are both being housed in the same box, along with the microcontroller and circuit boards.
Power will be provided by a 5V USB wall wart and will enter the unit via a micro USB socket.
A case has been designed using Inkscape and paper templates have been printed from the design. A prototype case has been made from mounting board and all externally mounted controls have been test fitted. Here is the design for the case looking from the top, with side mounted components shown in grey:
Once the prototype has been tested it is planned to build the box from plywood.
Ultimately it is hoped to replace the plywood box with a 3D printed copy.
Each of the externally mounted components illustrated above has been designed to be plugged in to the main circuit board, making it easier to disassemble the finished unit. The assemblies are:
USB micro power input board
A USB micro socket has been soldered to a piece of perfboard and the 5V and GND pins have been taken out via leads terminated in a 2 pin JST PH 2.0 plug.
The leads are connected to the main circuit board from where power is distributed to other components.
Input from gate sensor / Test switch
The momentary contact push button test switch has been wired in parallel with the input from the gate sensor so that pressing the switch has exactly the same effect as the reed switches being closed. Input from the gate sensor is by means of a 5.5×2.1mm DC socket. The GND and MCU pin connections from both are taken out via leads terminated in a 2 pin JST PH 2.0 plug.
The GND and MCU leads are connected to the main circuit board.
LCD
The display is a 16×2 blue backlit display with a built in I²C interface.
The display's VCC, ground and I²C SDA & SCL connections are brought out to four DuPont connector pins. 2 pairs of leads terminated in 2 pin JST PH 2.0 plugs have been connected to a 4 way DuPont socket that attaches to the display's connector pins. One pair of leads is for power (the 5V VCC and ground) connection, while the 2nd pair of leads carries the I²C SDA and SCL signals.
The power leads are connected to the main circuit board while the I²C leads are connected to the microcontroller.
Keypad
A 4×3 button membrane keypad is used. The membrane has 7 connections that are brought out in a ribbon cable that is terminated in a 7 way DuPont socket.
The keypad is to connected directly to the microcontroller. The method for doing this is yet to be determined.
Buzzer
A proprietory 3.3V buzzer is being used. The buzzer has positive and ground wires that have been terminated in a 2 pin XH2.4 plug.
The leads are connected to the main circuit board.
Buzzer isolation switch
The buzzer isolation switch is a SPST on-off rocker switch. A pair of leads have been soldered to the switch. The leads terminate in a 2 pins JST PH 2.0 plug.
The leads are connected to the main circuit board.
LED assembly
The two LEDs have been mounted together on their own assembly. The assembly has been built on a single piece of perfboard that includes the LEDs, the transistors used to drive the LEDs and various resistors. Screw terminals have been provided for connecting 5V, ground and the inputs for the two LEDs. The screw terminal connections are shown on the following image:
The 5V and ground terminals are connected to the main circuit board via a lead that terminates in a 2 pin JST PH 2.0 plug. The two LED terminals are connected to the relavant LED control pins of the microcontroller.
The circuit diagram for the LED assembly is:
And the assembly as built is:
Main circuit board
The main circuit board has 2 pin JST PH 2.4 sockets connecting to the following:
- 5V input from the USB micro power input board
- 5V supply to the LED assembly
- 5V supply to the LCD
- 5V supply to the microcontroller
- Connection to the gate sensor / test switch
- Connection to the buzzer isolation switch
There is also a 2 pin XH2.4 socket to connect to the buzzer and a 2 pin DuPont socket to connect the buzzer signal from the microcontroller and the gate alarm signal to the microcontroller.
The board has circuitry for the following:
- The buzzer driver. This circuit uses a MOSFET to switch the buzzer on and off in response to a signal from the microcontroller. There is also a 3.3V Zener diode to drop the 5V supply to the 3.3V required by the buzzer. The buzzer isolation switch is wired to interupt the buzzer signal from the microcontroller. The connection to the microcontroller is made via one of the DuPont sockets.
- The gate alarm. This simple circuit simply provides a pull up resistor for the gate alarm / test switches and connects the state of the switches to the microcontroller via one of the DuPont sockets.
The following image shows the front and back of the main circuit board as built:
The circuit board is to be mounted vertically in the case, attached to one hole in the LCD display via an angle bracket. The bracket is attached to, and insulated from, the circuit board by means of M2.5 nylon nuts, screws and spacers. The following image shows the circuit board with the bracket attached.
The piece of black electrical tape is used to prevent the electrolytic capacitor from shorting out against the bracket.
Microcontroller
The ultimate plan is to use a bare ATMega286P microcontroller chip to manage the system. However problems flashing code onto such chips mean that, at least in the interim, an Arduino Nano is to be used. This has been mounted on an expansion board that breaks the Nano's pins out to screw terminals. This should enable easy removal for flashing code and possible eventual replacement by the bare chip.
Software
The control software was developed using an Arduino Uno and the control circuit was mocked up on breadboards for testing.
Software is not yet finalised, but when v1 is completed it will be uploaded to the cahamo/gate-alarm
GitHub repository.