🛠 Mastering I/O with the Encoder Motor Driver Unit
Objectives
This tutorial aims to understand the I/O interfaces of the Encoder Motor Driver Unit (DRV8837DSGR). We will configure I2C communication to control the integrated WS2812C programmable RGB LED and read encoder pulses to estimate motor position. This introductory level requires a basic understanding of serial communication protocols.
Required Hardware
- A M5Stack compatible development board (Core2, StickC Plus2, or Stack)
- The Encoder Motor Driver Unit (DRV8837DSGR)
- A 6-wire geared motor with AB encoder
- Grove cables or compatible connectors
- Arduino IDE or M5Burner software
Steps
- Library Installation: In Arduino IDE, install the official
M5Stacklibrary and the dedicatedM5EncoderMotorDriverlibrary. These libraries handle the internal registers of the STM32G031. - Physical Connection: Connect the unit to a Grove I2C port (G16/G17 or G21/G22 depending on your board). Ensure the external DC 6-12V power supply is connected to the dedicated connector, as the motor requires more current than the Grove port alone can provide.
- I2C Initialization: In the code, initialize the I2C bus. The driver's default address is 0x2F. Verify the connection via an I2C scan function if necessary.
- RGB LED Configuration: Use the function to set the color of the WS2812C LED. For example, set the LED to red to indicate the motor is ready. The LED serves as a simple visual status indicator here.
- Encoder Reading: Configure the driver to read AB encoder pulses. Although closed-loop control is advanced, we read the raw counter value here to observe position changes.
Verification
Upload the code. The unit's LED should light up red. Turn the motor by hand or with a slow rotation command. Open the Serial Monitor (115200 bauds). You should see encoder values increasing or decreasing depending on the rotation direction, confirming that the encoder inputs and LED output work correctly via I2C.
Going Further
Explore closed-loop speed control using encoder data to automatically adjust PWM, or use the INA199A current detection to monitor motor load.
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