🛠 Closed-Loop Automation with the 4-Encoder Driver
Objectives
This tutorial aims to implement precise automation using the M5Stack 4-Encoder Motor Driver module. We will configure closed-loop position control for a motor, simulate a simple industrial automation sequence (move, wait, return), and discuss position data management. This intermediate level assumes a basic understanding of the I2C protocol and programming in C++ or Python.
Required Materials
- M5Stack 4-Encoder Motor Driver Unit (DRV8837DSGR)
- One 6-wire geared motor with encoder (PPR between 3 and 15)
- External DC power supply 6-12V (recommended for loads under 1.2A)
- Grove HY2.0-4P and PH2.0-6P cables
- I2C-compatible host microcontroller (e.g., M5Stack Core, ESP32, Arduino)
- IDE software with M5Stack library or direct I2C register access
Steps
- Physical Connection: Connect the motor to PH2.0-6P port #1. Ensure encoder wires are correctly aligned with dedicated pins. Connect the external DC power to the appropriate connector to guarantee necessary current (up to 1.2A per channel).
- I2C Initialization: In your code, initialize the I2C bus. The module's default address is
0x24. Verify communication by reading an identification or status register. - Mode Configuration: Write to configuration registers to enable "Position Control" mode (closed-loop) on motor channel 1. Define PID parameters if your library allows, or use default values for a first test.
- Automation Sequence: Implement a function that sends a position setpoint (in encoder ticks). Wait for the status register to indicate the motor has reached the target position ("Done" bit or equivalent). Add a 2-second delay to simulate an industrial task.
- Return to Origin: Send a position setpoint of 0 to return the motor to its initial position. Verify smooth movement thanks to the integrated soft start/stop.
- Data Reading: Periodically read the current position and consumed current (via the integrated INA199A sensor) to monitor system status.
Verification
The motor should move to the target position, stop precisely, wait, and then return to zero. The WS2812C RGB LED can serve as a visual indicator (e.g., green while moving, blue while waiting). If the motor vibrates or does not stop, check encoder wiring and PID gain parameters.
Further Exploration
Explore sleep mode to save energy between automation cycles. You can also extend this system to multiple motors to create a simple robotic arm, synchronizing movements via sequential or parallel I2C commands.
Other tutorials for this board
- First Connection and Startup of the 4-Encoder Motor Driver
- Discovering I/O: RGB LED and Encoder Reading
- Automate a Robot Chassis with the 4-Encoder Driver
- First connection and startup of the 4-Encoder Motor Driver
- GPIO and RGB LED Control on the 4-Encoder Motor Driver
- First wiring and initialization of the 4-Encoder Motor Driver
Code examples for this board
i2c-motor-spin.inoarduinoclosed-loop-speed-control.inoarduinorelative-position-move.inoarduinorgb-led-status-indicator.inoarduinodifferential-drive-robot.inoarduinoi2c_basic_motor_control.pyPython