Skip to content

Activity Monitor Firmware

Learn to build, flash, and test the Room Activity Scanner firmware featuring HLK-LD2420 mmWave radar integration, MQTT communication, and relay control for ESP32-S3. Includes complete setup guide, hardware wiring, testing procedures, and troubleshooting for IoT health monitoring applications. #ESP-IDF #HLK-LD2420 #MQTT

Introduction

The Activity Monitor firmware is the current implementation of the Room Activity Scanner project, focusing on presence detection using the HLK-LD2420 mmWave radar sensor. This firmware provides a foundation for room activity monitoring with real-time MQTT communication and relay control capabilities.

This guide walks you through setting up, building, flashing, and testing the Activity Monitor firmware on your ESP32-S3 device.


System Architecture

System Flow Diagram

Currently Implemented Features

HLK-LD2420 Presence Detection

Status: ✅ Operational

mmWave radar sensor integration via UART for detecting human presence, measuring distance, and tracking movement with high accuracy.

Relay Control System

Status: ✅ Completed

GPIO-based relay control with manual and automatic modes for controlling fans, lights, and other appliances based on sensor input.

ADC Reading (MQ-2 Gas Sensor)

Status: ✅ Completed

Analog-to-digital conversion for MQ-2 gas sensor with baseline calibration logic currently in progress for accurate air quality measurements.

MQTT Communication

Status: 🛠 In Progress

Basic MQTT publishing to broker working. Refinements ongoing for QoS levels, retained messages, and robust reconnection handling.

Feature Status Table

FeatureStatusImplementation Notes
HLK-LD2420 UART Communication✅ CompletedProtocol parser implemented, distance and presence detection operational
Relay GPIO Control✅ CompletedManual on/off control and automated triggering based on presence
MQ-2 ADC Reading✅ CompletedRaw ADC values captured, calibration algorithm in development
WiFi Connectivity✅ CompletedStation mode with automatic reconnection
MQTT Publishing🛠 In ProgressBasic publish working, enhancing reliability and features
MQTT Subscriptions🛠 In ProgressCommand reception for relay control being tested
Sensor Calibration Logic🛠 In ProgressBaseline establishment and drift compensation algorithms
BMP280 I2C Integration🔲 Not StartedPlanned for environmental monitoring
RCWL-0516 Motion Detection🔲 Not StartedGPIO interrupt-based implementation planned
INMP441 I2S Audio🔲 Not StartedSound level monitoring planned
OTA Firmware Updates🔲 PlannedFuture enhancement for remote updates

Prerequisites



Before you begin, ensure you have the following tools and hardware:

Hardware Requirements

  • YD-ESP32-S3 development board with 16MB flash memory
  • HLK-LD2420 mmWave radar sensor module
  • MQ-2 gas sensor (optional but recommended)
  • Relay module (5V or 3.3V compatible)
  • USB cable for programming and power
  • Breadboard and jumper wires for prototyping

Software Requirements

  • Ubuntu Linux (20.04 LTS or later recommended)
  • ESP-IDF v5.0 or later installed and configured
  • Python 3.8+ (comes with ESP-IDF)
  • Git for repository cloning
  • MQTT Broker: Mosquitto (local) or cloud service access

Verify ESP-IDF Installation

Ensure ESP-IDF is properly installed:

Terminal window
. $HOME/esp/esp-idf/export.sh
idf.py --version

You should see output like ESP-IDF v5.x.x.


Hardware Setup

Pin Connections

Connect the sensors and modules to your ESP32-S3 according to this wiring diagram:

HLK-LD2420 mmWave Radar (UART)

HLK-LD2420 PinESP32-S3 PinDescription
VCC5VPower supply
GNDGNDGround
TXGPIO 44 (RX1)UART transmit to ESP32 receive
RXGPIO 43 (TX1)UART receive from ESP32 transmit

MQ-2 Gas Sensor (Analog)

MQ-2 PinESP32-S3 PinDescription
VCC5VPower supply
GNDGNDGround
AOUTGPIO 1 (ADC1_CH0)Analog output to ADC

Relay Module

Relay PinESP32-S3 PinDescription
VCC5VPower supply
GNDGNDGround
INGPIO 21Control signal

Note: Pin assignments may be configured via menuconfig. Check your specific configuration before wiring.


