This project implements firmware for an STM32F103RBT7 microcontroller designed to:
- Measure temperature using an MCP9701A analog sensor.
- Measure current using an INA4181A4 current sense amplifier and a shunt resistor.
- Control a cooling fan via PWM based on the measured temperature.
The firmware utilizes the STM32 HAL library, DMA for efficient ADC sampling, and timers for PWM generation and ADC triggering.
- Reads temperature (MCP9701A) and current (INA4181A4).
- Uses TIM3-triggered ADC with DMA for efficient sampling at 1 kHz.
- Applies a moving average filter (configurable window size) to sensor readings.
- Controls a fan using 100 kHz PWM generated by TIM2.
- Implements linear fan speed control based on configurable temperature thresholds.
- Designed for STM32F103RBT7 MCU.
- Built using STM32 HAL library and ARM GCC toolchain.
- Microcontroller: STM32F103RBT7
- Temperature Sensor: MCP9701AT-E/TT (or other compatible 500mV @ 0°C, 10mV/°C analog sensor)
- Current Sensor: INA4181A4QPWRQ1 (100 V/V gain variant)
- Shunt Resistor: Value defined in code (Ohm) - ensure appropriate wattage rating for expected current.
- Fan: Standard 2, 3, or 4-wire PWM controllable DC fan.
- Power Supply: Appropriate voltage(s) for MCU (3.3V), sensors, and fan.
- Toolchain: ARM GCC (
arm-none-eabi-gcc) - Build System: Make
- Programming Utility: STM32CubeProgrammer, ST-Link Utility, or OpenOCD for flashing the firmware onto the MCU.
- Clone the repository:
git clone https://github.com/CPRT/SmartPDB.git cd SmartPDB - Configure Project: Review and adjust parameters in
Core/Inc/main.h(or relevant config file) as needed (see Configuration section below). Ensure pin definitions match your hardware connections. - Build the firmware:
- Using Make:
make clean make
- Using an IDE: Import the project and use the IDE's build command.
- Using Make:
- Flash the firmware:
- The compiled firmware will located in
build/ - Use your chosen programming utility (e.g., STM32CubeProgrammer) and an ST-Link debugger/programmer to flash the
.binor.elffile onto the STM32F103RBT7.
- The compiled firmware will located in
- Sensor Parameters:
MCP9701_TC: Temperature sensor coefficient (V/°C).MCP9701_V0C: Temperature sensor voltage offset at 0°C (V).INA4181_GAIN: Gain of the current sense amplifier (V/V).SHUNT_RESISTOR_OHMS: Value of the current shunt resistor (Ohms).
- ADC & Filtering:
V_REF: ADC reference voltage (usually 3.3V).ADC_FILTER_BUFFER_SIZE: Number of samples for the moving average filter window.
- Fan Control:
TEMP_MIN_THRESHOLD: Temperature (°C) below which the fan runs at minimum speed.TEMP_MAX_THRESHOLD: Temperature (°C) above which the fan runs at maximum speed.FAN_PWM_MIN_DUTY_PERCENT: Minimum fan duty cycle (%).FAN_PWM_MAX_DUTY_PERCENT: Maximum fan duty cycle (%).
PA0: ADC_IN0 (Temperature Sensor Output)PA1: ADC_IN1 (Current Sensor Output)PA15: PWM Output to Fan Control Circuit
Lmao dont touch my shit