Gate Alarm
Whys and Wherefores
Every morning, before letting our dog out in the back garden, my wife or I go outside to check that our garden gate is 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 gate.
This project aims to solve the problem by providing a sensor that sounds an alarm as soon as the gate is opened.
Requirements
The requirements for the alarm are:
- When the gate is opened an audible alarm should sound.
- No attempt is to be made to detect the gate being closed. This is entrusted to the humans! That's so we can be sure the gate has been properly secured and not just pushed closed without bolting it.
- There must be a way of cancelling the alarm once the gate has been confirmed to be closed.
- Since there are times when the gate needs to be left open, there should be a way of suspending, and then re-activating, the alarm.
- Should the gate be opened during a period of suspension then the alarm must be sounded after the suspension has ended, unless the gate has been confirmed to be closed.
- There should be a visual indication that the alarm is operational.
Overview
The project is split into two distinct components:
- Gate movement detector. This is attached to the gate and detects the gate being moved. It sends a signal to the control circuitry when this happens.
- Control unit. This responds to a signal from the gate detector by sounding an alarm if necessary. It also manages alarm suspension, information display and interaction with the user. The alarm buzzer is incorporated into the control unit.
The two components are described separately below.
Gate Movement Detector
Design
The gate movement detector uses normally open reed switches in association with a permanent magnet. There are two parts:
- A detector unit that is fastened to the gate post. This unit contains three glass reed switches wired in parallel. The use of three switches provides some redundancy in case one or more switches fail in the open position. The detector is wired to the control unit with two wires: one at five volts and the other normally at 0V.
- A magnet that is fastended to the gate.
The magnet and detector unit are mounted such that the magnet does not influence the reed switches when the gate is closed, meaning that the detector circuit remains open. The following drawing illustrates the design:
Opening the gate moves the magnet past the detector unit causing one or more reed switches to close. When any switch closes the ground wire is pulled up to 5V. The control unit's circuitry detects such changes and deems them to mean that the gate is open.
Note that this design can only detect that the gate has moved, not whether it has been opened or closed. The requirement to only detect an open gate makes this restriction is irrelevant. The advantage of the design is that the reed switches are normally left open which reduces current drain and may help with the longevity of the switches.
A downside of the design is that multiple pulses will be generated when the magnet passes each of the three reed switches in turn. Addition pulses may also be triggered if the gate blows around in the wind and moves the magnet past the reed switches again. This problem is to be addressed in software: only the first pulse after a reset will be used to indicate that the gate has opened with subsequent pulses being ignored until the alarm has been reset.
Construction
Detector unit
The detector unit was repurposed from an earlier experimental design. Construction is described and illustrated on the linked page.
Because the old design had four terminals protruding from the case (one for each reed switch and one for ground) the switch terminals had to be connected together outside the box to put them in parallel. The resulting two wires were taken to a pair of screw terminals. Another redundant plastic box was adapted to hold the new connectors, and was again sealed with hot glue. The following photo shows the completed unit, before fitting in place and adding a cover over the connectors:
The completed unit was screwed to the same small block of wood that was installed on the gate post to mount the earlier version of the unit. Wires from the control unit were connected to the detector unit's screw terminals and then a cover was hot glued in place over them. Hot glue was then used in an attempt to protect the whole unit from the weather. I was rather heavy handed when applying the hot glue, but it works! The final assembly is shown in the following photo:
Cabling
Two strands of cable from an old reel of "bell" wire was used to connect the detector unit to the control unit inside the house. The control unit end of the cable was connected to a DC power connector fitted with screw terminals. The detector end of the cable had the ends stripped, twisted and inserted into the detector's screw terminals. The two cables were joined together with pieces of heat shrink at intervals along its length.
The cable was fastened to the wall using cable clips nailed into external quality rawl plugs and was run into the house via a conveniently located (already broken) wall vent:
Magnet
An 8mm diameter neodymium magnet was fitted to the lid of an old plastic cocktail stick box lid. An 8mm hole was drilled in the side of the box lid and the magnet was push fitted. After mounting to the gate the magnet was secured with hot glue, as shown below:
Control unit
Circuit Design
The following input and output components are included in the control box:
- A 3.3V buzzer to sound the alarm.
- Red and yellow LEDs to indicate various alarm states.
- A LCD screen for more detailed information about the state of the alarm.
- A keypad used to control the alarm.
- A test switch to simulate a reed switch closing in the detector unit.
The electronic circuit that manages the above components is described by the following diagram:
The circuit is based around an Arduino Nano microcontroller. The Nano's pin connections are described in the following table:
Nano Pins | Type | Connects To | Notes |
---|---|---|---|
D2 | Input | Gate sensor / test switch | This pin is normally pulled high using a 20kΩ resistor. The pin is taken low when any of the gate movement detector reed switches or the test switch closes. |
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. |
D10 | Output | Buzzer circuit | Controls the MOSFET that activates the buzzer. The buzzer is on if and only if the pin is high. |
D11 | Output | Alarm LED | Controls the BJT that powers the red LED. The LED is on if and only if the pin is high. |
D12 | Output | Heartbeat / suspension LED | Controls the BJT that powers the yellow LED. The LED is on if and only if the pin is high. |
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 | |
GND | - | Ground connection |
Other aspects of the circuit to note are:
- A 680µF electrolytic capacitor connects 5V to ground to smooth out the power supply.
- 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.
- There are two jumpers that modify the operation of the circuit:
- Buzzer isolation jumper. Pulling this jumper breaks the connection to the MOSFET's gate and so silences the buzzer.
- Nano power jumper. Pulling this jumper disconnects the 5V supply to the Nano's 5V input pin. This must be done to enable the Nano to be powered safely via its USB socket when reprogramming.
Software
The microcontroller software was developed in Arduino flavoured C++ using various libraries. Development took place in VSCode using the PlatformIO extension. The software is open source. Details can be found in the cahamo/gate-alarm
repository on GitHub.
The software supports the following commands entered on the keypad:
Entry | Meaning |
---|---|
+ve number followed by # |
Suspends the alarm for the given number of minutes. |
0 followed by # |
Cancels any active suspension of the alarm. |
# on its own |
Suspends the alarm indefinitely. |
* on its own |
Resets any active or suspended alarm to indicate that the gate has been closed. |
The software controls the state of the buzzer, LEDs and LCD to communicate the current state of the alarm. The state of each component is described in the following table.
Gate state | Suspension state | Buzzer | Red LED | Yellow LED | LCD text | LCB backlight |
---|---|---|---|---|---|---|
MCU initialising | Silent | Off | Off | Welcome message | On | |
Closed | Not suspended | Silent | Off | Blinks every few seconds | "OK" | On for a few seconds |
Closed | Infinite | Silent | Off | On | "Alarm suspended" | On for a few seconds |
Closed | Timed | Silent | Off | On | Countdown to end of suspension | On |
Open | Not suspended | Sounding | Flashing | Blinks every few seconds | "Gate open" | On |
Open | Infinite | Silent | Flashing | On | "Alarm suspended" | On for a few seconds |
Open | Timed | Silent | Flashing | On | Countdown to end of suspension | On |
In addition to the above, when the user is entering a suspension timeout on the keypad the LCD displays a prompt and the number of minutes entered. The backlight stays on.
When a low signal is detected on Nano pin D2 the software latches into the gate open state. Subsequent low signals on D2 are ignored until the alarm is reset.
Case
A case for the control unit was designed in Inkscape and saved as an SVG file. Paper templates of the case sides, top and bottom were printed from Inkscape. The templates were used create cardboard mockups of the case to check the layout of components. This process was repeated until a suitable design was arrived at. The final design is illustrated in the following picture that shows a top view with an indication of the location of side mounted components. Black borders indicate cutouts, red borders indicate the footprint of some components on the top of the case. Blue writing describes the side-fitted components.
Construction
The control unit resides in our sitting room. It is located close the the wall vent where the wires connecting to the gate movement detector enter the house.
A modular approach to contructing the electronics was taken wherever possible, with each module being connected to others by means of JST PH 2.0 and XH2.4 plugs and sockets, screw terminals and DuPont cables. Details of the construction of the various modules follows.
Microcontroller
The microcontroller is an Arduino Nano. This has been mounted on an expansion board that breaks the Nano's pins out to screw terminals. This enables the Nano to be removed easily for updating or replacement. Connections can also be reconfigured much more simply than if they were soldered.
USB C power input
A panel mounted USB C socket had its 5V and GND pins soldered to leads terminated in a 2 pin JST PH 2.0 plug. The following photo shows the components before the leads were trimmed and soldered to the USB board:
The PH 2.0 plug is connected to the main circuit board from where the power is distributed to other components.
Input from gate sensor / Test switch
A momentary contact push button has been used to act as the alarm test switch. This was 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 both connected to the main circuit board via the PH 2.0 plug.
LCD
The display is a 16×2 blue backlit LCD 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 (VCC and GND) 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 via an extension cable with a PH 2.0 socket on one end and bare cable at the other.
Keypad
A 4×3 button membrane keypad is used. The membrane has 7 connections that are brought out in a ribbon terminated in a 7 way female DuPont connector.
Seven DuPont male to male leads were cut, leaving a male connector at one end and bare cable at the other. The ends with the remaining male DuPont connector was plugged into the keypad's 7 way female connectors. The cut ends had wire exposed, twisted and connected to the appropriate pins on the microcontroller via its expansion board's screw connectors.
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 a matching socket the main circuit board.
LED assembly
The 5mm red and 3mm yellow 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 assembly, with connecting wires, is shown in the following photo, along with an key to the screw terminals:
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 relevant LED control pins of the microcontroller via its expansion board's screw connectors.
Jumper switch board
This board is built on a scrap piece fibre perf board. It contains the buzzer isolation and Nano 5V jumpers. Each jumper is made from twin header pins connected to two pin screw terminals. The relevant cables are connected to the screw terminals. Jumper header pins are connected by means of yellow jumper caps.
Warning! No USB connection should ever be made to the Nano MCU without first removing the relevant jumper cap.
Main circuit board
The main circuit board has the following connections:
- A 2 pin JST PH 2.4 socket used for 5V power input and ground.
- 3 × 2 pin JST PH 2.4 sockets to distrubute 5V power and ground to other modules. The following modules are plugged into these sockets:
- the LED assembly
- the LCD
- the MCU (the 5V connection goes via the jumper).
- A 2 pin JST PH 2.4 socket to connect to the gate movement detector and test switch.
- A 2 pin XH2.4 socket to connect 3.3V and ground to the buzzer.
- A 2 pin JST PH 2.4 socket to connect to the buzzer isolation jumper.
- A 2 pin DuPont socket to connect the following:
- buzzer activation signals from the MCU
- gate movement detector signals to the MCU.
The board also houses components relating to the buzzer driver and gate movement detector along with the power supply smoothing capacitor.
The following image shows the front and back of the main circuit board as built:
The circuit board is attached to a modified steel bracket to enable it to be mounted vertically in the case. The bracket is attached to, and insulated from, the circuit board by means of M2.5 nylon nuts, screws and spacers. A piece of insulating tape is used to insulate the capacitor from the bracket. The following image shows the circuit board with the bracket attached.
Case
Paper templates for the top, bottom and all four sides of the case were printed directly from the design in Inkscape. These templates were flipped so that they could be adhered to what would become the insides of the case. The templates were glued to fibre board with a glue stick.
The fibre board was cut out with a hand saw using the templates as guides. Circular holes were drilled while rectangular apertures had corners drilled out before cutting with a fret saw. All apertures were finished with various files.
Cockup alert!. The fibre board had a tendency to rip. That combined with my poor DIY skills resulted in a rather untidy box. I wouldn't use this material again. This is a pity because this box lives in our sitting room.
The following photos show the inside and outside of the various case parts after cutting out:
The LCD was installed in its aperture and secured by nuts and bolts. One of these bolts was also used to fasten main circuit board's bracket in place. The keypad ribbon cable was pushed through a small slot in the top of the case and the keypad was then stuck to the top of the case using a glue stick. The two LEDs on the LED assembly were pushed into their pre-drilled holes and held in place by friction. The buzzer was pushed into its own hole and is also a friction fit. The following photo shows the top of the case after all these components were fitted.
The top and all sides were then glued together.
The test switch and sockets were fitted in place through the relevant mounting holes. A further hole was added later to allow a USB cable to be passed through the case side for connection to the microcontroller. See following montage of photos for details:
The microcontroller board and the jumper board were held loosely in place by self adhesive pads then all cable connections were made.
Cockup alert! The components held in by friction, glue stick and sticky pads were intended to be replaced by more permanent connections once the control unit had been thoroughly tested. I forgot to do this! From time to time the buzzer gets pushed inside the case and the internal components come loose.
A pair of old cupboard door magnets were fitted approximately centrally at the lower edge of the left and right sides of the case. The door magnets' companion steel plates were then glued in a matching postion on the case bottom. A small lug was cut from fibre board and attached to one edge of the base to aid alignment when closing the case. The case bottom was then fitted in place.
The following photo shows the inside of the main case structure, with electronics and magnetic fasteners installed:
The next photo shows inside of the case bottom with the lug and steel plates fitted, one of which is covered over.
The control unit was connected to power and to the gate movement detector. It was then tested successfully. The following two photos show the finished unit. The first photo shows a top view, with the alarm in suspended state and the gate closed. The second photo shows the unit stood on end, in un-suspended state and reporting that the gate is open.
Finally, here is a video of the control unit with the alarm sounding: