🛠 Distributed Automation: Precise Control and Telemetry with the Encoder Motor Driver Unit
Objectives
This advanced tutorial aims to design an autonomous automation node using the Encoder Motor Driver Unit (DRV8837DSGR). The goal is to move beyond manual control to implement a closed-loop (PID) strategy based on encoder data, while integrating the card into an IoT network for telemetry. We will leverage the INA199A current sensing capabilities for motor protection and the RGB LED for local status indication.
Required Hardware
- M5Stack compatible main board (e.g., Core2 or AtomS3) with I2C support.
- Encoder Motor Driver Unit (DRV8837DSGR).
- M5Stack gear motor (6-wire compatible with AB encoder).
- External DC 6-12V power supply.
- Grove I2C cable.
- M5Stack library for Arduino/PlatformIO.
- MQTT broker or REST API for data reception.
Steps
Connect the unit to the Grove I2C port of your main board. Ensure the external power supply is connected to the unit's DC connector (6-12V). By default, the I2C address is 0x2F. For a multi-node network, you must configure a unique address (between 0x08 and 0x77) via AD0/AD1 lines if available, or via the initial software command if the firmware allows, to avoid bus conflicts.
- Hardware Configuration and Addressing
Initialize I2C communication in the code. Configure the operating mode to "Closed Loop". Calibrate the encoder resolution. The unit supports encoders from 3 to 15 PPR (Pulses Per Revolution). Define the PPR constant according to your specific motor to convert pulses into degrees or revolutions.
- Initialization and Encoder Calibration
Do not just send a speed setpoint. Read position and speed data via I2C registers. Implement a simple PID algorithm on the main board: calculate the error between the target position and the current position, then adjust the PWM setpoint sent to the unit. Use "Progressive Start/Stop" modes to avoid mechanical shocks during setpoint changes.
- PID Control Loop Implementation
Every 500ms, read status registers: current consumption (INA199A), current speed, and position. Publish this data to an MQTT broker. Use the current sensing circuit to define a safety threshold: if current exceeds 1.2A (load limit), immediately stop the motor to prevent overheating.
- Network Integration and Telemetry
Drive the integrated WS2812C LED to reflect system status: Green (nominal operation), Red (position error or overcurrent), Blue (active network connection). This allows for immediate visual diagnosis without a screen.
- Visual Feedback via RGB LED
Verification
- Verify that the motor reaches the target position with less than 5% precision.
- Confirm that the LED changes color during a mechanical jam (current increase detected by the INA199A).
- Ensure that MQTT packets arrive at the server with correct position coordinates.
To Go Further
Explore multi-axis synchronization using multiple units on the same I2C bus with distinct addresses. Integrate a Kalman filter algorithm to smooth AB encoder data and improve PID control stability at high speeds.
Other tutorials for this board
- First connection and startup of the Encoder Motor Driver Unit
- Mastering I/O with the Encoder Motor Driver Unit
- Automating Precise Movement with the Encoder Motor Driver
- First Setup of the Encoder Motor Driver
- GPIO and RGB LED Control on the Encoder Motor Driver Unit
- Encoder feedback automation with DRV8837
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