MCP23017 Door and Window Alarm on Raspberry Pi 5 - Proteus Simulation: the Raspberry Pi 5, the MCP23017, four MC-38 contacts and the siren

MCP23017 Door and Window Alarm on Raspberry Pi 5 - Proteus Simulation

40
700
60
25
60
PCBWay

Hello friends, I hope you all are doing great. Today, I am going to share the MCP23017 Door and Window Alarm on Raspberry Pi 5 - Proteus Simulation, the ninety-eighth project of our IoT Projects series. If you have searched for a Raspberry Pi burglar alarm, how to use an MCP23017 with a Raspberry Pi, MCP23017 interrupts in Python, or how magnetic door contacts work, here is a complete one in a Proteus simulation. A wired alarm panel is a surprisingly good lesson in embedded design: inputs that must never be missed, a state machine with delays, and a failure mode that has to be the safe one.

This project turns a Raspberry Pi 5 into an alarm panel for a small house. Four MC-38 magnetic contacts guard the front door, the back door, the kitchen window and the bedroom window. They are wired to an MCP23017 16-bit I/O expander, so all four zones cost the Pi only its two I2C wires and one interrupt line, and twelve pins are left for more. The MCP23017 raises its interrupt the moment a contact changes; the Pi reads which pin changed and what the port looked like at that instant. The panel chimes when a door opens while it is disarmed, refuses to arm with a window open, gives you an exit delay to leave and an entry delay to come home, and sounds the siren for anything else. A dashboard arms and disarms it from a laptop or a phone. In this tutorial, we will go through the circuit, how a reed contact works, the MCP23017's registers and its interrupt-on-change, the alarm's state machine and the code. Then we leave a window open, come home through the front door, and catch a burglar at the back door in the simulation. At the end, you can download the project with the program and every library file.

NOTICE: Our team has put a lot of effort into this project: the circuit, the code and every part in the simulation are our own work, and you get them all free. Your feedback is the fuel that keeps us going, so please tell us what you think in the comments below. And if our free projects help you, you can buy us a coffee. So, let's get started with the MCP23017 door and window alarm on Raspberry Pi 5 in Proteus simulation:

MCP23017 Door and Window Alarm on Raspberry Pi 5 - Proteus Simulation: the Raspberry Pi 5, the MCP23017, four MC-38 contacts and the siren
Figure: The door and window alarm in a Proteus simulation.
Raspberry Pi 5 door and window alarm Proteus simulation: a window opened, armed, the entry delay and the alarm at the back door
Figure: The project working in the Proteus simulation (an animation of the main steps).

What Does This Door and Window Alarm Do?

  • Four zones: two doors and two windows, each with an MC-38 magnetic contact.
  • One interrupt line: the MCP23017 tells the Pi the moment any contact changes, and which one.
  • Debounced: a change counts only after the port has been steady for 30 ms.
  • A real panel's logic: a chime when disarmed, no arming with a zone open, a 6 s exit delay and a 6 s entry delay.
  • The alarm: a siren for 15 s, a red LED until someone disarms, and the zone that tripped it remembered.
  • A dashboard: ARM and DISARM, every zone, its openings and the alarm memory.

In the simulation, the MC-38's pop-up opens and shuts each door, and the MCP23017's pop-up shows every pin, every register and every interrupt, so you can watch exactly what the Pi reads.

Components Used in the Proteus Simulation

All the parts come from our Proteus libraries, each with its own tutorial. The project zip at the end includes every LIB and DLL file.

The parts of the door and window alarm and their library posts
PartProteus deviceLibrary postConnection
Raspberry Pi 5RASPBERRY PI 5Raspberry Pi 5 Library for ProteusPI1, alarm.py, Ethernet 192.168.1.51
MCP23017 I/O expanderIOEXPMCP23017TEPMCP23017 IO Expander Library for ProteusU1 at 0x20: I2C1, INTA to GPIO17
MC-38 door contact (x 4)DOORMC38TEPMC-38 Door Sensor Library for ProteusU2 - U5 on GPA0 - GPA3
5 mm LEDs (green, red)LED5MMGREENTEP, LED5MMREDTEP5mm LED Library for ProteusD1 ARMED on GPB0, D2 ALARM on GPB1
Resistors 330 ohmRESISTOR4BANDTEPResistor Library for ProteusR1, R2 in series with the LEDs
Active buzzerBUZZERACTIVETEPActive Buzzer Library for ProteusBZ1, the siren, on GPB2
Laptop WiFi MonitorLAPTOP WIFI MONITORLaptop WiFi MonitorPC1
Mobile WiFi MonitorMOBILE WIFI MONITORMobile WiFi MonitorMOB1

Proteus Simulation of the Alarm: the Circuit

Door and window alarm Proteus simulation circuit: a Raspberry Pi 5, an MCP23017 and four MC-38 contacts
Figure: The complete circuit of the alarm on an A3 Proteus sheet.
The connections of the door and window alarm
SignalRaspberry Pi 5MCP23017Goes to
SDA / SCLGPIO2 / GPIO3 (pins 3 / 5)SDA / SCLthe I2C bus (the board has the pull-ups)
INTGPIO17 (pin 11)INTALOW when a zone changes
Z1 - Z4-GPA0 - GPA3the contacts: FRONT DOOR, BACK DOOR, KITCHEN WINDOW, BEDROOM WINDOW (the other wire to GND)
ARM / ALM-GPB0 / GPB1the green and the red LED, each through 330 ohm to GND
SIREN-GPB2the active buzzer

Each contact has just two wires: one to its expander pin, one to ground. There are no resistors at the contacts, because the MCP23017 has a 100 k pull-up inside every pin and the program turns them on. The address pins A0 - A2 are left open, so the expander answers at 0x20.

How the Door and Window Alarm Works

A Magnetic Contact

An MC-38 is two plastic blocks: a switch on the door frame and a magnet on the door. Inside the switch is a reed switch, two thin steel blades in a glass tube. The magnet's field pulls the blades together, so with the door shut the contact is closed; open the door and the blades spring apart. The contact closes when the gap drops under about 15 mm and opens above about 20 mm, with the state held in between, so a door rattling in its frame does not chatter. Wired from the pin to ground with a pull-up, a shut door reads LOW and an open door HIGH. And a cut wire also reads HIGH, an open door: for an alarm, that is exactly the right way to fail.

Sixteen Pins on Two Wires

The MCP23017 is an I2C chip with two 8-bit ports, A and B, and 22 registers. The program writes a few of them once: IODIRA = 0xFF makes port A inputs, GPPUA turns on the pull-ups, IODIRB = 0x00 makes port B outputs. After that the Pi reads the doors in GPIOA and drives the LEDs and the siren by writing OLATB. Eight more expanders could share the same two wires, at 0x20 - 0x27.

Interrupt-on-Change

Polling four doors over I2C would work, but an alarm should not depend on how often it asks. The MCP23017 can watch the pins itself. GPINTENA = 0x0F enables the interrupt on GPA0 - GPA3, and INTCONA = 0 means any change counts. When a contact changes, the chip pulls INTA LOW, notes the pin in INTF and freezes the whole port at that moment in INTCAP. The Pi sees INTA fall on GPIO17, reads INTF and INTCAP, then reads GPIO, and that read releases INTA. Reed contacts bounce as they close, so one closing can raise several interrupts within a few milliseconds; the program waits until the port has been steady for 30 ms before it believes a change.

The Alarm's State Machine

The panel is always in one of five modes. DISARMED: an opening only chimes. ARM is refused while any zone is open, otherwise the panel goes to EXIT, a 6 s exit delay with a beep every second. Then ARMED: the front door, the way you come home, starts ENTRY, a 6 s entry delay with fast beeps, and DISARM in that time stops it; any other zone goes straight to ALARM. In ALARM the siren sounds for 15 s, which in a real panel would be several minutes as local rules allow, and the red LED stays on and then blinks until someone disarms. The zone that tripped the alarm is kept as the alarm memory, so you know which door it was when you come home.

The Python Code Explained

The program is alarm.py, written for the Raspberry Pi 5 with lgpio for I2C and the GPIO, and http.server for the dashboard. The registers and the zones:

IODIRA, IODIRB, GPINTENA, INTCONA, IOCON, GPPUA = 0x00, 0x01, 0x04, 0x08, 0x0A, 0x0C    # (IOCON.BANK = 0: ports A / B interleaved)
ZONES = ['FRONT DOOR', 'BACK DOOR', 'KITCHEN WINDOW', 'BEDROOM WINDOW']

