🛠 Encoder feedback automation with DRV8837

M5Stack Encoder Motor Driver Unit (DRV8837DSGR) · intermediaire · Mettez en place une boucle de régulation PID pour maintenir une position précise en temps réel.

Objectives

This tutorial aims to implement a closed-loop control loop to reach and maintain a precise target position. We will use AB encoder data to correct motor speed via a simple PID algorithm, illustrating a basic automation application.

Required Hardware

Steps

  1. Physical Connection: Connect the board to the I2C bus via the Grove port. Ensure external power (6-12V) is connected for the motor, as the load can reach 1.2A.
  2. Software Configuration: Initialize I2C communication with the default address 0x2F. Configure the encoder's PPR (e.g., 12 PPR) in the configuration register.
  3. Encoder Reading: Implement a function that reads the current position by accumulating A and B pulses. Resolution depends on PPR and counting mode (1x, 2x, or 4x).
  4. PID Algorithm: Write a PID calculation function. The error is the difference between target position and current position. Calculate proportional, integral, and derivative outputs.
  5. PWM Command: Convert PID output to a PWM value for the DRV8837 H-bridge. Limit the value to respect the max current of 1.2A.
  6. Main Loop: In your loop, read position, calculate PID correction, and apply PWM. Repeat at a high frequency (e.g., 100 Hz) for optimal stability.

Verification

Try to manually block the motor. The system should increase power to reach the target. Check stability once the position is reached; slight oscillation is normal but should be damped by the derivative term.

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials