🛠 Automating Precise Movement with the Encoder Motor Driver
Objectives
This tutorial aims to automate a DC motor movement using closed-loop control. Unlike simple PWM mode, closed-loop control uses encoder data to correct speed in real-time, ensuring positioning accuracy even under variable load. We will configure the unit to reach a specific target position.
Required Materials
- 1x M5Stack Encoder Motor Driver Unit (DRV8837DSGR)
- 1x M5Stack Core or Core2 (I2C Master)
- 1x 6-wire Gear Motor (AB encoder compatible)
- 1x Grove Cable
- External DC 6-12V Power Supply (connected to the unit)
- Motor connection cables (to M+ and M- ports)
Steps
- Physical Connection: Connect the motor to the motor terminals of the unit. Ensure the encoder wires (usually center or dedicated wires depending on the model) are correctly connected to the unit's encoder ports. Connect the unit to the master via the Grove cable (I2C). Provide external power (6-12V).
- Software Configuration: In your IDE (Arduino or UIFlow), initialize I2C communication. The default address is 0x2F. If using UIFlow, add the 'Encoder Motor Driver' block.
- Parameter Definition: Configure the PPR (Pulses Per Revolution) of your encoder (between 3 and 15). For example, if your motor has a 12 PPR encoder, enter this value. Define the target speed in RPM.
- Position Command: Send a command to move the motor to an absolute or relative position. For example, ask the motor to rotate 360 degrees. The driver will use the DRV8837 H-bridge to adjust current and speed to reach the target.
- Status Monitoring: Use read functions to check the current position and actual speed. The RGB LED can be configured to indicate status (e.g., blue while moving, green when target is reached).
Verification
Run the program. The motor should rotate and stop precisely at the requested position. If you slightly block the motor with your hand, the driver should increase current to maintain position (thanks to INA199A detection). Verify that speed remains stable despite load variations.
Further Exploration
Explore the 'soft start/stop' mode to avoid mechanical shocks. You can also change the I2C address to connect multiple units on the same bus and create a multi-axis positioning system.
Other tutorials for this board
Code examples for this board
m5stack-encoder-motor-i2c-esp32.inoC++ (Arduino)m5stack-encoder-motor-pwm-control.inoC++ (Arduino)m5stack-encoder-motor-closed-loop.inoC++ (Arduino)m5stack-encoder-motor-position-control.inoC++ (Arduino)m5stack-encoder-motor-led-status.inoC++ (Arduino)closed_loop_speed_pid.pyMicroPython