/* ESP32 + HX711 (Arduino framework) - DOUT (DT) -> GPIO 4 - SCK -> GPIO 5 - VCC HX711 -> 3V3 (recommandé) ou 5V si module compatible - GND -> GND */ /* Calibration (méthode simple) : poser une masse connue (ex. 1 kg), lire la valeur affichée, puis ajuster CALIBRATION_FACTOR jusqu’à obtenir la bonne valeur. Si l’affichage part en négatif quand tu ajoutes du poids, inverse le signe du facteur (ou inverse A+/A-). */ #include #include "HX711.h" static const int PIN_DOUT = 4; static const int PIN_SCK = 5; HX711 scale; // Ajuster après calibration static float CALIBRATION_FACTOR = -7050.0f; // exemple (le signe dépend du câblage) void waitReadyOrFail(uint32_t timeoutMs = 2000) { uint32_t start = millis(); while (!scale.is_ready()) { if (millis() - start > timeoutMs) { Serial.println("HX711 non prêt (timeout). Vérifier câblage/alimentation."); delay(1000); start = millis(); } delay(5); } } void setup() { Serial.begin(115200); delay(200); // (Optionnel) Fixer explicitement les modes pinMode(PIN_DOUT, INPUT); pinMode(PIN_SCK, OUTPUT); scale.begin(PIN_DOUT, PIN_SCK); // Si la lib le permet (selon version), choisir gain/canal : // scale.set_gain(128); Serial.println("Initialisation HX711..."); waitReadyOrFail(); Serial.println("Tare (ne rien mettre sur la balance)..."); scale.set_scale(); // échelle à 1 scale.tare(20); // moyenne sur 20 mesures Serial.println("Tare OK."); scale.set_scale(CALIBRATION_FACTOR); Serial.println("Lecture en cours. Utiliser une masse étalon pour calibrer."); } void loop() { waitReadyOrFail(); // Moyenne sur N lectures (réduit le bruit) const int N = 10; float units = scale.get_units(N); Serial.print("Poids (unités calibrées) : "); Serial.println(units, 3); delay(250); }