Installation & Setup

  1. Clone the Repository

    Clone the Room Activity Scanner repository from GitHub:

    Terminal window
    git clone [email protected]:SiliconWit/room-activity-scan.git
    cd room-activity-scan/embedded_programming/activity_monitor
  2. Set ESP32-S3 Target

    Configure the build system for the ESP32-S3 architecture:

    Terminal window
    idf.py set-target esp32s3

    This downloads necessary toolchain components and configures the build environment.

  3. Configure Project Settings

    Launch the interactive configuration menu:

    Terminal window
    idf.py menuconfig

    Critical Configuration Items:

    Serial flasher config

    • Flash size: Set to 16 MB (required for YD-ESP32-S3)
    • Flash SPI mode: Set to DIO (Dual I/O)
    • Flash SPI speed: 80 MHz (recommended)
    • After flashing: Reset after flashing

    Press S to save, then Q to quit menuconfig.

  4. Build the Firmware

    Compile the project:

    Terminal window
    idf.py build

    Build output is saved to build/ directory. Look for activity_monitor.bin.

  5. Flash to ESP32-S3

    Connect your ESP32-S3 via USB and flash the firmware:

    Terminal window
    idf.py -p /dev/ttyUSB0 flash

    Note: Replace /dev/ttyUSB0 with your actual port (check with ls /dev/ttyUSB* or dmesg | grep tty).

  6. Monitor Serial Output

    View real-time logs and debug output:

    Terminal window
    idf.py -p /dev/ttyUSB0 monitor

    Press Ctrl+] to exit monitor mode.

    Combined Flash & Monitor (recommended):

    Terminal window
    idf.py -p /dev/ttyUSB0 flash monitor

Testing the Firmware



Initial Boot Verification

After flashing, monitor the serial output. You should see:

I (328) boot: ESP-IDF v5.x.x
I (342) boot: Chip Revision: v0.1
I (356) boot_comm: chip revision: v0.1, min. bootloader chip revision: v0.0
I (894) wifi:WiFi connected
I (1024) MQTT: MQTT_EVENT_CONNECTED
I (1156) HLK_LD2420: Radar initialized successfully
I (1200) APP: System ready

Testing HLK-LD2420 Presence Detection

Presence Detection Test

Objective: Verify the radar detects human presence

Steps:

  1. Position yourself 1-3 meters in front of the sensor
  2. Remain still (mmWave radar detects stationary humans)
  3. Monitor serial output

Expected Output:

I (5234) HLK_LD2420: Target detected at 2.3m
I (5456) HLK_LD2420: Presence: YES, Distance: 2.3m

Troubleshooting:

  • No detection: Check UART wiring (TX/RX may be swapped)
  • Random detections: Adjust sensitivity in sensor configuration
  • No serial output: Verify UART port configuration in menuconfig

Testing Relay Control

Manual Relay Control via MQTT

Via MQTT Command (requires MQTT broker setup):

Publish command to turn relay ON:

Terminal window
mosquitto_pub -h localhost -t "room/scanner/relay/set" -m "ON"

Turn relay OFF:

Terminal window
mosquitto_pub -h localhost -t "room/scanner/relay/set" -m "OFF"

Expected Behavior:

  • Relay clicks audibly
  • LED indicator changes state
  • Serial output confirms state change

Serial Output:

I (45678) RELAY: Command received: ON
I (45680) RELAY: Relay state changed to ON

Testing MQ-2 Gas Sensor

Baseline Measurement

Objective: Capture clean air baseline

Steps:

  1. Ensure sensor is in fresh air (open window)
  2. Let sensor warm up for 2-3 minutes
  3. Monitor ADC readings

Serial Output:

I (12000) MQ2: ADC Raw: 1245
I (13000) MQ2: ADC Raw: 1238
I (14000) MQ2: ADC Raw: 1251
I (15000) MQ2: Baseline established: 1244

Note: Initial readings may fluctuate. Baseline stabilizes after warm-up period.

Testing MQTT Communication

Mosquitto MQTT Broker Setup

Install Mosquitto (if not already installed):

Terminal window
sudo apt update
sudo apt install mosquitto mosquitto-clients
sudo systemctl start mosquitto
sudo systemctl enable mosquitto

Verify Broker Running:

Terminal window
sudo systemctl status mosquitto

Test Broker:

Terminal window
# Terminal 1: Subscribe
mosquitto_sub -h localhost -t "test" -v
# Terminal 2: Publish
mosquitto_pub -h localhost -t "test" -m "Hello MQTT"

Data Format & Topics

Published Topics

TopicPayload FormatFrequencyDescription
room/scanner/presence{"detected":bool,"distance":float,"confidence":int}1-2sPresence detection data
room/scanner/air_quality{"gas_level":int,"status":string}5-10sAir quality readings
room/scanner/relay{"state":string,"auto_mode":bool}On changeRelay state updates
room/scanner/status{"uptime":int,"wifi_rssi":int,"free_heap":int}30sSystem health status

Subscribed Topics (Commands)

TopicPayload FormatAction
room/scanner/relay/set{"state":"on"} or {"state":"off"}Control relay
room/scanner/config/set{"parameter":"value"}Update configuration
room/scanner/command{"cmd":"reset"} or {"cmd":"calibrate"}System commands

