Here are several practical motor control applications you can implement with the [STM32F103](https://www.ampheo.com/search/STM32F103), along with implementation details: ![IMG_6953-1024x768](https://hackmd.io/_uploads/HkL54-cvgg.jpg) **1. Robotic Arm Joint Control (Servo/[Stepper Motors](https://www.onzuu.com/category/stepper-motors))** **Implementation** **Motors Used:** * SG90 servo motors (PWM position control) * NEMA 17 stepper motors (precision movement) **[STM32](https://www.ampheo.com/search/STM32) Setup:** ``` c // Servo control (50Hz PWM, 0.5-2.5ms pulse) void Set_Servo_Angle(TIM_HandleTypeDef *htim, uint32_t channel, float angle) { uint32_t pulse = 500 + (angle / 180.0) * 2000; // 500-2500μs __HAL_TIM_SET_COMPARE(htim, channel, pulse); } // Stepper control void Rotate_Stepper(uint16_t steps, uint8_t dir) { HAL_GPIO_WritePin(DIR_GPIO_Port, DIR_Pin, dir); for(uint16_t i=0; i<steps; i++) { HAL_GPIO_WritePin(STEP_GPIO_Port, STEP_Pin, 1); HAL_Delay(1); HAL_GPIO_WritePin(STEP_GPIO_Port, STEP_Pin, 0); HAL_Delay(1); } } ``` **Key Features:** * Inverse kinematics calculations for multi-axis coordination * End-stop switches for homing (GPIO interrupts) * UART/Bluetooth remote control **2. Electric Vehicle Wheel Control (BLDC Hub Motor)** **Implementation** **Components:** * 48V 1000W BLDC motor * 3-phase inverter ([DRV8301](https://www.onzuu.com/search/DRV8301)) * Throttle input (0-3.3V analog) **STM32 Setup:** ``` c // 6-step commutation table const uint8_t bldc_commutation[6] = {0b101, 0b100, 0b110, 0b010, 0b011, 0b001}; // Hall sensor interrupt void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { if(GPIO_Pin == HALL_U_Pin) { uint8_t hall_state = (HAL_GPIO_ReadPin(HALL_U_GPIO_Port, HALL_U_Pin) << 2) | (HAL_GPIO_ReadPin(HALL_V_GPIO_Port, HALL_V_Pin) << 1) | HAL_GPIO_ReadPin(HALL_W_GPIO_Port, HALL_W_Pin); BLDC_Commutation(hall_state); } } ``` **Key Features:** * Battery voltage monitoring (ADC) * Regenerative braking * CAN bus communication for vehicle network **3. CNC Machine Axis Control** **Implementation** **Motors Used:** * NEMA 23 steppers (high torque) * Closed-loop steppers with encoders **STM32 Setup:** ``` c // Linear interpolation for XY movement void Move_Linear(float x_target, float y_target, float feedrate) { float dx = x_target - current_x; float dy = y_target - current_y; float distance = sqrt(dx*dx + dy*dy); uint32_t steps = distance * steps_per_mm; float steps_x = dx * steps_per_mm; float steps_y = dy * steps_per_mm; for(uint32_t i=0; i<steps; i++) { if(i%(steps/steps_x) == 0) STEP_X(); if(i%(steps/steps_y) == 0) STEP_Y(); HAL_Delay(1000/(feedrate*steps_per_mm)); } } ``` **Key Features:** * G-code interpreter * Acceleration/deceleration profiles * Limit switch handling **4. Quadcopter Motor Control (BLDC)** **Implementation** **Components:** * 4x 2205 2300KV BLDC motors * 4-in-1 ESC (Electronic Speed Controller) * MPU6050 IMU **STM32 Setup:** ``` c // PWM output for ESCs (1-2ms pulse at 400Hz) void Set_Motor_Speeds(uint16_t m1, uint16_t m2, uint16_t m3, uint16_t m4) { __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, m1); __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, m2); __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, m3); __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_4, m4); } // PID stabilization loop void Flight_Control_Loop() { MPU6050_Read(); // Get gyro/accel data pitch_error = target_pitch - current_pitch; roll_error = target_roll - current_roll; // PID calculations pitch_output = Kp*pitch_error + Ki*pitch_integral + Kd*gyro_x; roll_output = Kp*roll_error + Ki*roll_integral + Kd*gyro_y; // Mix outputs to motors m1 = throttle + pitch_output + roll_output; m2 = throttle + pitch_output - roll_output; // ... etc } ``` **Key Features:** * Sensor fusion (Madgwick filter) * RF remote control ([NRF24L01](https://www.onzuu.com/search/NRF24L01)) * Battery monitoring **5. Conveyor Belt Speed Control** **Implementation** **Motor Used:** 12V DC gearmotor with encoder **STM32 Setup:** ``` c // Closed-loop speed control void Conveyor_Control(float target_speed) { static uint32_t last_encoder = 0; uint32_t current_encoder = __HAL_TIM_GET_COUNTER(&htim3); uint32_t delta = current_encoder - last_encoder; last_encoder = current_encoder; float actual_speed = (delta * 60.0) / (PPR * sample_time); // RPM PID_Control(target_speed, actual_speed); // Adjust PWM duty } ``` **Key Features:** * Infrared object detection * RS485 communication for industrial networks * Fault detection (jams, overloads) **6. 3D Printer Extruder Control** **Implementation** **Motor Used:** NEMA 17 with filament drive gear **STM32 Setup:** ``` c // Volumetric extrusion control void Extrude(float length) { uint32_t steps = length * steps_per_mm; float required_temp = 200.0; // °C for PLA while(actual_temp < required_temp) { Heat_Nozzle(PID_Calculate(required_temp, actual_temp)); HAL_Delay(100); } for(uint32_t i=0; i<steps; i++) { STEP_EXTRUDER(); HAL_Delay(1000/(feedrate*steps_per_mm)); } } ``` **Key Features:** * Thermistor temperature control * Filament runout sensor * Pressure advance algorithm **Development Tips** 1. Use Hardware PWM Timers (TIM1, TIM2) for smooth motor control 2. Implement Watchdog Timer for fault recovery 3. Add Current Sensing for overload protection: ``` c if(ADC_Read(MOTOR_CURRENT) > MAX_CURRENT) { Emergency_Stop(); } ``` 4. Use DMA for efficient [sensor](https://www.ampheoelec.de/c/sensors) data collection 5. Consider RTOS (FreeRTOS) for complex multi-motor systems Each application requires careful tuning of PID parameters and protection circuits.