This repository contains the source code, hardware simulation, and documentation for Node 1 (Safety System & TPMS) of the Smart Car project, developed on the STM32F401VE microcontroller.
- Instant Crash Detection: Utilizing External Interrupts (EXTI) on a falling edge to ensure the fastest possible response time.
- Emergency Brake Activation: Instantly triggering a relay-based braking mechanism (Brake Actuator) the moment the sensor is activated.
- Continuous Analog Monitoring: Utilizing the Analog-to-Digital Converter (ADC) to read real-time tire pressure values from an analog sensor.
- Non-Blocking Logic: Implementing a timer-based polling system to check tire pressure every 500ms without halting the main CPU loop.
- Low-Pressure Alert: Automatically activating a warning indicator when the pressure drops below a defined safety threshold.
- Non-Blocking Data Transmission: Utilizing UART with Interrupts (IT) to transmit data without stalling the CPU or crashing the simulation.
- Emergency Prioritization: Instantly broadcasting an emergency alert to the central system when the brake is activated, overriding standard telemetry.
- Periodic Telemetry: Sending formatted, real-time tire pressure data to a monitoring console every 1000ms.
- Development Environment: STM32CubeMX + Keil uVision v5
- Simulation Environment: Proteus 9 Professional
- Microcontroller Clock: 16MHz (Internal - HSI)
| Pin Name | Type (I/O) | Circuit Role | Initial State |
|---|---|---|---|
| PA0 | Input (EXTI0 / Pull-up) | Crash Sensor Button | High (1) |
| PA2 | Analog Input (ADC1_IN2) | Tire Pressure Sensor (Analog) | N/A |
| PA9 | Alternate Function | USART1_TX (Data Transmission) | N/A |
| PD12 | Output (Push-Pull) | Normal Status (Green LED) | High (1) |
| PD13 | Output (Push-Pull) | Emergency Status (Red LED) | Low (0) |
| PD14 | Output (Push-Pull) | TPMS Warning (Blue LED) | Low (0) |
| PC1 | Output (Push-Pull) | Brake Relay Actuator & Yellow LED | Low (0) |
The hardware initialization was completely configured using STM32CubeMX:
- System Core: Debug mode set to
Serial Wire. - RCC (Clock): Configured to use the High-Speed Internal (HSI) clock for simulation stability.
- EXTI Setup (Phase 1):
PA0configured asGPIO_EXTI0with an internal Pull-up resistor and Falling Edge trigger detection. NVIC enabled for EXTI line0. - ADC1 Setup (Phase 2): * Configured
PA2asADC1_IN2in Independent Mode.- Stability Fix: Set Sampling Time to 480 Cycles to ensure proper capacitor charging and stable digital readings during Proteus simulation.
- Trigger set to Software Start with 12-bit resolution.
- USART1 Setup (Phase 3): Configured in Asynchronous mode with a Baud Rate of 9600. Crucially, the USART1 global interrupt is enabled in the NVIC settings to allow non-blocking transmission.
- GPIO Setup:
PD12,PD13,PD14, andPC1configured as standardGPIO_Outputwith Push-Pull configuration.
The application logic was written in C using the STM32 HAL Library:
- Phase 1 (Interrupt Logic): Bypassed polling by utilizing the
HAL_GPIO_EXTI_Callback(). Upon a crash (PA0 pulled to ground), the system immediately turns OFF the Green LED, turns ON the Red LED, and setsPC1HIGH to activate the brake relay. - Phase 2 (TPMS Logic): * Introduced
#define TPMS_THRESHOLD 2000and#define TPMS_CHECK_PERIOD_MS 500for modularity.- Implemented a
Check_Tire_Pressure()function to start the ADC, poll for conversion, and compare the result against the threshold. Iftire_pressure_adc < 2000, the Blue TPMS LED (PD14) turns ON. - Non-Blocking Architecture: Used
HAL_GetTick()in the mainwhile(1)loop to execute the TPMS check every 500ms, ensuring the microcontroller remains free for other potential tasks.
- Implemented a
- Phase 3 (Telemetry & Prioritization):
- Replaced blocking UART functions with
HAL_UART_Transmit_IT()to prevent system crashes caused by Proteus timing discrepancies. - Introduced a priority flag (
brake_emergency_flag) triggered within the EXTI callback. - Formatted standard strings using
sprintfto periodically transmit"Tire Pressure: [value]"every 1 second, while instantly overriding the UART buffer with"!!! EMERGENCY BRAKE ACTIVATED !!!"upon a crash event.
- Replaced blocking UART functions with
The physical behavior of the system was validated using Proteus 9 Professional:
- MCU Setup: The
STM32F401VEcomponent is loaded with the compiled.hexfile. - Analog Power Requirements (Crucial): To ensure proper ADC functionality in Proteus, the
VDDAandVREF+pins must be explicitly connected to the +5V power rail, andVSSA/VREF-connected to Ground. Without this reference voltage, the ADC outputs zero. - TPMS Sensor (Phase 2): A Potentiometer is connected to
PA2to simulate varying tire pressures. - Serial Terminal (Phase 3): Added a Virtual Terminal connected to the MCU's
TXpin (PA9). The terminal baud rate is strictly matched to 9600 to verify real-time monitoring of sensor data and emergency alerts. - Relay & Actuator Circuit (Phase 1): * The
PC1pin drives an NPN Transistor (BC547) through a 1kΩ base resistor to safely switch the relay.- A 12V Relay acts as the mechanical brake actuator.
- A Flyback Diode (1N4007) is placed in parallel with the relay coil to protect the transistor from inductive voltage spikes.
├── 1_Firmware/ # Keil project files, CubeMX .ioc config, and source codes (.c/.h)
├── 2_Simulation/ # Proteus simulation file (.pdsprj)
└── 3_Docs/ # Project documentation, datasheets, and PDF reports