🛠 GPIO and RGB LED Control on the 4-Encoder Motor Driver
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
- M5Stack 4-Encoder Motor Driver module (DRV8837DSGR)
- M5Stack compatible host board (Core2, CoreS3, etc.) or ESP32 with M5Unified library
- Grove HY2.0-4P cables
- A push button or digital sensor connected to a Grove port
- External 6-12V DC power supply (recommended for motor tests)
Steps
- 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.
- Software Initialization: In your Arduino code, include the
M5Stack_M5Stacklibrary. Initialize the module withM5.begin()and start the motor driver withM5.M5Stack.begin(). Verify that I2C communication (address 0x24) is active.
- 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.
- 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.
- 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
- First Connection and Startup of the 4-Encoder Motor Driver
- Discovering I/O: RGB LED and Encoder Reading
- Automate a Robot Chassis with the 4-Encoder Driver
- First connection and startup of the 4-Encoder Motor Driver
- Closed-Loop Automation with the 4-Encoder Driver
- First wiring and initialization of the 4-Encoder Motor Driver
Code examples for this board
i2c-motor-spin.inoarduinoclosed-loop-speed-control.inoarduinorelative-position-move.inoarduinorgb-led-status-indicator.inoarduinodifferential-drive-robot.inoarduinoi2c_basic_motor_control.pyPython