The set-up: the inputs with their pull-ups, the interrupt on the four zones, the outputs:

    wr(GPPUA, 0xFF)                                               # the 100 k pull-ups (the unused GPA4 - GPA7 do not float)
    wr(INTCONA, 0x00)                                             # interrupt on any change (not against DEFVAL)
    wr(GPINTENA, MASK)                                            # interrupts on the four zones

INTA on GPIO17, with an lgpio callback for its falling edge:

    lgpio.gpio_claim_alert(chip, INT_PIN, lgpio.FALLING_EDGE)
    lgpio.callback(chip, INT_PIN, lgpio.FALLING_EDGE, on_int)

Serving the interrupt: which pin, the port at that moment, then the read that releases INTA:

    intf, cap = rd(INTFA), rd(INTCAPA)
    port = rd(GPIOA)

The debounce: every interrupt starts the 30 ms again, and only the steady port counts:

            settle_at = time.monotonic() + SETTLE_S               # (every bounce starts the 30 ms again)
                zone_change(port & ~old, old & ~port)

The state machine: no arming with a zone open, the entry delay for the front door, the alarm for the rest:

        return event('ARM refused: %s open' % names(st))
        elif m == 'ARMED' and k == ENTRY_ZONE:
        elif m in ('ARMED', 'ENTRY'):

The outputs are written to OLATB only when they change:

    if v != last:

The dashboard's links are Refresh, ARM and DISARM; /data gives the mode, the zones, the openings and the alarm memory as JSON.

How to Run the Proteus Simulation

  1. Download the project zip below and install the library files and the TEPPi folder (the steps are under the download button).
  2. Open Pi-5-Door-Window-Alarm.pdsprj from the Proteus Simulation folder. Keep alarm.py and the DLL files next to it.
  3. Press Play. Click a contact (U2 - U5) for its pop-up and press Open 50 mm: the chime.
  4. Open the Laptop WiFi Monitor (press Go), press ARM, wait for the exit delay and open a door.
  5. Click U1 for the expander's pop-up: the pins, INTF, INTCAP and the interrupts.

Simulation Results: a Day of the Alarm in Proteus

We pressed Play and lived a short day with the alarm: a window left open, leaving the house, coming home through the front door, and a burglar at the back door. The doors were opened and shut with the MC-38 pop-ups' chips; the MCP23017's pop-up shows what the Pi reads from it. The times are the program's clock; the whole run took 74.4 simulated seconds.

Ready: All Zones Shut

The program reported READY at 0.00 s: 4 zones on the MCP23017 at 0x20, all shut; DISARMED. The expander's pop-up shows the set-up: port A all inputs (IODIR 0xFF) with the pull-ups on (GPPU 0xFF) and the interrupt on GPA0 - GPA3 (GPINTEN 0x0F); port B all outputs, all LOW. Port A reads 0xF0: the four zones LOW, held there by the shut contacts, and the four unused pins HIGH on their pull-ups.

Door and window alarm Proteus simulation ready: four MC-38 contacts shut, disarmed
Figure: Ready: every door and window shut.
MCP23017 pop-up in the alarm simulation: port A 0xF0, GPINTEN 0x0F, GPPU 0xFF, port B outputs LOW
Figure: The expander after the set-up.
MC-38 pop-up in the alarm simulation: the front door shut, the gap 0 mm, the reed closed
Figure: A contact: shut, the reed closed.
Laptop WiFi Monitor showing the alarm dashboard in the simulation: DISARMED, all zones shut
Figure: The dashboard.

A Window Left Open

We opened the kitchen window 50 mm. The MCP23017 pulled INTA LOW and the Pi read INTF 0x04, the bit of GPA2, and INTCAP 0xF4, the port at that moment, at 32.32 s; 30 ms later the port was still the same and the program reported KITCHEN WINDOW opened - chime. On the sheet, the contact's LED turned red. Then we pressed ARM on the dashboard, and the panel answered ARM refused: KITCHEN WINDOW open, just as a real panel will not arm with a window open.

Door and window alarm Proteus simulation: the kitchen window open, its contact red, the chime
Figure: The kitchen window open.
MC-38 pop-up in the alarm simulation: the kitchen window 50 mm open, the reed open
Figure: The window 50 mm open: the reed opens.
MCP23017 pop-up in the alarm simulation: GPA2 HIGH, port A 0xF4, INTCAP 0xF4, one interrupt
Figure: The expander: GPA2 HIGH, captured in INTCAP.
Laptop WiFi Monitor in the alarm simulation: ARM refused, the kitchen window open
Figure: ARM refused.

