i2c-motor-spin.ino

Unit 4-Encoder Motor Driver (DRV8837DSGR) · File : i2c-motor-spin.ino · Language : arduino · Use case : Faire tourner un moteur DC sur le canal 1 à vitesse constante.

Minimalist example to initialize I2C communication and send a speed command to the first motor.

Uses the default I2C bus (GPIO 21/22 on ESP32/M5Stack). Verify that address 0x24 is correct. This code is a skeleton; for precise control (PID, position), use the official M5Stack_MotorDriverUnit library.

i2c-motor-spin.ino

#include <Wire.h>

#define UNIT_ADDR 0x24
#define MOTOR_CH 1 // 1-4
#define SPEED 100  // 0-100%

void setup() {
  Serial.begin(115200);
  Wire.begin();
  Serial.println("Starting Motor Driver Unit...");
}

void loop() {
  // Commande simplifiée via registre de contrôle (exemple générique M5Stack)
  // Note : L'API réelle nécessite souvent la lib M5Stack_MotorDriverUnit
  // Ici, simulation d'une écriture bas niveau pour l'exemple
  Wire.beginTransmission(UNIT_ADDR);
  Wire.write(0x01); // Registre de commande
  Wire.write(MOTOR_CH);
  Wire.write(SPEED); // Vitesse positive = avance
  Wire.endTransmission();
  delay(1000);
}

← Back to the board