🛠 GPIO and RGB LED Control on the 4-Encoder Motor Driver

Unit 4-Encoder Motor Driver (DRV8837DSGR) · intermediaire · Apprenez à utiliser les entrées-sorties et la LED RGB WS2812C intégrée au module DRV8837DSGR via I2C.

Objectives

This tutorial aims to master the Input/Output (GPIO) and light display features of the M5Stack 4-Encoder Motor Driver module. Unlike standard drivers, this module integrates an STM32F030C8T6 microcontroller that manages peripherals. We will configure the Grove ports to read logic states and control the integrated WS2812C RGB LED to display system status, while maintaining motor control via I2C.

Required Materials

Steps

  1. Hardware Connection: Insert the module into the host board's Grove port. Connect a push button to one of the two available Grove HY2.0-4P ports on the module. Ensure the module is powered via the Grove port (5V) or the external DC input.
  1. Software Initialization: In your Arduino code, include the M5Stack_M5Stack library. Initialize the module with M5.begin() and start the motor driver with M5.M5Stack.begin(). Verify that I2C communication (address 0x24) is active.
  1. GPIO Configuration: The module exposes GPIO lines via its Grove ports. Use specific driver functions to set pin directions. For example, configure the pin associated with the used Grove port as a digital input (INPUT_PULLUP) to read the button state.
  1. RGB LED Control: The WS2812C LED is addressable via the I2C bus. Send a command to set the color. For example, turn the LED green when the button is released, and red when pressed. Use the setRGB(r, g, b) function provided by the driver library.
  1. Reading Loop: In the loop() function, read the GPIO state connected to the button. Update the LED color accordingly. Add a 10ms delay to avoid mechanical bouncing and I2C bus saturation.

Verification

Open the serial monitor at 115200 bauds. Display the button state read by the GPIO. Press the button: the module's LED should change color instantly. Verify that motors remain stationary if no movement command is sent, confirming that GPIO configuration does not affect the H-bridges.

Going Further

Explore the use of the integrated INA199A current sensor. You can read the current consumed by each motor and make the RGB LED blink based on the detected load, creating a visual diagnostic system for your robots.

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials