Skip to content

s0Appy/mcu-signal-processing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 

Repository files navigation

MCU Signal Processing – ADC & Ultrasonic Distance Capture (STM32F411RE)

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.


System Overview

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.


Intended Usage

  1. Power the board via USB
  2. Observe live ultrasonic distance on the LCD
  3. Press B1 to begin capture
  4. Move a flat object in front of the ultrasonic sensor and adjust the potentiometer
  5. Observe live statistics (min/max, peaks, direction changes)
  6. Press B1 again or wait 60 seconds to end capture
  7. Review post-capture summaries on the LCD and via UART

Hardware & Peripherals

component_layout
  • 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

Pin Mapping

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

Timer Architecture

Accurate ultrasonic ranging requires precise timing, achieved using multiple hardware timers.

TIM2 – ADC Sampling

  • 100 Hz sampling rate (10 ms)
  • Triggers ADC conversions via interrupt
  • Ensures consistent analog acquisition

TIM3 – Ultrasonic Echo Timing

  • Free-running microsecond-resolution counter
  • Measures echo pulse width
  • Accessed via HAL_TIM_GET_COUNTER()
  • No interrupts → avoids jitter and overhead

TIM4 – Capture Scheduler

  • 50 ms period (20 Hz)
  • Triggers ultrasonic measurements and data logging during LIVE CAPTURE

EXTI – Echo Pulse Capture

  • External interrupt on PA8
  • Rising and falling edges timestamped
  • Pulse width = echo travel time

Ultrasonic Sensor Implementation

Measurement Principle

  1. TRIG pulse generated on PA9
  2. Ultrasonic burst emitted by sensor
  3. ECHO pin goes HIGH
  4. Rising edge → store TIM3 counter (t_start)
  5. Falling edge → store TIM3 counter (t_end)
  6. Pulse width = t_end - t_start
  7. Distance calculation:
 distance_mm = (pulse_width_us * speed_of_sound) / 2 

This approach avoids software delays and provides sub-millisecond precision.


Direction Change Detection (Distance)

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.


Software Architecture

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.

state_machine

Display Behaviour

Live Capture Views

  • View A: Time, voltage min/max, peak count
  • View B: Time, distance min/max, direction changes

Post Capture Views

  1. Capture duration, voltage range, total peaks
  2. Time above/below thresholds, mid-level crossings
  3. Ultrasonic min/max/average distance
  4. Cross-metrics (e.g. peaks when distance < 100 mm)

LCD updates are change-driven to reduce I2C traffic and flicker.


UART Output

  • Human-readable summary report
  • Time-indexed raw sample dump
  • Designed for easy import into Python / MATLAB / Jupyter notebooks

Testing & Validation

  • 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
data_validation

Design Justifications

  • Interrupt-driven echo timing for precision
  • Hardware timers eliminate jitter
  • Threshold-based motion detection for noise immunity
  • Flat data structures ensure predictable memory usage

Conclusion

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.


AI Usage Statement

Generative AI tools (including ChatGPT) were used for documentation assistance, code cleanup, and limited debugging support.


References

  • STM32F4 Reference Manual
  • STM32 HAL Library Documentation
  • HC-SR04 Ultrasonic Sensor Datasheet

About

simutaneous digital and analog signal processing using an stm32 F411RE

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages