Microchip Cat Flap with ESP32 and EM-18 RFID - Proteus Simulation: the ESP32, the EM-18 reader, the two IR beams, the two SG90 latches and the OLED

Microchip Cat Flap with ESP32 and EM-18 RFID - Proteus Simulation

40
700
60
25
60
PCBWay

Hello friends, I hope you all are doing great. Today, I am going to share the Microchip Cat Flap with ESP32 and EM-18 RFID in Proteus Simulation, the sixty-third project of our IoT Projects series. If you have searched for an ESP32 RFID project, a smart pet door, or a Proteus simulation of an EM-18 reader, here is a complete one. An ordinary cat flap lets in every cat in the street; this one opens only for its own cats. A 125 kHz RFID reader round the tunnel reads the tag on each cat's collar, two infrared beams tell whether the cat went in or out, two small servos are the latches, and the ESP32 keeps a record of who is at home. At night a curfew lets the cats come in but not go out, and a dashboard on a laptop or a phone shows it all.

Microchip cat flaps are a familiar sight in homes with cats, and they are a neat lesson in RFID, sensors and simple state machines. In this tutorial, we will go through the circuit, see how a 125 kHz reader and its tags work and what the EM-18 sends, check the tag's checksum, find the direction of a trip from the order of two beams, drive the two latches, add the OPEN, IN ONLY and LOCKED modes, walk through the code, and then run the simulation: a cat going out, a neighbour's cat refused, the cat coming back, a cat that changes its mind, and the night curfew. At the end, you can download the project with the sketch 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 microchip cat flap with ESP32 and EM-18 RFID in Proteus simulation:

Microchip Cat Flap with ESP32 and EM-18 RFID - Proteus Simulation: the ESP32, the EM-18 reader, the two IR beams, the two SG90 latches and the OLED
Figure: The cat flap in a Proteus simulation: MILO at the flap, the exit latch open.
Microchip Cat Flap with ESP32 and EM-18 RFID: the working Proteus simulation, an animated view of the project's main steps
Figure: The project working in the Proteus simulation (an animation of the main steps).

What Does This Cat Flap Do?

  • Knows its cats: an EM-18 125 kHz RFID reader reads the tag on each cat's collar; MILO and LUNA are registered, any other tag is refused.
  • Reads from both sides: the reader's coil goes round the tunnel, so a tag is read whichever way the cat comes.
  • Opens the right latch: two SG90 servos are the latches, one for coming in and one for going out; a registered tag opens the one the mode allows, for 15 seconds.
  • Tells in from out: two infrared beams across the tunnel, outside and inside; the one the cat breaks first gives its direction.
  • Knows who is at home: every trip flips that cat between IN and OUT and counts it.
  • Keeps them in at night: IN ONLY lets the cats come in but not go out, LOCKED shuts both ways.
  • Shows it all: a 0.91-inch OLED by the flap and a dashboard with the cats, the mode and the last events.

It is a great project for learning RFID readers, serial data with a checksum, digital sensors, servos and a little state machine, all on the ESP32. In the simulation, the EM-18's pop-up holds four tags you tap on the reader, each FC-51's pop-up moves an object into or out of its beam, and the servos' pop-ups show the latch angle and the pulse that set it.

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 microchip cat flap and their library posts
PartProteus deviceLibrary postConnection
ESP32 DevKit V1ESP32 DEVKIT V1ESP32 DevKit V1 Library for ProteusU1, ESP32_Cat_Flap.hex, TEP-WiFi 192.168.1.101
EM-18 RFID readerEM18ADVTEPEM-18 RFID Library for ProteusU2, TX to RX2 (GPIO16) at 9600 baud
2 x FC-51 IR beamFC51ADVTEPIR Obstacle Sensor (FC-51) LibraryU3 outside (D27), U4 inside (D14)
2 x SG90 servoSG90SERVOTEPSG90 Servo Motor LibrarySERVO1 entry latch (D13), SERVO2 exit latch (D12)
0.91" OLED 128x32OLED12832WHITEADVTEPOLED 128x32 Library for ProteusOLED1, I2C at 0x3C on D21 / D22
Laptop WiFi MonitorLAPTOP WIFI MONITORLaptop WiFi MonitorPC1
Mobile WiFi MonitorMOBILE WIFI MONITORMobile WiFi MonitorMOB1

Proteus Simulation of the Cat Flap: the Circuit

Microchip cat flap Proteus simulation circuit: ESP32 DevKit V1, EM-18 RFID reader, two FC-51 IR beams, two SG90 latches and OLED
Figure: The complete circuit of the cat flap on an A3 Proteus sheet.
The connections of the cat flap
PartPinsESP32 / supply
U2 EM-18TX / SEL / VCCRX2 (GPIO16) / +5V (the ASCII output) / +5V
U3 outside beam / U4 inside beamOUT / VCCD27 / D14; the 5 V rail
SERVO1 entry / SERVO2 exitSIG / V+D13 / D12; the board's VIN (5 V)
OLED1SDA / SCL / VCCD21 / D22 / +3.3V

The EM-18 uses the ESP32's second serial port, Serial2, so the first stays free for the serial monitor; with its SEL pin high it sends the tag as plain ASCII text. The FC-51 modules pull their output LOW when something is in front of them. Each SG90 takes a 50 Hz pulse: 544 us is 0 degrees, the latch locked, and 1472 us is 90 degrees, the latch open. The servos run from the board's 5 V pin, and the OLED from 3.3 V.

How the Cat Flap Works

125 kHz RFID: a Coil, a Tag and Its Number

A 125 kHz RFID tag has no battery. The reader's coil makes a magnetic field; the tag's own coil picks up enough energy from it to wake its chip, which then sends its fixed number back by loading and unloading the field. EM4100 tags, like the ones in collar tags, keyfobs and the microchips vets put under a pet's skin, carry a 40-bit number. Range is a few centimetres, which is exactly right for a cat flap: only the cat in the tunnel is read, not the one sitting next to it. The EM-18 module does all the radio work and simply sends the number when a tag arrives.

What the EM-18 Sends, and the Checksum

Each tag is sent once, as 12 ASCII characters at 9600 baud: the 10 hex digits of its 40-bit number and 2 hex digits of a checksum. The checksum is the five bytes of the number combined with XOR. Worked example for MILO's tag 5400A1B2C3: 0x54 XOR 0x00 = 0x54; XOR 0xA1 = 0xF5; XOR 0xB2 = 0x47; XOR 0xC3 = 0x84. So the reader sends 5400A1B2C384, and the sketch accepts it only if its own XOR gives the same 84. A noisy line that changes one bit changes the checksum, and the read is thrown away.

Which Way Did the Cat Go?

The tag says who is in the tunnel, but not which way the cat is going. Two infrared beams across the tunnel, one at the outside end and one at the inside end, answer that: a cat coming in breaks the outside beam first, then the inside one; a cat going out does the opposite. The sketch notes which beam was broken first after a tag opened the flap, and when the other beam follows within 8 seconds, it records the trip and the direction, flips the cat between IN and OUT, and locks the latch again. The beams are only read as a trip while a latch is open, so a stranger pushing its nose against the flap is noticed but never counted.

The Latch Servo: Pulse Width to Angle

A hobby servo such as the SG90 takes a pulse every 20 ms, 50 times a second, and turns its horn to an angle set by the width of that pulse. Its own electronics compare the pulse with the position of a small potentiometer on the output gear and drive the motor until the two agree, so the horn holds its angle against a push. The sketch tells the servo library the SG90's range: 544 us for 0 degrees and 2400 us for 180 degrees. Any angle in between is a straight line: for 90 degrees the pulse is 544 + (2400 - 544) x 90 / 180 = 544 + 928 = 1472 us, exactly the pulse the servo's pop-up measures when a latch opens. At 0 degrees the latch's arm stands across the flap and blocks it; at 90 degrees it lies along the frame and the flap swings freely.

The Trip as a State Machine

The whole flap is a small state machine, and writing it down makes the code easy to follow. It waits, with both latches locked, until a tag arrives. A registered tag that the mode allows moves it to the open state: one latch open, a 15-second timer running, no beam seen yet. The first beam broken moves it on and remembers which one it was. The other beam broken within 8 seconds completes the trip: the cat's place flips, its trip counter grows, both latches lock and the machine waits again. If the timer runs out first, the latches lock and nothing changes. Anything that happens while the machine is waiting, a stranger's tag or a nose at the outside beam, is logged but moves nothing. Because every path ends with the latches locked, the flap can never be left open by an odd sequence of events.

