🛠 GPIO and RGB LED Control on the Encoder Motor Driver Unit

M5Stack Encoder Motor Driver Unit (DRV8837DSGR) · intermediaire · Apprenez à utiliser les extensions GPIO de l'unité pour piloter des périphériques externes tout en gérant le moteur.

Objectives

This tutorial aims to exploit the input/output capabilities of the Encoder Motor Driver Unit (EMDU) beyond simple motor control. We will configure available GPIO pins to interact with simple sensors or actuators, while synchronizing the motor state with the integrated RGB LED (WS2812C). The goal is to create a system where the LED color reflects the motor state (red for stop, green for run, blue for error or high current detection), demonstrating the versatility of the I2C interface for peripheral control.

Required Hardware

Steps

  1. Hardware and Connection: Mount the EMDU on the Grove I2C bus of your M5Stack controller. Connect the motor to the EMDU respecting polarity. Connect the external 6-12V power supply to the EMDU. Verify the motor spins freely.
  1. Software Configuration: Install the M5Stack library for Arduino. Include the specific EMDU library. Initialize I2C communication with the default address 0x2F.
#include <M5Unified.h>
#include <M5_EncoderMotorDriver.h>

M5_EncoderMotorDriver emd;

void setup() {
  M5.begin();
  Wire.begin();
  if (!emd.begin(0x2F)) {
    Serial.println("EMDU initialization error");
    while(1);
  }
  emd.setMode(MODE_CLOSED_LOOP_SPEED); // Closed loop speed
}
  1. RGB LED Management: The WS2812C LED is controlled via specific I2C commands. Write a function to change the color based on the state.
void setLedStatus(uint8_t r, uint8_t g, uint8_t b) {
  emd.setRgbLed(r, g, b); // Dedicated I2C command
}
  1. Control Logic: In the loop(), read the speed measured by the encoder. If speed is zero, light the LED red. If speed is stable, light it green. Simulate high current detection (via reading the INA199A register if exposed) to light the LED blue.
void loop() {
  int speed = emd.getSpeed();
  if (speed == 0) {
    setLedStatus(255, 0, 0); // Red
    emd.stopMotor();
  } else {
    setLedStatus(0, 255, 0); // Green
    emd.setSpeed(100); // Target speed
  }
  delay(100);
}
  1. Testing Inputs (Optional): If the unit exposes GPIOs via I2C registers for digital inputs (e.g., limit switches), configure the GPIO direction register. Read the state via emd.readGpioState() and change the LED color accordingly (e.g., blue if button pressed).

Verification

Open the serial monitor. Verify that the LED lights up correctly according to motor state changes. Disconnect the motor to simulate an error or stop, observe the color change. Ensure no I2C errors appear in the console.

To Go Further

Explore the use of hardware interrupts if available to react instantly to GPIO state changes without polling. Integrate an external temperature sensor connected via a separate Grove I2C module and correlate temperature with motor speed for advanced thermal management.

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials