The Problem That Started It All
In my office, I have a pc that produced a ton of heat (9800x3d and a 5090), the issue is my apartment does not have individual room AC control so in order to keep the room temp manageable, I have a small fain I run in the windowsill. With home assistant, I had this setup to turn on after it reaches 73F in the room, the issue is though I had to way to detect if the window was closed or open. I had a couple options i could use a switch, but that would not detect when its partially open, as i only open the window just enough for the fan. So i needed something that would measure the distance rather than a binary open/closed sensor.
Initial Research & Exploration
For this application, I need precise distance measurement rather than simple binary detection. While my primary use case is window position monitoring, this sensor design can work for many distance sensing applications.
Commercial Options:
- Traditional binary sensors (door/window, motion) only provide on/off status
- Purpose-built distance sensors are expensive and often lack smart home integration
DIY Approach:
- Time-of-Flight sensors provide precise distance measurement with excellent accuracy
- VL53L1X offers extended range (up to 4m) suitable for various monitoring applications
- ESPHome provides seamless Home Assistant integration without custom firmware development
The design uses a VL53L1X Time-of-Flight sensor for precise distance measurement. While optimized for window monitoring, the same hardware can be used for garage parking assistance, liquid level monitoring, occupancy detection, or any application requiring accurate distance sensing.
Design Requirements & Constraints
Design Requirements
Core Functionality:
- Precise distance measurement for various monitoring applications
- ESPHome integration for seamless Home Assistant connectivity
- Dual power options: USB-C or 2x AA batteries in series (wires are ugly)
- Compact form factor for versatile mounting in different locations
Power Management:
- Automatic switching between USB-C and battery power
- Long battery life when running on AA batteries
- Low power sleep modes when not actively measuring
Integration:
- ESPHome configuration for easy Home Assistant discovery
- Configurable measurement reporting and automation triggers
- OTA updates through ESPHome dashboard
Versatility:
- Applicable to window monitoring, parking assistance, liquid levels, occupancy detection
- Adjustable measurement intervals and thresholds via ESPHome configuration
- Multiple mounting orientations and installation options
Component Selection
VL53L1X Time-of-Flight Sensor:
- Extended range up to 4 meters - suitable for various distance sensing applications
- High accuracy (±3%) for precise measurement in different scenarios
- Narrow beam angle reduces interference from nearby objects
- Low power consumption ideal for battery-powered operation
- I2C interface simplifies integration and wiring
ESP32-S3 Microcontroller:
- Proven WiFi reliability for Home Assistant connectivity
- Dual-core architecture for handling sensor and networking tasks simultaneously
- Multiple GPIO options for flexible power management and expansion
- Excellent ESPHome compatibility with extensive sensor library support
- Built-in USB connectivity for easy programming and debugging
Dual Power System Design
Power Source Options:
- USB-C: For permanent installations where power is available
- 2x AA Batteries: For flexible placement where USB power isn’t practical
- Automatic Switching: Circuit detects USB-C presence and switches power source
Power Management Considerations:
- Voltage regulation from 3V (2x AA) and 5V (USB-C) to 3.3V system voltage
- Battery monitoring for low power alerts
- Deep sleep modes to maximize battery life when on AA power
- Power path switching to seamlessly transition between sources
ESPHome Integration
Configuration Approach:
- ESPHome YAML configuration for VL53L1X sensor
- Automatic Home Assistant device discovery
- Position reporting as distance sensor entity
- Battery level monitoring when on AA power
Key Features:
- Distance measurement updates at configurable intervals
- Deep sleep between measurements for battery conservation
- OTA updates through ESPHome dashboard
- Diagnostic sensors for WiFi signal strength and battery voltage
Applications and Use Cases
Primary Use Case (Window Monitoring):
- Mount sensor on window frame to measure distance to sliding window panel
- Determine exact window position for automated fan control
- Provides precise position data vs binary open/closed detection
- Enables automation based on window opening percentage
Alternative Applications:
- Garage Parking: Mount to measure car position for parking assistance
- Liquid Level Monitoring: Non-contact measurement of tank levels
- Occupancy Detection: Detect presence in doorways or specific areas
- Security Monitoring: Alert when objects move into restricted zones
Mounting Flexibility:
- Small form factor for discreet installation in various locations
- Sensor beam alignment configurable for different mounting orientations
- Power option flexibility (USB-C where available, batteries elsewhere)
- Weather-resistant design for indoor or covered outdoor installations
Design Challenges
Dual Power Management:
- Seamless switching between USB-C and battery power
- Voltage regulation from different input voltages (3V AA vs 5V USB-C)
- Battery monitoring and low power alerts
- Optimizing sleep modes for battery longevity
Sensor Integration:
- VL53L1X requires 2.8V logic levels with 3.3V ESP32-S3
- I2C communication reliability over varying distances
- Calibration for different window materials and colors
- Temperature compensation for outdoor installations
Form Factor Constraints:
- Compact PCB design to fit window frame aesthetics
- Component placement for optimal antenna performance
- Enclosure design for weather protection
- Mounting hardware for various window frame types
Project Status
This project is currently in the design phase. The goal is to create a versatile distance sensing solution that can be deployed for various monitoring applications with flexible power options.
Current Progress:
- Component selection completed (ESP32-S3 + VL53L1X)
- Power management approach defined (USB-C + AA battery dual power)
- ESPHome integration strategy planned
- Form factor requirements established for versatile mounting
Next Steps:
- Schematic design and PCB layout
- Enclosure design for various installation environments
- ESPHome configuration development with configurable thresholds
- Prototyping and testing across different use cases
ESPHome Configuration Approach
Sensor Configuration: The VL53L1X will be configured in ESPHome using the appropriate sensor platform, providing distance measurements at regular intervals with configurable update rates.
Power Management: ESPHome’s deep sleep functionality will be utilized to maximize battery life when running on AA batteries, with wake intervals optimized for the window monitoring use case.
Home Assistant Integration: ESPHome automatically handles Home Assistant discovery, creating entities for window position, battery level, and device diagnostics without manual MQTT configuration.
Home Assistant Integration
ESPHome will automatically create entities in Home Assistant for:
Core Entities:
sensor.distance_measurement
- Distance reading in centimeterssensor.distance_battery
- Battery percentage (when on AA power)binary_sensor.threshold_triggered
- Configurable distance threshold statussensor.distance_signal_strength
- WiFi signal strength for diagnostics
Automation Examples:
- Window Monitoring: Turn on/off fans based on window position
- Parking Assistance: Alert when car reaches optimal parking position
- Security: Notify when objects enter monitored zones
- Level Monitoring: Track liquid levels in tanks or containers
- Occupancy Detection: Detect presence in specific areas
Design Goals
This general-purpose distance sensor aims to provide:
Versatility:
- Small package suitable for various mounting locations and applications
- Dual power options for permanent or temporary installations
- ESPHome integration for easy Home Assistant connectivity
- Configurable thresholds and measurement intervals for different use cases
Reliability:
- Precise distance measurement for various monitoring applications
- Long battery life when operating on AA batteries
- Weather-resistant design for diverse installation environments
- Consistent performance across different materials and lighting conditions
Simplicity:
- ESPHome configuration eliminates custom firmware development
- Automatic Home Assistant discovery and entity creation
- OTA updates through standard ESPHome workflow
- Plug-and-play deployment for multiple applications
Project Files & Resources
- Schematics: KiCad Project Files
- PCB Gerbers: Manufacturing Files
- Firmware: Arduino Code on GitHub
- 3D Models: Enclosure STL Files
- Build Guide: Step-by-Step Instructions
Building your own version? Have questions about the design decisions? Reach out – I love discussing the technical details and helping others learn from my mistakes!