pwm-imu-heater.ino

Seeed Studio XIAO STM32C5 · File : pwm-imu-heater.ino · Language : C · Use case : Activer le circuit de chauffage du capteur IMU via PWM pour stabiliser la température.

Generates a PWM signal to control the integrated heating of the LSM6DS3TR-C.

The LSM6DS3TR-C sensor has internal heating driven by PWM. This code configures a timer (e.g., TIM3) to generate a PWM signal. Frequency and duty cycle must be adjusted according to the sensor datasheet specs to avoid overheating. The goal is to maintain constant temperature to reduce drift.

pwm-imu-heater.ino

#include "stm32c5xx_hal.h"

TIM_HandleTypeDef htim3;

void MX_TIM3_Init(void) {
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};
  TIM_OC_InitTypeDef sConfigOC = {0};

  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 144 - 1; // 144 MHz / 144 = 1 MHz
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 1000 - 1;   // 1 kHz PWM
  HAL_TIM_PWM_Init(&htim3);
  
  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = 500; // 50% Duty Cycle
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1);
  HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
}

int main(void) {
  HAL_Init();
  MX_TIM3_Init();
  while(1) {
    // Ajuster la puissance de chauffe selon la température lue
    // Ex: si Temp < 40C, Pulse = 800, sinon Pulse = 200
    HAL_Delay(100);
  }
}

← Back to the board