current_monitoring_safety.py

M5Stack Encoder Motor Driver Unit (DRV8837DSGR) · File : current_monitoring_safety.py · Language : MicroPython · Use case : Surveillance du courant moteur en temps réel pour déclencher un arrêt d'urgence en cas de surcharge.

Continuous monitoring of motor current consumption to protect the mechanism against stalls or overloads.

Reads the current value measured by the INA199A amplifier via I2C. If the current exceeds the defined threshold, PWM is set to zero to stop the motor. This mechanism is essential for gear longevity and system safety.

current_monitoring_safety.py

import time
from m5stack import i2c

ADDR = 0x2F
bus = i2c.I2C(1)
MAX_CURRENT_MA = 1000 # Seuil de sécurité

while True:
    # Lecture du courant (INA199A) via registre dédié (ex: 0x15, 2 octets)
    current_data = bus.readfrom_mem(ADDR, 0x15, 2)
    current_ma = int.from_bytes(current_data, 'little')
    
    print(f"Courant: {current_ma} mA")
    
    if current_ma > MAX_CURRENT_MA:
        print("URGENCE: Surcharge détectée, arrêt moteur")
        # Arrêt immédiat (PWM 0)
        bus.writeto_mem(ADDR, 0x01, bytes([0]))
        # Optionnel: Allumer LED Rouge
        bus.writeto_mem(ADDR, 0x30, bytes([255]))
        break
    
    time.sleep(0.1)

← Back to the board