Two Latches and Three Modes

A real microchip flap has two locks, one for each direction, and so does this one. A registered tag opens the exit latch if the cat is at home and the entry latch if it is out, but only if the mode allows it. OPEN allows both; IN ONLY, the night curfew, allows coming in but not going out; LOCKED allows neither. If the cat changes its mind and walks away, the latch locks again after 15 seconds. An unknown tag never opens anything.

The Arduino Code Explained

The sketch is ESP32_Cat_Flap.ino. It uses ESP32Servo for the latches, Adafruit SSD1306 and GFX for the OLED, and the ESP32's WiFi and WebServer for the dashboard. The cats' tags and the timing come first:

const char *CAT_TAG[CATS] = {"5400A1B2C3", "5400D4E5F6"};           // the 10 hex digits of each collar tag (the reader's TAG 1 / TAG 2)
const unsigned long OPEN_MS = 15000, PASS_MS = 8000;                // a latch stays open 15 s (a cat may hesitate); a trip's two beams

The reader is on Serial2 at 9600 baud, and the latches get the SG90's pulse range:

  Serial2.begin(9600, SERIAL_8N1, 16, 17);
  entryLatch.attach(ENTRY_PIN, 544, 2400); exitLatch.attach(EXIT_PIN, 544, 2400);   // (the SG90's 0 - 180 deg pulses; the library's

The checksum: the five bytes of the number, XORed together:

      for (int i = 0; i < 10; i += 2) { char b[3] = {rx[i], rx[i + 1], 0}; x ^= (byte)strtol(b, nullptr, 16); }

A tag that is not one of the cats keeps the flap shut:

  if (c < 0) { logEvent(String("unknown tag ") + id + " - the flap stays shut", "STRANGER - SHUT"); return; }

The mode decides which ways are allowed, and the cat's place decides which latch opens:

  bool in = mode != LOCKED, out = mode == OPEN;                     // (IN ONLY: the curfew - in yes, out no)
  if (home[c]) exitLatch.write(OPEN_DEG); else entryLatch.write(OPEN_DEG);

