🛠 Intro to motor control with PWM

HPMicro HPM5100: 400 MHz RISC-V MCU for Motor Control and Analog Sensing · debutant · Apprenez à générer des signaux PWM sur le HPM5100 pour piloter la vitesse d'un moteur CC.

Objectifs

This tutorial guides you through configuring the HPM5100 PWM module to control the speed of a DC motor. You will learn to utilize the 32 available PWM channels and understand the principle of Pulse Width Modulation. This is an excellent introduction to the motor management capabilities of the RISC-V microcontroller.

Matériel nécessaire

Étapes

``c pwm_config_t config = { .period = 20000, // Example period .pulse = 10000, // 50% duty cycle }; pwm_set_config(HPM_PWM1, &config); pwm_enable(HPM_PWM1, PWM_CHANNEL_0, true); ``

``c while(1) { for(int duty = 0; duty <= 100; duty+=5) { pwm_set_duty(HPM_PWM1, PWM_CHANNEL_0, duty, 100); delay_ms(100); } // Reverse variation direction if necessary } ``

  1. Hardware Connection : Connect the PWM output pin of the HPM5100 to the Gate of the MOSFET. Connect the MOSFET drain to the motor positive pole. The MOSFET source goes to Ground (GND). The other motor pole goes to the external positive supply. Do not forget the flyback diode in parallel with the motor.
  2. Project Configuration : Open your IDE (e.g., VS Code with HPM plugin) and create a new project based on the drivers/pwm template.
  3. PWM Initialization : In your C code, initialize the PWM peripheral. The HPM5100 has synchronized timers. Configure the switching frequency (e.g., 20 kHz to avoid audible noise).
  4. Main Loop : Dynamically modify the pulse value to change speed. Use a variable that increases and decreases progressively.
  5. Compile and Flash : Compile the firmware with make and flash the board via USB.

Vérification

Observe the motor. It should start slowly, accelerate to maximum speed, then slow down. If using an LED, it should brighten gradually without visible flickering. If available, check with an oscilloscope that the signal is square and stable at 20 kHz.

Pour aller plus loin

Explore advanced features like complementary mode for H-bridges, or use FreeRTOS to create a task for speed control regulated by feedback via the integrated 16-bit ADCs.

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials