🛠 Controlling GPIOs and Peripherals on the Encoder Motor Driver Unit

M5Stack Encoder Motor Driver Unit (DRV8837DSGR) · avance · Exploitez les capacités avancées de l'Encoder Motor Driver Unit pour gérer les entrées-sorties, la LED RGB et l'affichage via son microcontrôleur STM32.

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

Steps

Install necessary libraries. The unit uses a default I2C address of 0x2F. ``cpp #include <M5Unified.h> #include <M5_I2CEncoderMotor.h> ``

  1. 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 } ``

  1. 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 } } ``

  1. 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 ``

  1. 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); ``

  1. Advanced Control Loop

Verification

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

Code examples for this board

← Back to the board 🛠 All tutorials