The beams are LOW while blocked, and the first one broken gives the direction:

  bool outNow = digitalRead(OUT_BEAM) == LOW, inNow = digitalRead(IN_BEAM) == LOW;
  if (edge != firstBeam && millis() - firstAt < PASS_MS) {
    bool cameIn = firstBeam == 1;

A latch that stays open too long locks itself again:

  if (who >= 0 && millis() - openedAt > OPEN_MS) {

Every event has a full line for the serial monitor and the dashboard and a short one for the OLED, which fits 21 characters on a line. The dashboard's pages are / (the dashboard), /open, /inonly and /locked (the modes) and /data (the mode and each cat's place as JSON).

How to Run the Proteus Simulation

  1. Download the project zip below and install the library files (the steps are under the download button).
  2. Open ESP32-Cat-Flap.pdsprj from the Proteus Simulation folder. Keep ESP32_Cat_Flap.hex and the DLL files next to it.
  3. Press Play. The OLED shows OPEN, MILO IN, LUNA IN.
  4. On the EM-18's pop-up (U2) press Tap beside TAG 1: MILO is at the flap and the exit latch opens.
  5. On the inside beam's pop-up (U4) press 5 cm, then the same on the outside beam's (U3): MILO went out. Press 30 cm on both to clear them.
  6. Tap TAG 3, a stranger's tag: the flap stays shut. On the dashboard press IN ONLY for the night curfew.

Simulation Results: the Cat Flap in Proteus

Here is the run that made the screenshots of this tutorial, 62 seconds of simulation: MILO goes out, a neighbour's cat is refused, MILO comes back, LUNA hesitates at the flap, and the night curfew keeps LUNA in. The times are the sketch's own clock, as it printed them on the serial monitor; the parts' numbers come from their pop-ups.

Ready

At 2.3 s the sketch says "ready - MILO and LUNA at home, the flap OPEN". The OLED shows OPEN, MILO IN, LUNA IN and READY, and the dashboard on the laptop shows the same with no trips yet.

The ESP32 cat flap Proteus simulation ready: both cats at home, the flap OPEN
Figure: Ready: both cats at home.
OLED pop-up in the Proteus simulation: OPEN, MILO IN, LUNA IN, READY
Figure: The OLED: ready.
Laptop WiFi Monitor in the Proteus simulation: the cat flap dashboard, both cats in
Figure: The dashboard at the start.

MILO Goes Out

MILO's collar tag (TAG 1) passes the coil, and the EM-18's pop-up shows what it sent: 5400A1B2C3 with the checksum 84. At 8.8 s: "MILO at the flap - the exit latch open". The exit servo turns to 89.6 degrees on a 1472 us pulse. MILO breaks the inside beam, then the outside one (the FC-51's pop-up shows OUT LOW, an object at 5 cm), and at 13.2 s: "MILO went out (trip 1)". The latch locks again and the OLED shows MILO OUT.

The ESP32 cat flap Proteus simulation: MILO at the flap, the exit latch open
Figure: MILO at the flap.
EM-18 RFID pop-up in the Proteus simulation: TAG 1 read, 5400A1B2C3 with the checksum 84
Figure: The EM-18: MILO's tag.
SG90 pop-up in the Proteus simulation: the exit latch at 89.6 degrees, a 1472 us pulse
Figure: The exit latch open.
FC-51 pop-up in the Proteus simulation: the outside beam blocked at 5 cm, OUT LOW
Figure: The outside beam blocked.
The ESP32 cat flap Proteus simulation: MILO went out, the latch locked again
Figure: MILO went out.
OLED pop-up in the Proteus simulation: MILO OUT, LUNA IN, MILO WENT OUT
Figure: The OLED: MILO out.

A Stranger at the Flap

The neighbour's cat comes along with its own tag (TAG 3). At 20.0 s: "unknown tag 1A0077B8C9 - the flap stays shut". It pushes at the outside beam anyway, and at 20.7 s: "something at the outside beam - the flap is shut". No latch moves, and nothing is counted.

The ESP32 cat flap Proteus simulation: a stranger's tag, the flap stays shut
Figure: A stranger refused.
OLED pop-up in the Proteus simulation: OUTSIDE - FLAP SHUT
Figure: The OLED: the flap shut.

MILO Comes Back

MILO returns. The same tag opens the entry latch this time, at 26.2 s, because the sketch knows MILO is out. The outside beam breaks first, then the inside one, and at 28.6 s: "MILO came in (trip 2)". The dashboard shows MILO is IN with 2 trips.

SG90 pop-up in the Proteus simulation: the entry latch open at 89.6 degrees
Figure: The entry latch open.
The ESP32 cat flap Proteus simulation: MILO came back in
Figure: MILO came in.
Laptop dashboard in the Proteus simulation: MILO is IN after 2 trips, the last events
Figure: The dashboard: MILO back.

LUNA Hesitates

LUNA comes to the flap from inside at 35.7 s and the exit latch opens, but LUNA never goes through. Fifteen seconds later, at 50.7 s: "LUNA did not go through - the latch locked again". LUNA is still IN.

OLED pop-up in the Proteus simulation: LUNA DID NOT PASS
Figure: The OLED: LUNA stayed.

The Night Curfew

IN ONLY from the laptop at 52.4 s. When LUNA tries again at 54.0 s: "LUNA wants out - IN ONLY, the exit stays locked". The exit servo stays at 0.4 degrees, on a 544 us pulse. The phone and the laptop show the night's log.

Laptop dashboard in the Proteus simulation: IN ONLY, the night curfew on
Figure: The dashboard: IN ONLY.
The ESP32 cat flap Proteus simulation at night: LUNA refused by the curfew
Figure: The curfew: LUNA kept in.
OLED pop-up in the Proteus simulation: IN ONLY, LUNA KEPT IN
Figure: The OLED: the curfew.
SG90 pop-up in the Proteus simulation: the exit latch locked at 0.4 degrees, 544 us
Figure: The exit latch locked.
Mobile WiFi Monitor in the Proteus simulation: the cat flap dashboard at night
Figure: The phone dashboard.
Laptop dashboard in the Proteus simulation: IN ONLY, MILO and LUNA in, the last events
Figure: The laptop dashboard at the end.
ESP32 serial monitor in the Proteus simulation: every tag, every trip and the curfew
Figure: The serial monitor: the whole run.
The results of the cat flap simulation
WhatResult
MILO goes outtag 5400A1B2C3 (checksum 84), the exit latch at 89.6 degrees; trip 1 at 13.2 s
A strangertag 1A0077B8C9 refused; the outside beam ignored
MILO comes backthe entry latch open; trip 2 at 28.6 s
LUNA hesitatesthe latch locked again after 15 s
IN ONLYLUNA refused at 54.0 s; the exit latch stays at 0.4 degrees
Simulation62 s of simulation

Download the Project Files

Click the button below to download Microchip-Cat-Flap-ESP32-EM18-RFID-Proteus-Simulation-v1.0.zip (about 4.2 MB, without the source code of the models):

Microchip Cat Flap with ESP32 and EM-18 RFID - Proteus Simulation V1.0
  • Proteus Simulation: ESP32-Cat-Flap.pdsprj, ESP32_Cat_Flap.hex and copies of the models.
  • Arduino Code: ESP32_Cat_Flap.ino, the sketch.
  • Proteus Library Files: the LIB files of the ESP32 DevKit V1, the EM-18, the FC-51, the SG90, the OLED and the two WiFi Monitors.
  • Proteus Model Files: the DLL models.
  • 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 the files from Proteus Model Files into the MODELS folder.
  4. Start Proteus and open the project from the Proteus Simulation folder.

Changing the Code

Open ESP32_Cat_Flap.ino in the Arduino IDE with our TEP Boards for Proteus, choose ESP32 Dev Module (TEP Proteus), and export the compiled binary; U1's PROGRAM property names the .hex file it runs. Put your own cats' names and tag numbers in CAT_NAME and CAT_TAG. On a real ESP32 the same sketch runs with the standard ESP32 board package and the ESP32Servo, Adafruit SSD1306 and Adafruit GFX libraries from the Library Manager.

Ideas to Extend the Project

  • Switch to IN ONLY and back to OPEN by the time of day, or by a light sensor at dusk and dawn.
  • Send a notification when a cat goes out or comes in, and an alert when a stranger tries the flap.
  • Learn new tags from the dashboard: hold a new tag at the reader and give it a name.
  • Log every trip with its time and draw a daily chart of each cat's hours outside.
  • Use a reader for pet microchips (FDX-B at 134.2 kHz), so the cats need no collar at all.
  • Add a camera at the outside end and keep a picture of every stranger.

From Simulation to Real Hardware

  • Mind the logic levels: the EM-18 runs on 5 V and its TX is a 5 V signal; a divider of 1 kohm and 2 kohm brings it to the ESP32's 3.3 V.
  • Wind the coil round the tunnel: real flaps use a large antenna loop round the opening; the EM-18's small built-in coil reads only a few centimetres.
  • Power the servos properly: two servos moving at once draw short current peaks; give them their own 5 V supply with a capacitor, the grounds joined.
  • Weatherproof the beams: rain and sunlight upset infrared sensors; shade them inside the tunnel.
  • Fail safe: decide what happens on a power cut; most real flaps let the cats in but not out.

Troubleshooting

  • No tag is read: TX to RX2 (GPIO16), SEL high for the ASCII output, and the tag within its range.
  • A bad read (checksum): the 12 characters did not add up; the reader's Bad checksum test does this on purpose.
  • A trip is not recorded: both beams must be broken, in order, within 8 seconds, while a latch is open.
  • A beam says NO POWER: its VCC must be on the 5 V rail.
  • A latch does not move: SIG on D13 or D12 and V+ on 5 V.

Limitations of the Simulation

  • The cats are the EM-18's tags and the FC-51s' objects, set by hand on their pop-ups; a real cat may stop half way or turn back.
  • The flap itself is imagined: the servos turn, but there is no flap door to push.
  • Each trip assumes one cat in the tunnel at a time.
  • The network to the laptop and the phone is simulated inside Proteus.
  • ESP32 is a product of Espressif Systems; this project is not made or endorsed by them or by Labcenter.

So, that was all about the Microchip Cat Flap with ESP32 and EM-18 RFID in Proteus Simulation. I hope it helps you understand how a 125 kHz RFID reader and its tags work, why a checksum matters, how two beams give a direction, and how a small state machine with two latches and three modes keeps the right cats in and the wrong ones out, all tested in a simulation before you build a real flap. More IoT projects with the ESP32 and the Raspberry Pi 5 are coming in this series. 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