Skip to content
My Account

HomeKit

ESP8266 HomeKit Window Contact Sensor with a Hall Sensor

A HomeKit window contact sensor: an A3144 Hall sensor, a magnet, an ESP8266 and a 3D-printed housing that sticks to the frame. A test setup with the circuit explained from the datasheet.

  • ESP8266
  • HomeKit
  • 4 min read
No ratings yet, be the first.

A wireless window contact sensor for Apple HomeKit. A small Hall-effect sensor notices a magnet, an ESP8266 reports it to the Home app, and the whole sensor sits in a 3D-printed housing that sticks to the window frame with double-sided tape.

The same build exists in four versions for four uses: this one for a window, and one each for a general contact sensor, a door and blinds. The housing shown here is a design of the author, and the model is not published anywhere. This is a test setup.

The four versions

VersionUse
Window Contact SensorA window that is open or closed
Door Contact SensorA door that is open or closed
Blinds Contact SensorBlinds that are open or closed
Contact SensorAny other contact, such as a cupboard

The hardware is the same, and the versions differ in the application. In the Home app you choose how the sensor is displayed: as blinds, a contact sensor, a door, a garage door or a window. An older version with a reed contact is the ESP8266 HomeKit Reed Contact Sensor.

The Hall sensor

The sensor is an A3144, a unipolar Hall-effect switch. From its datasheet: it runs on 4.5 to 24 V and has an open-collector output that can sink up to 25 mA. The output, on pin 3, switches low when the magnetic field at the sensor rises above its operate point, and goes high again when the field falls below the release point. The difference between the two points, the hysteresis, keeps it from flickering. In the datasheet, pin 1 is the supply and pin 2 is ground. The schematic below is drawn with its own pin numbers, so go by the labels VCC, GND and VOUT.

So the sensor needs no moving parts: put a magnet on the moving part, the sensor on the fixed part, and the output tells you whether they are close together.

The circuit

The A3144 is powered from 5 V, but the ESP8266 input takes 3.3 V. The circuit deals with that with two resistors on the output: R1, 10 kΩ, to +5 V, and R2, 20 kΩ, to ground.

Circuit diagram: an A3144 Hall sensor powered from 5 volts, with a 10 kilohm resistor from the supply to the output and a 20 kilohm resistor from the output to ground

With no magnet near, the output is released, and the two resistors form a divider: 5 V times 20 kΩ divided by 30 kΩ is about 3.3 V, safe for the ESP8266. With a magnet near, the sensor pulls the output to ground. R1 also works as the pull-up that an open-collector output needs.

Hardware and wiring

ComponentNotes
NodeMCU (ESP8266) boardA development board with USB. See the ESP8266 pinout
A3144 Hall sensorSupply, ground and output
Resistors10 kΩ and 20 kΩ, as in the circuit
MagnetOn the moving part of the window
5 V USB powerThe board is powered over USB
Breadboard diagram: a NodeMCU board powered from 5 volts over USB, with an A3144 Hall sensor and two resistors, and a blue wire from the sensor output to pin D7

In the diagram, the sensor output goes to pin D7 of the NodeMCU, which is GPIO13. The pin is read from the diagram, since the repository holds no source code, so check it before you build.

The 3D-printed housing

The sensor and the magnet each get a small housing, and the sensor housing holds the electronics. The author printed a test version first, with the sensor inside.

The printed test housing parts on a wooden table, one with the sensor and a purple cable fitted

The housing is fixed with double-sided tape, and the gap between the two parts should be about 2 mm when the window is closed.

How to apply: two housings stuck with double-sided tape on a frame, with a 2 millimetre gap between them

The model of the housing is a design of the author and is not published, so there is no download.

Flash and install

The installation is the same as for the other ESP8266 HomeKit accessories on this site. The firmware runs on ESP-Open-RTOS with Life Cycle Manager 2, which sets up the WiFi and installs updates over the air. The full steps, from installing esptool to flashing, joining the LCM- WiFi network and pairing, are in ESP8266 HomeKit Blinds.

In the portal, use these values:

FieldValue
OTA repositoryAchimPieters/ESP8266-HomeKit-Window-Contact-Sensor
OTA binary filemain.bin

The portal also has a setting for the LED pin. For a high output, use the active-high option, “If active on 1”.

The Life Cycle Manager 2 portal with a Join button, an LED pin setting, and fields for the OTA repository and the OTA binary file

When the installation is done, scan the QR code the device generates in the Home app to add it.

In the Home app

The sensor first shows up as a plain sensor. After adding it, the Home app asks how to display it: as blinds, a contact sensor, a door, a garage door or a window. That choice decides the icon and the words the Home app uses.

Two Home app screenshots: adding the sensor to the home, and the Display As list with blinds, contact sensor, door, garage door and window

The screenshots show the Home app with an English interface.

Firmware releases

VersionDate
0.0.326 April 2021
0.0.226 April 2021
0.0.124 April 2021

Each release has a main.bin and a signature file, main.bin.sig.

Good to know

  • This is a test setup and not a finished product.
  • The HomeKit Accessory Protocol specification the firmware follows is the non-commercial version, for accessories you build yourself and do not distribute or sell.
  • The repository is under the MIT licence and holds the firmware release, not the source code.

Everything is in the ESP8266-HomeKit-Window-Contact-Sensor repository on GitHub.

Advertisement
All HomeKit

More HomeKit

Keep building.

Switches, sensors, plugs and blinds: more accessories that you build yourself and add to the Home app.

All HomeKit