DIY Automation – The Garage – Part 2

The Garage Door Interface Electronics

For the garage door state detection I decided to go old school with a magnetic contact switch. I chose these for their simple implementation and ease in interfacing them to the RPI. Two per door will be used to detect when the door has reached its fully open and closed positions. The design includes the ability to actuate the existing garage door opener with a relay.

RPI interface to double door garage

P1 is the HAT header of the RPI. P2 & P3 are the door actuator relay outputs. P4-P7 are the door position switch inputs. When the magnet closes the switch on one of the inputs its corresponding RPI GPIO is pulled low to a logical 0. TOP corresponds to the top of the garage door and indicates that the door is in its full open position. Likewise BOT is the bottom of the door and indicates the door is in its full closed position.

The Input Circuit

Studying the input circuit surrounding P4, the diode D9 allows for the use of a door sensor that outputs voltage instead of a switch closure. If you use the magnetic switches it isn’t necessary. The resistor R9 act as a pull-up for the input to define a logical 1 since the magnetic switch does not provide a voltage. The LED D5 is strictly a visual indicator for the user to see when the input is being actuated. The resistor R5 is used to limit the amount of current that is sourced/sinked from the RPI’s gpio pin. It is primarily used as a buffer should the RPI gpio pin ever be accidentally put into output mode it won’t short against a closed external switch and damage the RPI.

The Actuation Circuit

Studying the output circuit surrounding P2, the relay K1 can be any general purpose 5 volt relay. The diode D3 is needed if the relay doesn’t come with one. It is used to short out the fly-back current produced by the relay coil when it is switched off. The fly-back current can at minimum cause the RPI to reset and at worst damage it. The resistor LED pair R3 & D1 are purely for visual indication that the relay is being actuated. Transistor Q1 is used to gate the relay since it cannot be driven directly from the RPI gpio without damaging the RPI. The transistor used can be any general purpose NPN or N channel FET. I used 2N3904 transistors because I had an ample supply of them in my personal inventory. The transistor needs to be sized to handle the current draw of the selected relay. In the case of a NPN type transistor the series resistor R1 is needed. In the case of an N channel FET, R1 would instead be attached to ground to bias the FET in the off state when the RPI gpio is an input. The RPI gpio’s are generally set to inputs whenever the RPI is reset. The last thing you want is the door inadvertently opening/closing in the case the RPI is power-cycled or reboot.

The Board So Far

Here we see the current board in all its glory. This is build with an original RPI Zero without the built in wifi, hence the TrendNet wifi adapter. Pay no attention to the ATTINY26. That was an experimental expansion that later proved to be unnecessary. I’ll discuss it more in an installment about the choice of temperature measurement and the radon abatement detection that was ultimately chosen but not yet added to the board.


The door interface is quite simple and easy to build. In the next part I’ll cover the code I wrote to turn the inputs into an MQTT message for the HAB system to digest.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.