Mission
Build this
Build a magnetic door-state sensor that publishes open or closed and increments one count for each confirmed opening.
Borrowed parts are labelled below and have a fallback. The registered device ID is fixed; sensing and behaviour decisions remain yours.
Bill of materials
Parts
| Source | Part | Fallback |
|---|---|---|
| ○ fablab | Reed switch and magnet | Use a kit push button to bench-test the complete state machine |
| ● kit | RGB LED and 220 Ω resistors ×3 | — |
| ● kit | SG90 servo | — |
Disconnect USB before rewiring. Motors, the relay, and the servo need appropriate power and a shared ground. Never drive an actuator from the ESP32 3V3 pin.
Backup assignment · your invitation
Notice the door, not the person
A tiny magnetic switch can tell a complete mechanical story: closed, beginning to open, open, and safely closed again. The Door Mouse watches that story while remaining unable to know who crossed the threshold.
Your creature will peek out when the greenhouse door opens, return home after a confirmed close, and add exactly one to its count for every completed opening. The challenge is not seeing an open door once. It is refusing to turn rattles, hesitation, and switch bounce into imaginary visits.
This device reports the door’s state and confirmed openings. It does not report people, entries, exits, or occupancy.
Meet the idea
A reed switch is a magnet-controlled button
Inside the glass capsule are thin metal reeds. A nearby magnet pulls them together. With the ESP32 input pull-up enabled, closed contacts usually read LOW and separated contacts read HIGH.
That electrical answer describes magnet distance, not door angle. Placement creates the meaning. Mark where the door becomes mechanically open enough to release the switch, then protect both pieces from shifting.
Real contacts can chatter between open and closed for a few milliseconds. Doors can also pause near the boundary. Your state machine decides which changes become events.
State
What the door appears to be now: confirmed closed, confirmed open, or still uncertain.
Transition
A change between confirmed states. One closed-to-open transition earns one count.
Debounce
A short demand for stable evidence that prevents one physical movement becoming several events.
- in the worldDoor movesThe magnet approaches or leaves the fixed switch.
- the partReed contactsThe magnetic field closes or releases the tiny metal reeds.
- electricalHIGH / LOW inputSeveral fast edges may occur around one physical transition.
- in the codeConfirmed state + countCode remembers the previous state and accepts one transition.
- on the spinestatus + countThe garden learns the door state and aggregate openings.
The count belongs after confirmation, not directly after a noisy electrical edge.
The lovely trick
A count is a story with memory
If code adds one whenever the input says open, an open door becomes hundreds of openings. If it adds one on every electrical edge, contact bounce becomes several. The device must remember what was already accepted.
A candidate state begins when the input changes. Only if that evidence remains stable for your chosen confirmation time does the official state change. Count only the confirmed closed-to-open transition.
- 01Confirmed closedMouse is home; input is stable near the magnet.then, input changes,
- 02Opening candidateThe switch released, but the timer is still checking.then, stable for ___ ms,
- 03Confirmed openIncrement once and let the mouse peek.then, input returns,
- 04Closing candidateThe magnet returned; wait before declaring closed.
Back to the start: Confirm the close, return the mouse home, and arm the next opening.
A hesitant door may move between candidates without creating an event until one state is confirmed.
If the door stops where the magnet chatters at the boundary, keep the last confirmed state or expose an uncertain status. Do not invent a clean transition just to make the display look decisive.
Wiring
Find the magnet’s useful distance before fixing anything
Bench-test the reed switch like a button. Watch the raw input while moving the magnet slowly from several angles. Only after you understand the sensing region should you design a mount and add light or movement.
The power rule: The reed switch is a dry contact and uses the ESP32 internal pull-up. Each RGB channel needs its 220 Ω resistor. Power the servo from a separate 5 V supply and join grounds.
Bench referenceOpen the exact wiring map
| ESP32 pin | Part | Part marking | Carries |
|---|---|---|---|
GPIO 25 | Reed switch | one lead | Digital in/out — configure INPUT_PULLUP |
GND | Reed switch | other lead | Ground — the nearby magnet closes the switch |
GPIO 26 | RGB LED | R via 220 Ω | Digital in/out — common-cathode reference |
GPIO 27 | RGB LED | G via 220 Ω | Digital in/out |
GPIO 14 | RGB LED | B via 220 Ω | Digital in/out |
GND | RGB LED | common cathode | Ground |
GPIO 18 | SG90 servo | signal (orange) | PWM to actuator — mouse peeking position |
external 5 V | SG90 servo | power (red) | 5V power — separate supply |
GND | SG90 servo | ground (brown) | Ground — join servo and ESP32 grounds |
Glass reed capsules are fragile. Cover the leads with strain relief, keep fingers away from door pinch points, and unplug USB before changing the mounted wiring.
- Digital in/out
- Ground
- PWM to actuator
- 5V power
This map assumes a normally open reed switch and common-cathode RGB LED. If the supplied parts differ, document the inverted logic; the state names must still describe the real door.
Give it character
Give a careful watcher a readable personality
The mouse may peek quickly, creep out in stages, or remain cautious during an uncertain transition. Its colour and posture should agree with the same stored door state.
Your team decides:
- At what physical opening should the switch release?
- How long must open or closed evidence persist before confirmation?
- What does the creature do when the door stops at the boundary?
- Does one count mean confirmed opening, complete open-close cycle, or another clearly documented event?
Write these decisions in plain language before turning them into code. A clear rule is easier to test, explain, and change.
Your field adventure
Can one frozen rule survive an awkward door rehearsal?
Write the movements before tuning the software. Include ordinary use and the irritating cases a friendly demonstration tends to avoid.
- Map the magnetic boundary.
Mark the switch and magnet positions, then measure where release and reconnection occur during slow movement.
- Write a labelled script.
Prepare at least thirty slow, fast, held, hesitant, partial, and lightly rattled movements with expected state and count.
- Tune once, then freeze.
Choose confirmation times from the first run. Do not change them during the final run.
- Repeat with the mounted device.
Report missed, extra, delayed, and uncertain events plus any shift caused by the real door hardware.
| Movement | Expected state | Expected +count | Observed state | Observed +count | Delay / note |
|---|---|---|---|---|---|
| ordinary open-close | closed | 1 | ___ | ___ | ___ ms |
| pause near boundary | team defines | ___ | ___ | ___ | ___ |
| light door rattle | closed | 0 | ___ | ___ | ___ |
When it gets dramatic
The mouse’s confusion points to mechanics or memory
One opening adds many counts
The code is counting a level or raw edges. Count only when the confirmed state changes from closed to open.
The state is backwards
Your switch or pull-up logic is inverted. Print raw input beside the real door state, then name the Boolean for meaning rather than voltage.
The door is closed but sometimes reads open
The magnet sits too near the release boundary or the mount flexes. Increase mechanical margin before increasing debounce time.
The mouse jitters at the threshold
The servo is following raw input. Let it follow only the confirmed stored state and give uncertain evidence time to resolve.
The count returns to zero after restart
Decide whether the project needs local persistence or a documented session count. Never disguise a reset as continuity.
Choose your direction
What kind of door investigator will you become?
The state-machine authors
Make hesitant and incomplete movements the centre of the project and draw every accepted transition.
The magnetic cartographers
Map distance, angle, and mounting tolerance to build the most forgiving physical installation.
The interval keepers
Add aggregate open-minutes and study how uncertain beginnings and endings affect duration.
Each direction is real engineering. Pick the question that keeps your team curious.
A calm way through the build
Collect four small wins
- dm-01 says hello with the switch on the bench.
Print raw state before mounting the magnet.
- One clean open and close produces two confirmed states.
Add timers without the servo.
- Exactly one count follows the confirmed opening.
Challenge it with a held-open and a bounce script.
- The mounted mouse peeks and returns once.
Add protected wiring, colour, and movement after the rule is frozen.
When a new step fails, return to the last small win. The fault is now somewhere in the few wires or lines you just added.
The garden handshake
Share what you found
These names are the rigid part of the project. They let another team find your work without knowing what you called the variables in your code.
Confirmed door state
garden/greenhouse-2/door-node/dm-01/statusUnit: enum
Confirmed opening count
garden/greenhouse-2/door-node/dm-01/countUnit: events
Listen beyond your own device.
garden/greenhouse-2/climate-node/ro-01/temperatureRomeo supplies greenhouse temperature. You may let warmth influence the mouse’s style or display, but it must not change what counts as a physical door transition.
Finish line
Ready to introduce to the garden
dm-01 stays online and publishes confirmed status plus one count per accepted opening.
The reject feed stays clear after the final code starts.
The device reads Romeo temperature without confusing climate with door state.
The light and mouse follow the confirmed state without blocking garden messages.
The build log contains the magnetic boundary, labelled movement script, frozen timing rule, misses, extras, uncertainty, and delay.
The fragile switch and wires are protected from strain and pinch points, and the device label can be scanned.
The Door Mouse is a good observer because it knows exactly what it witnessed: one door changing state, and nothing about the people beyond it.