🛠 Controlling GPIOs and Peripherals on the Encoder Motor Driver Unit
Objectives
This advanced tutorial aims to demonstrate how to use the internal resources of the Encoder Motor Driver Unit (based on STM32G031) beyond simple motor control. We will configure the WS2812C RGB LED, read available GPIO inputs via the Grove interface, and understand the interaction between the microcontroller and the DRV8837 H-bridge. The goal is to create visual feedback and external control logic.
Required Hardware
- 1x M5Stack Encoder Motor Driver Unit (DRV8837DSGR)
- 1x M5Stack Controller (Core2, CoreS3, or Atom Lite with Grove)
- 1x Grove Cable
- 1x 6-wire Gear Motor with AB Encoder (3-15 PPR)
- 1x DC 6-12V Power Supply
- Arduino IDE with M5Stack and M5Unified libraries
Steps
Install necessary libraries. The unit uses a default I2C address of 0x2F. ``cpp #include <M5Unified.h> #include <M5_I2CEncoderMotor.h> ``
- Environment Configuration
Initialize the I2C bus and motor object. Verify connection with the STM32G031 microcontroller. ``cpp M5_I2CEncoderMotor motor; void setup() { M5.begin(); Wire.begin(); motor.init(0x2F); motor.setMotorMode(MOTOR_MODE_PWM); // Low level mode for GPIO test } ``
- Initialization and I2C Communication
The integrated LED serves as a status indicator. Use the dedicated function to change color based on speed or errors. ``cpp void loop() { // Example: Red LED if error, Green if active if(motor.isRunning()) { motor.setLedColor(0, 255, 0); // Green } else { motor.setLedColor(255, 0, 0); // Red } } ``
- RGB LED Control (WS2812C)
The unit exposes pins via the Grove connector. To read an external sensor (e.g., button), configure the corresponding pin as input. Note that the internal STM32 logic handles multiplexing. ``cpp // Generic example of reading via I2C bus if sensor is serially connected // Or direct use of Grove pins if mapped to main MCU ``
- Using GPIO via Grove
Implement a loop that adjusts PWM based on a simulated or real external input, while updating the LED. ``cpp int speed = 100; // Read a value (simulated or via I2C sensor) speed = constrain(speed + random(-10, 10), 0, 255); motor.setMotorSpeed(speed); delay(100); ``
- Advanced Control Loop
Verification
- The RGB LED changes color in real-time according to the motor state.
- The motor responds to speed changes sent via I2C.
- No I2C error messages appear in the serial monitor.
To Go Further
Explore the closed-loop position control mode using AB encoder data to regulate speed. You can also configure the I2C address (0x08-0x77) to manage multiple units on the same bus.
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