Example Payloads

Presence Detection:

{
"detected": true,
"distance": 2.34,
"confidence": 87,
"timestamp": "2025-04-10T14:32:15Z"
}

Air Quality:

{
"gas_level": 1244,
"status": "normal",
"baseline": 1240,
"timestamp": "2025-04-10T14:32:15Z"
}

System Status:

{
"uptime": 3600,
"wifi_rssi": -45,
"free_heap": 156789,
"firmware_version": "1.0.0",
"timestamp": "2025-04-10T14:32:15Z"
}

Troubleshooting

Common Issues

Flashing Issues

Problem: A fatal error occurred: Failed to connect to ESP32-S3

Solutions:

  1. Hold BOOT button while connecting USB
  2. Check USB cable - some cables are power-only
  3. Verify port: ls /dev/ttyUSB* or ls /dev/ttyACM*
  4. User permissions: sudo usermod -a -G dialout $USER (logout/login required)
  5. Try different USB port - some ports have power issues

Problem: Flash size mismatch

Solution: Set flash size to 16MB in menuconfig → Serial flasher config

Debug Logging

Enable verbose logging for troubleshooting:

Terminal window
idf.py menuconfig
# Navigate to: Component config → Log output
# Set default log level to: Debug or Verbose

Component-Specific Logging:

// In your code, set individual component log levels:
esp_log_level_set("HLK_LD2420", ESP_LOG_DEBUG);
esp_log_level_set("MQTT", ESP_LOG_VERBOSE);
esp_log_level_set("wifi", ESP_LOG_DEBUG);

Future Planned Features



Based on the project roadmap, the following enhancements are planned for future firmware versions:

Short-Term Additions

BMP280 Environmental Sensor

Priority: High

I2C integration for temperature and atmospheric pressure monitoring. Enables comfort optimization and weather correlation.

Implementation Plan:

  • I2C driver integration
  • Temperature/pressure reading
  • Calibration and filtering
  • MQTT data publishing

RCWL-0516 Motion Detection

Priority: Medium

GPIO interrupt-based Doppler motion sensor for complementary motion detection alongside mmWave radar.

Implementation Plan:

  • GPIO interrupt configuration
  • Debounce logic
  • Integration with presence detection
  • Activity pattern tracking

INMP441 Sound Monitoring

Priority: Medium

I2S digital audio interface for ambient sound level measurement and noise analysis.

Implementation Plan:

  • I2S interface configuration
  • Audio sampling and buffering
  • dB level calculation
  • Sound event detection

Enhanced MQTT Features

Priority: High

Robust MQTT implementation with advanced features for production deployment.

Features:

  • QoS 1 and 2 support
  • Retained messages for state
  • Last Will and Testament
  • TLS/SSL encryption
  • Message queuing during disconnection

Long-Term Vision

Full-Stack Web Application

Technology Stack:

  • Frontend: React.js with real-time WebSocket updates
  • Backend: FastAPI (Python) RESTful API
  • Database: MongoDB for time-series data storage
  • Visualization: Chart.js or D3.js for graphs

Features:

  • Real-time sensor visualization
  • Historical data analysis and trends
  • Device configuration management
  • Alert rule configuration
  • Multi-device monitoring
  • User authentication and access control

Current Status: 🔲 Planned


Contributing to Development

This project is under active development and welcomes contributions!

Areas Needing Help

  • Sensor Integration: BMP280, RCWL-0516, INMP441 drivers
  • MQTT Enhancements: QoS, TLS/SSL, message queuing
  • Calibration Algorithms: Advanced MQ-2 calibration and drift compensation
  • Testing: Hardware testing, edge case scenarios
  • Documentation: Wiring diagrams, tutorials, use case examples

How to Contribute

  1. Fork the repository on GitHub
  2. Create a feature branch: git checkout -b feature/your-feature-name
  3. Make your changes with clear commit messages
  4. Test thoroughly on actual hardware
  5. Submit a pull request with description of changes

Development Guidelines

  • Follow ESP-IDF coding standards
  • Add comments for complex logic
  • Update documentation for new features
  • Test on hardware before submitting
  • Include serial output examples for debugging

Resources & References

Documentation

Tools

Community


Next Steps

Once you have the Activity Monitor firmware running successfully:

  1. Experiment with configurations - Adjust sampling rates, thresholds, and automation rules
  2. Integrate with dashboard - Set up Node-RED, Grafana, or Home Assistant for visualization
  3. Collect long-term data - Run for days/weeks to understand typical patterns
  4. Contribute improvements - Share your enhancements with the community
  5. Explore additional sensors - Add BMP280, INMP441, or other sensors



Comments

© 2021-2025 SiliconWit. All rights reserved.