This repository contains the firmware and supporting material for ENGN4213 – Assignment 2, which implements live signal acquisition, processing, and visualization on the STM32F411RE microcontroller. The system simultaneously samples an analog voltage (ADC) and distance measurements from an ultrasonic sensor, displays results live on an LCD, and streams captured data over UART for offline visualization.
Primary contribution focus: Ultrasonic sensor timing, distance calculation, and motion analysis logic.
The system is designed around a finite state machine with three clearly defined modes:
- IDLE – Live ultrasonic distance measurement and display
- LIVE CAPTURE – Time-synchronised sampling of ADC voltage and ultrasonic distance
- POST CAPTURE – Offline analysis, statistics, LCD summaries, and UART reporting
This structure ensures deterministic behaviour, clean separation of concerns, and predictable user interaction.
- Power the board via USB
- Observe live ultrasonic distance on the LCD
- Press B1 to begin capture
- Move a flat object in front of the ultrasonic sensor and adjust the potentiometer
- Observe live statistics (min/max, peaks, direction changes)
- Press B1 again or wait 60 seconds to end capture
- Review post-capture summaries on the LCD and via UART
- MCU: STM32F411RE (Nucleo)
- Ultrasonic Sensor: HC-SR04–style trigger/echo interface
- ADC Input: Trim potentiometer
- Display: 16x2 LCD via I2C (PCF8574A)
- Communication: UART (USB serial)
- User Input: Buttons (B1/B2), LEDs
| Function | MCU Pin | Notes |
|---|---|---|
| ADC Input | PA0 | ADC1 IN0 |
| Ultrasonic TRIG | PA9 | GPIO Output |
| Ultrasonic ECHO | PA8 | GPIO Input + EXTI |
| LCD SDA / SCL | D14 / D15 | I2C |
| UART TX | PA2 | Async serial |
Accurate ultrasonic ranging requires precise timing, achieved using multiple hardware timers.
- 100 Hz sampling rate (10 ms)
- Triggers ADC conversions via interrupt
- Ensures consistent analog acquisition
- Free-running microsecond-resolution counter
- Measures echo pulse width
- Accessed via
HAL_TIM_GET_COUNTER() - No interrupts → avoids jitter and overhead
- 50 ms period (20 Hz)
- Triggers ultrasonic measurements and data logging during LIVE CAPTURE
- External interrupt on PA8
- Rising and falling edges timestamped
- Pulse width = echo travel time
- TRIG pulse generated on PA9
- Ultrasonic burst emitted by sensor
- ECHO pin goes HIGH
- Rising edge → store TIM3 counter (
t_start) - Falling edge → store TIM3 counter (
t_end) - Pulse width =
t_end - t_start - Distance calculation:
distance_mm = (pulse_width_us * speed_of_sound) / 2 This approach avoids software delays and provides sub-millisecond precision.
To detect meaningful object motion:
- 32-sample rolling history
- Minimum delta threshold: 20 mm
- Direction change registered only when both trend reversal and threshold are satisfied
This prevents jitter-induced false detections.
The firmware is structured around a finite state machine:
IDLE ↓ B1 LIVE CAPTURE ↓ timeout / B1 POST CAPTURE ↓ B1 IDLE Each state activates only the required subsystems, ensuring deterministic execution.
- View A: Time, voltage min/max, peak count
- View B: Time, distance min/max, direction changes
- Capture duration, voltage range, total peaks
- Time above/below thresholds, mid-level crossings
- Ultrasonic min/max/average distance
- Cross-metrics (e.g. peaks when distance < 100 mm)
LCD updates are change-driven to reduce I2C traffic and flicker.
- Human-readable summary report
- Time-indexed raw sample dump
- Designed for easy import into Python / MATLAB / Jupyter notebooks
- Ultrasonic distance validated against ruler measurements
- Motion detection verified via controlled object movement
- ADC accuracy checked using known references
- UART logs cross-checked against LCD summaries
- Interrupt-driven echo timing for precision
- Hardware timers eliminate jitter
- Threshold-based motion detection for noise immunity
- Flat data structures ensure predictable memory usage
This project demonstrates a robust real-time embedded system with a strong emphasis on accurate ultrasonic distance measurement and motion analysis. The design balances precision, reliability, and simplicity within the constraints of a microcontroller-based platform.
Generative AI tools (including ChatGPT) were used for documentation assistance, code cleanup, and limited debugging support.
- STM32F4 Reference Manual
- STM32 HAL Library Documentation
- HC-SR04 Ultrasonic Sensor Datasheet