Key takeaway: IC1 should hold ~3.3 V for light loads deep into AA discharge, but high-current WiFi bursts become risky below ~0.9–1.0 V because the boost converter can hit current limit and VOUT collapses.
Keep in mind that in code I set wifi power to lowest level WIFI_POWER_8_5dBm. Wifi Connect last about 2500ms in average. also do not forget about C2, C4 and C5
Now update the graph
Updated the graph/document with WIFI_POWER_8_5dBm, the ~2500 ms WiFi connect duration, and C2/C4/C5 energy contribution: TPS613221A VOUT vs AA Battery Voltage.
Key change: the relevant low-power WiFi curves are now 75–100 mA; C2/C4 help only with short transients (~0.065 ms at 100 mA), so the 2.5 s connect window is dominated by AA cell loaded voltage, not capacitor storage.
Firmware Technical Requirements — WiFi Door and Window Sensor
1. Project overview
This firmware is for the WiFi Door and Window Sensor PCB. The product is a compact battery-powered smart-home door/window contact sensor built around an ESP32-C3-MINI-1-N4 module, a single AA primary cell, a TPS613221A 3.3 V boost regulator, a magnetic reed switch, a user button, BOOT/EN controls, a programming header, and a common-anode RGB status LED.
Primary firmware goals:
Detect door/window open and closed state changes using the reed switch.
Report events reliably over a low-power wireless path.
Spend nearly all time in deep sleep to maximize AA battery life.
Provide simple bring-up/setup feedback through serial logs and the RGB LED.
Support production flashing/debug through the existing UART/programming header.
The hardware can support both WiFi and Bluetooth LE, but BLE event reporting is strongly preferred for battery life when a nearby BLE hub is available.
2. Target hardware
Table
Item
Value
MCU module
ESP32-C3-MINI-1-N4
Vendor
Espressif Systems
Main supply rail
3.3 V, net VOUT
Power source
Single non-rechargeable AA cell
Boost regulator
TPS613221ADBVR, 3.3 V output
Magnetic sensor
Littelfuse 59170-1-S-00-D reed/magnetic sensor
RGB LED
Everlight 19-237/R6GHBHC-A04/2T / LTST-C19HE1WT style common-anode RGB LED
Buttons
PTS815 tactile switches for USER, BOOT, EN/reset
Programming header
J1, 1x6 2.54 mm vertical header
3. Firmware build environment requirements
3.1 Arduino IDE target
Recommended Arduino environment:
Arduino IDE: 2.3.x or newer
Board package: esp32 by Espressif Systems 2.0.17 or newer
Board selection: ESP32C3 Dev Module or ESP32-C3 DevKitM-1 equivalent
Serial monitor: 115200 baud
Upload speed: start with 460800, fall back to 115200 if upload is unreliable
Flash mode: DIO
USB CDC on boot: enable if using native USB serial
3.2 Library requirements
Default BLE firmware:
Table
Library
Version
Notes
Arduino-ESP32 core
2.0.17+ or 3.0.7+
Code must account for LEDC API differences between 2.x and 3.x if supporting both.
NimBLE-Arduino by h2zero
2.2.1+
Use for low-power BLE advertising/reporting.
No external WiFi, HTTP, Preferences, or ArduinoJson library is required for the default BLE event-advertising build.
ESP1:IO0 → R6 50 Ω → REED1:2, with R2 1 MΩ pull-up to 3.3 V and C1 debounce/filter to GND
GPIO0
Active LOW when magnet/reed closes to GND
Configure as input. Do not enable strong internal pull unless needed for diagnostics. Treat LOW as closed = true.
User button
ESP1:IO1 → USER switch, C6 debounce/filter to GND
GPIO1
Active LOW
Configure as input. Treat LOW as pressed. Use for setup/test mode.
RGB LED red
ESP1:IO4 → R3 130 Ω → LED1:R
GPIO4
Common-anode LED, active LOW sink drive
Use PWM-capable output. LOW/on, HIGH/off. Keep off in normal sleep/idle.
RGB LED green
ESP1:IO5 → R4 50 Ω → LED1:G
GPIO5
Common-anode LED, active LOW sink drive
Use PWM-capable output. LOW/on, HIGH/off. Keep off in normal sleep/idle.
RGB LED blue
ESP1:IO6 → R5 50 Ω → LED1:B
GPIO6
Common-anode LED, active LOW sink drive
Use PWM-capable output. LOW/on, HIGH/off. Keep off in normal sleep/idle.
GPIO2 strap
ESP1:IO2 → R7 10 kΩ pull-up to 3.3 V
GPIO2
Boot strap/pull-up only
Do not use as runtime output. Avoid driving this pin.
BOOT button
ESP1:IO9 → BOOT switch to GND + R8 10 kΩ pull-up to 3.3 V + J1 Pin 4
GPIO9
Active LOW boot/download strap
Leave as boot/programming function. Runtime use only if necessary and with strap-state caution.
EN/reset
ESP1:EN → EN switch to GND + R1 10 kΩ pull-up + C5 filter + J1 Pin 5
EN
Active LOW hardware reset
Firmware cannot read this as GPIO; use for reset/programming only.
UART TX
ESP1:TXD0 → J1 Pin 2
GPIO21 / TXD0
Serial output/programming
Use for boot logs and optional debug.
UART RX
ESP1:RXD0 → J1 Pin 3
GPIO20 / RXD0
Serial input/programming
Use for flashing/debug only unless required.
3.3 V
ESP1:3V3 → VOUT
3V3
Boost output
No firmware control. Brownout behavior must be tested under RF transmit.
GND
ESP1:GND pins → GND
GND
Common ground
N/A
Unused or currently unassigned ESP32-C3 pins visible in schematic: GPIO3, GPIO7, GPIO8, GPIO10, GPIO18, GPIO19. Before using any of these, verify ESP32-C3 boot strap/JTAG/USB/default peripheral interactions in the Espressif datasheet and current board routing.
5. Electrical behavior and polarity
5.1 Reed switch
Reed input is connected to GPIO0 through a 50 Ω series resistor.
The 1 MΩ pull-up reduces idle current but makes the input higher impedance than a typical digital input. Firmware should avoid enabling internal pulldowns or strong pulls during normal operation. Sampling should allow a short settle/debounce time after wake.
5.2 User button
User button is active LOW.
The button should be used for setup mode, test mode, pairing/provisioning, or factory reset gestures.
Because deep sleep wake is level-based and the reed wake polarity changes, the user button may not be simultaneously wake-armed in all reed states without a more advanced wake strategy.
5.3 RGB LED
LED is common-anode tied to 3.3 V (VOUT).
GPIO pins sink current through resistors.
Logic:
GPIO LOW → LED channel ON.
GPIO HIGH → LED channel OFF.
LED must remain off except during boot, setup, pairing, error indication, and manual test because LED current dominates sleep current.
Recommended status colors:
Table
State
LED behavior
Boot
Short dim white or blue flash
Open event
Short green flash
Closed event
Short red flash
User/setup mode
Blue or RGB cycling
Wireless transmit failure
Short amber/red blink pattern
Low battery, if battery measurement is later added
Red repeated blink during manual wake only
6. Power and battery constraints
6.1 Battery and regulator
Battery is a single AA primary cell.
The boost regulator is TPS613221ADBVR with 3.3 V output.
TPS613221A quiescent current into VOUT is approximately 6.5 µA typical, 10 µA max.
ESP32-C3 deep sleep with RTC timer + RTC memory is approximately 5 µA typical.
Expected base sleep current estimate: about 11.5 µA before reed pull-up conduction.
If the reed is closed and R2 is pulled to GND, the 1 MΩ pull-up adds approximately 3.3 µA.
6.2 RF burst current risk
WiFi TX current can be high for a single AA + boost design:
Table
ESP32-C3 WiFi case
Approx current at 3.3 V
802.11n HT20 MCS7 @ 17.5 dBm
290 mA
802.11g 54 Mbps @ 18 dBm
295 mA
802.11b 1 Mbps @ 20.5 dBm
350 mA
Approximate AA input current during WiFi TX through the boost converter can reach:
Table
Battery voltage
Low/mid WiFi TX
High WiFi TX
1.5 V
~0.8–1.1 A
~1.1 A
1.2 V
~1.0–1.4 A
~1.4 A
0.9 V
~1.3–1.8 A
~1.8 A
TPS613221A switch current limit is approximately 0.75 A minimum, 1.20 A typical, 1.60 A maximum, so high-power WiFi at low AA voltage may brown out or fail. Firmware must minimize RF-on time and avoid unnecessarily high TX power.
6.3 Runtime targets and expectations
For WiFi reporting with 2-second events and 10 open/close cycles/day, expected AA runtime is approximately 112–244 days, depending on AA capacity, TX mode, and reed state.
For BLE reporting with optimized 0.2-second events and 10 open/close cycles/day, expected AA runtime can improve dramatically, approximately 1453–2191 days for a 2000 mAh AA in modeled low-to-mid BLE cases. Real-world cell aging, pulse capability, self-discharge, RF retries, and enclosure/range effects will reduce this.
7. Wireless reporting requirements
7.1 Preferred default: BLE event advertising
The default firmware should prioritize BLE event reporting for battery life.
Requirements:
Device wakes from deep sleep on reed state change or user interaction.
Firmware reads reed/user state after debounce/settle.
Firmware broadcasts a compact BLE event payload.
Firmware returns to deep sleep immediately after a short advertising window.
Target BLE active time: 0.2–0.5 s if the hub reliably receives events.
Initial bring-up advertising window may be 700 ms; increase to 1.5–3 s only if reliability requires it.
Use the lowest BLE TX power that passes range/reliability testing; start with 0 dBm or 9 dBm for margin, then reduce if possible.
Recommended BLE identity:
Table
Item
Value
Device name
LatchBit or project-specific product name
Service UUID
7b18d000-9f2a-4e1c-9c6a-2b4b0d6d0001
Advertisement type
Non-connectable or short connectable event, depending on hub design
Recommended manufacturer data payload:
Table
Byte(s)
Meaning
0–1
Company ID or temporary pseudo ID. Existing starter used 0xFFFF. Replace with real assigned ID if productized.
WiFi reporting is supported by the ESP32-C3 hardware, but should be treated as a higher-power mode.
If WiFi is implemented:
Store WiFi credentials securely using ESP32 NVS/Preferences.
Avoid continuous WiFi association.
Wake, connect, report, and sleep as fast as possible.
Use static IP or other fast reconnect techniques if acceptable.
Minimize DHCP, DNS, TLS handshake, and retry time.
Target less than 2 seconds active time per event; ideally less than 1 second.
Use lowest TX power that reliably reaches the access point without increasing retries.
Include brownout/retry logging during development.
If reporting fails, use a bounded retry count and go back to sleep rather than draining the AA cell indefinitely.
8. Sleep and wake requirements
8.1 Sleep mode
Use ESP32-C3 deep sleep for normal idle.
Preserve small state in RTC memory, including last reed state, boot count, and event count.
Turn LED pins to off state before sleeping.
Disable unused peripherals before sleep.
Keep GPIO pulls consistent with the external hardware to minimize leakage.
8.2 Reed wake strategy
The reed input is effectively level-based, so firmware must store the last reed state and arm wake for the opposite level:
Table
Current reed state before sleep
GPIO0 level
Desired next event
Wake level
Closed / magnet present
LOW
Open
HIGH
Open / magnet absent
HIGH
Closed
LOW
ESP32-C3 Arduino builds may not expose classic ESP32 EXT1 wake APIs. Prefer ESP32-C3-compatible GPIO wake APIs such as gpio_wakeup_enable() plus esp_sleep_enable_gpio_wakeup() when using Arduino-ESP32 versions where EXT1 is unavailable.
8.3 User wake limitation
The USER button is active LOW. In the simple wake strategy:
If reed is open and firmware arms LOW wake, USER can also be armed as LOW wake.
If reed is closed and firmware arms HIGH wake to detect opening, USER cannot always be simultaneously armed using the same simple level-wake mode.
EN/reset remains physically available at all times.
If always-wake-on-user is required, the firmware developer should evaluate ESP32-C3 wake-source capabilities in the exact Arduino/IDF version and may need a different wake design.
9. Debounce and event handling
9.1 Reed debounce
The PCB includes hardware filtering on the reed input, but firmware should still debounce after wake.
Recommended behavior:
On wake, wait 20–50 ms for input settling.
Sample reed multiple times over a short window.
Accept a state change only if readings are stable.
Suppress duplicate reports where the state equals the last reported state unless the wake cause is boot/timer/manual test.
9.2 User button handling
Suggested gestures:
Table
Gesture
Behavior
Short press during wake/setup
Send user/test event.
Hold USER during reset/power-up
Enter setup/provisioning/test mode.
Long hold, e.g. >10 s
Optional factory reset / clear credentials if WiFi provisioning is implemented.
10. Programming and bring-up requirements
10.1 Programming header J1
Table
J1 pin
Connection
1
3.3 V / VOUT
2
ESP32-C3 TXD0
3
ESP32-C3 RXD0
4
BOOT / GPIO9
5
EN/reset
6
GND
Flashing procedure if auto-reset is not available:
Connect 3.3 V UART adapter GND, TX, RX.
Hold BOOT low.
Pulse EN/reset low, then release EN.
Release BOOT after the upload begins.
Use 115200 baud if upload at higher speed is unreliable.
Do not use a 5 V UART interface on ESP32-C3 RX/TX pins.
10.2 Serial logs
During development, print at 115200 baud:
Firmware version/build date.
Wake cause.
Reed raw level and interpreted state.
User button state.
Event code.
Wireless report result.
Sleep wake configuration.
Brownout/reset reason if available.
Release builds should reduce or disable serial output to save active time.
11. Firmware states
Recommended state machine:
Boot/init
Initialize GPIOs.
Initialize serial if debug build.
Read reset/wake cause.
Input settle/debounce
Wait 20–50 ms.
Read reed and user button.
Mode decision
If USER held, enter setup/test/provisioning.
Else determine event from current reed state vs RTC last state.
Indication
Optional short LED feedback.
Report
BLE advertising by default, optional WiFi report if configured.
Persist state
Update RTC/NVS as needed.
Arm wake
Configure reed opposite-level wake.
Configure user wake if compatible with selected wake mode.
Deep sleep
12. Battery measurement
The current schematic data does not show a dedicated battery-voltage divider into an ADC pin. Therefore:
Do not implement battery voltage reporting unless a valid ADC input path is confirmed on the PCB.
Do not enable internal pulls or repurpose strap pins for battery measurement.
If a future PCB revision adds a divider, ensure the divider is switched or high impedance enough for AA sleep current targets, and account for ADC source impedance/acquisition time.
13. Reliability and failure behavior
Firmware must avoid battery-draining failure loops.
Requirements:
Wireless retries must be bounded.
If report fails, optionally send a failure LED indication only during manual/setup mode, then sleep.
Maintain event counters to help detect missed reports.
Avoid long blocking setup/connect loops on every reed wake.
Do not keep WiFi/BLE active while waiting indefinitely for a hub/AP.
Include a periodic failsafe wake only if useful; if implemented, use a long interval such as once per day.
14. Validation checklist for firmware developer
14.1 Compile and flash
Compile for ESP32-C3 target.
Confirm selected Arduino-ESP32 core version.
Confirm NimBLE-Arduino version.
Flash through J1/UART or native USB method used for bring-up.
Confirm serial boot log at 115200 baud.
14.2 GPIO validation
Confirm reed closed reports GPIO0 LOW and closed=true.
Confirm reed open reports GPIO0 HIGH and closed=false.
Confirm USER button reports active LOW.
Confirm BOOT/EN still work for flashing/reset.
Confirm RGB LED channel polarity and colors.
Confirm GPIO2 is not driven by firmware.
14.3 Sleep/wake validation
Closed → open transition wakes and reports open.
Open → closed transition wakes and reports closed.
Device returns to deep sleep after report.
LED is off in deep sleep.
Wake cause logging matches expected behavior.
USER wake/setup behavior is documented and tested for both reed states.
14.4 Wireless validation
BLE advertisement payload decodes correctly.
Hub receives events at intended distance and enclosure orientation.
Advertising window is minimized after reliability testing.
TX power is reduced only after packet reliability remains acceptable.
If WiFi is used, active time and retry count are measured.
14.5 Power validation
Measure deep-sleep current with LED off.
Measure current with reed closed and open to quantify R2 contribution.
Measure active current during BLE and/or WiFi report.
Test with fresh and partially depleted AA cells.
Test cold/aged cell behavior if product environment requires it.
Confirm no brownouts during RF transmit.
15. Known firmware starter issue to avoid
Earlier starter code targeted Arduino-ESP32 3.x APIs and may fail under Arduino-ESP32 2.x with errors such as:
ledcAttach not declared.
NimBLEAdvertising has no setScanResponse member.
ESP32-C3 EXT1 wake symbols unavailable.
A robust implementation should:
Use Arduino-ESP32 2.x compatible ledcSetup() + ledcAttachPin() or guard LEDC calls by core version.
Avoid adv->setScanResponse(false) unless confirmed available in the installed NimBLE-Arduino version.
Use ESP32-C3-compatible GPIO wake APIs for the selected Arduino/IDF version.
16. Acceptance criteria
Firmware is acceptable for initial hardware bring-up when:
It compiles with the documented Arduino/PlatformIO environment.
It flashes successfully through the available programming method.
Serial logs identify firmware version, wake cause, reed state, and event type.
Reed open/closed events are correctly detected and reported.
RGB LED status works and remains off during sleep.
Device returns to deep sleep after each event.
BLE reporting works with a documented payload and hub decode method, or WiFi reporting works with bounded retries.
Deep sleep current and RF burst behavior are measured on assembled hardware.
The firmware does not use GPIO2 as a runtime output and does not interfere with BOOT/EN programming behavior.
A compact battery-powered door/window sensor built around the ESP32-C3-MINI-1-N4 module. The design uses a single non-rechargeable AA cell with a TPS613221A 3.3 V boost regulator, reed switch magnetic contact sensing, low-power wake/report/sleep firmware strategy, RGB status LED for setup feedback, BOOT/EN controls, programming header, and input polarity protection. It is intended for smart-home security and automation applications, supporting WiFi or Bluetooth LE hub reporting with emphasis on low idle current, reliable RF burst power delivery, and field-replaceable battery operation. #ESP32-C3 #BLE#WiFi#DoorSensor#WindowSensor#ReedSwitch#LowPower#BatteryPowered#SmartHome#IoT