current_sensing_ina199.ino

Unit 4-Encoder Motor Driver (DRV8837DSGR) · File : current_sensing_ina199.ino · Language : C++ · Use case : Lecture du capteur de courant INA199 pour la protection contre les surcharges.

Real-time monitoring of motor current consumption to detect stalls or overloads and trigger a stop.

The module integrates an INA199A. The raw ADC to Ampere conversion depends on the specific shunt resistors and gain used on the board. Consult the electrical schematic to calibrate the conversion constant.

current_sensing_ina199.ino

#include <Wire.h>

#define ADDR 0x24
#define REG_CURRENT_M1 0x20 // Adresse supposée pour le courant Moteur 1

void setup() {
  Wire.begin();
  Serial.begin(115200);
}

void loop() {
  float current = readCurrent();
  Serial.print("Courant Moteur 1: ");
  Serial.print(current, 3);
  Serial.println(" A");
  
  if (current > 1.0) {
    Serial.println("SURCHARGE ! Arrêt moteur.");
    stopMotor();
  }
  
  delay(100);
}

float readCurrent() {
  Wire.beginTransmission(ADDR);
  Wire.write(REG_CURRENT_M1);
  Wire.endTransmission();
  
  Wire.requestFrom(ADDR, 2); // Lecture 2 octets (16 bits)
  if (Wire.available() == 2) {
    int raw = Wire.read();
    raw |= Wire.read() << 8;
    
    // Conversion ADC en Ampères
    // INA199 gain typique et résistance shunt à définir selon le schéma
    // Exemple : Vout = I * Rshunt * Gain
    // ADC 12 bits, Vref 3.3V
    float voltage = (raw / 4095.0) * 3.3;
    float current = voltage / 0.1; // Exemple : Gain 10, Rshunt 0.01 Ohm
    return current;
  }
  return 0.0;
}

void stopMotor() {
  // Code d'arrêt d'urgence
}

← Back to the board