Leaving the House

We shut the window. The closing raised two interrupts in the same millisecond, INTCAP 0xF0 and then 0xF4: the reed bounced as it closed. The 30 ms debounce turned them into one change, KITCHEN WINDOW shut. ARM now worked: the exit delay, 6 s with a short beep every second and the green LED blinking, then at 40.98 s ARMED: all zones shut. The expander shows GPB0 HIGH, the green LED on.

Laptop WiFi Monitor in the alarm simulation: ARM accepted, EXIT 6 s
Figure: The exit delay.
Door and window alarm Proteus simulation armed: the green ARMED LED on, all zones shut
Figure: Armed.
MCP23017 pop-up in the alarm simulation: GPB0 HIGH, the ARMED LED, OLATB 0x01
Figure: The expander: GPB0 HIGH.
Mobile WiFi Monitor showing the alarm dashboard in the simulation: ARMED
Figure: The phone: armed.

Coming Home: the Entry Delay

Then we came home through the front door, opening it wide. The program reported FRONT DOOR opened: ENTRY DELAY 6 s - disarm now and beeped fast; the green LED went out. We pressed DISARM on the phone and the panel answered DISARMED from the web 1.3 s into the entry delay: no alarm, as it should be for the person who lives there.

Door and window alarm Proteus simulation: the front door open, the entry delay running
Figure: The front door: the entry delay.
MC-38 pop-up in the alarm simulation: the front door wide open, the gap 120 mm
Figure: The front door wide open.
MCP23017 pop-up in the alarm simulation: GPA0 HIGH, port A 0xF1, the ARMED LED off
Figure: The expander: GPA0 HIGH.
Mobile WiFi Monitor in the alarm simulation: disarmed 1.3 s into the entry delay
Figure: Disarmed from the phone in time.

A Burglar at the Back Door

We shut the front door, armed again and waited for ARMED at 52.98 s. Then the back door opened, and the back door has no entry delay: ALARM: BACK DOOR - the siren on at 54.02 s, 50 ms after its interrupt. Port B went to 0x06, the red LED and the siren; the buzzer's pop-up shows it SOUNDING at 2300 Hz. One detail from the pop-up matters for a real build: the buzzer drew 29.2 mA straight from GPB2, more than the 25 mA an MCP23017 pin is rated for. A real siren needs a transistor.

Door and window alarm Proteus simulation: ALARM, the back door open, the red LED and the siren on
Figure: ALARM: the back door.
Active buzzer pop-up in the alarm simulation: SOUNDING at 2300 Hz, 29.2 mA from the expander pin
Figure: The siren sounding.
MCP23017 pop-up in the alarm simulation: port A 0xF2, port B 0x06 - the ALARM LED and the siren
Figure: The expander: port B 0x06.
MC-38 pop-up in the alarm simulation: the back door 50 mm open, the reed open
Figure: The back door open.
Laptop WiFi Monitor in the alarm simulation: ALARM - BACK DOOR, the alarm memory
Figure: The dashboard: ALARM - BACK DOOR.

The Siren Stops, the Memory Stays

At 69.02 s, exactly 15 s later, the program reported the siren stopped after 15 s: port B 0x02, the red LED only, now blinking. The buzzer's pop-up shows it silent after 18.0 s of sounding in all, the chime and the delays' beeps included. Then we pressed DISARM on the laptop: DISARMED from the web: the siren off, the alarm memory: BACK DOOR. We shut the back door; it had been open 17.02 s.

Door and window alarm Proteus simulation after 15 s: the siren silent, the ALARM LED blinking
Figure: The siren stopped.
Active buzzer pop-up in the alarm simulation: SILENT after 18.0 s of sounding
Figure: The buzzer: silent.
MCP23017 pop-up in the alarm simulation: port B 0x02, the ALARM LED only
Figure: The expander: port B 0x02.
Laptop WiFi Monitor in the alarm simulation: DISARMED, the alarm memory BACK DOOR
Figure: Disarmed: the alarm memory.
Door and window alarm Proteus simulation at the end: disarmed, every zone shut again
Figure: The end: all shut.
MCP23017 pop-up in the alarm simulation: 12 interrupts, 108 writes and 34 reads
Figure: The expander's totals.
MC-38 pop-up in the alarm simulation: the back door shut again, its opening lasted 17.02 s
Figure: The back door shut.

In all, the expander raised 12 interrupts, every one of them served, with 108 writes and 34 reads on the bus. The phone shows the same dashboard, and the Pi's terminal has every event:

Mobile WiFi Monitor showing the alarm dashboard in the simulation after the alarm
Figure: The phone at the end.
Raspberry Pi 5 terminal in the alarm simulation: every interrupt, INTF and INTCAP, and every event
Figure: The program's log.

Download the Project Files

Click the button below to download MCP23017-Door-Window-Alarm-Raspberry-Pi-5-Proteus-Simulation-v1.0.zip (about 14.3 MB, without the source code of the models):

MCP23017 Door and Window Alarm on Raspberry Pi 5 - Proteus Simulation V1.0
  • Proteus Simulation: Pi-5-Door-Window-Alarm.pdsprj, alarm.py and copies of the models.
  • Python Code: alarm.py, the program.
  • Proteus Library Files: the LIB files of every part.
  • Proteus Model Files: the DLL models and the TEPPi folder (the Raspberry Pi runtime).
  • README.txt: the installation steps, the properties, the test results and the links to every library post.

No Proteus yet? Read How to Download and Install Proteus 7 and 8. New to libraries? Follow How to Install a New Library in Proteus.

How to Install the Project Files

  1. Close Proteus and extract the whole zip file, for example to your Desktop.
  2. Copy the files from Proteus Library Files into the LIBRARY folder of Proteus.
  3. Copy everything in Proteus Model Files, the DLL files and the TEPPi folder, into the MODELS folder.
  4. Start Proteus and open the project from the Proteus Simulation folder.

Changing the Code

alarm.py is plain Python; edit it in any editor and keep it next to the project. The zone names, the entry zone and the delays are near the top. On a real Raspberry Pi 5, install python3-lgpio and run it with python3 alarm.py.

Ideas to Extend the Project

  • Use the other four port A pins for four more zones, or a second MCP23017 at 0x21 for eight more.
  • Add a PIR motion sensor as an interior zone that is ignored in a STAY mode (armed at night, people inside).
  • Ask for a PIN code on the dashboard before DISARM, and log every arm and disarm with the time.
  • Send a message to your phone on an alarm, with the zone that tripped it.
  • Watch the contact wires for tamper with end-of-line resistors and an ADC: shut, open, cut and shorted all read differently.
  • Keep the siren's time and the alarm memory in a file so a restart does not forget them.

From Simulation to Real Hardware

  • The siren: a 12 V siren draws far more than an expander pin can give (25 mA); switch it with a transistor or a relay.
  • The contacts: mount the switch on the frame and the magnet on the door, aligned and within about 10 mm when shut; steel doors need contacts made for them.
  • The wires: run them as twisted pairs; on long runs add a 100 nF capacitor at the pin and keep the debounce.
  • Power: a real panel has a battery backup, so cutting the mains does not silence it.
  • The web: a dashboard that arms a house must be protected: a password, HTTPS, and never open to the internet.

Troubleshooting

  • Every zone reads open: the pull-ups are off, or the contacts' other wire is not on GND.
  • Nothing happens when a door opens: INTA must reach GPIO17 and GPINTENA must have the zone's bit.
  • An opening counts twice: no debounce; the reed bounces as it closes.
  • The LEDs stay dark: IODIRB must be 0 before OLATB is written.
  • No dashboard: the program serves on port 8000; open http://192.168.1.51:8000/.

Limitations of the Simulation

  • The MCP23017 and the MC-38 are our libraries' models, built from their datasheets and the parts' behaviour, not every detail of real parts.
  • The doors are the MC-38 pop-up's chips, not a house: a door opens as fast as the chip is pressed.
  • The siren time is 15 s so that the run stays short; a real panel sounds for minutes.
  • Raspberry Pi is a trademark of Raspberry Pi Ltd; this project is not made or endorsed by Raspberry Pi Ltd or by Labcenter.

So, that was all about the MCP23017 Door and Window Alarm on Raspberry Pi 5 - Proteus Simulation. I hope it helps you understand how magnetic contacts guard a house, how an MCP23017 gives a Raspberry Pi sixteen more pins with an interrupt that never misses a door, and how an alarm panel's delays fit together, all tested in a simulation before you drill a single door frame. Two more projects will complete this series of one hundred. If you have any questions, ask in the comments and I will help you out. Till the next tutorial, take care and have fun!


Comments

0

Join the conversation