#include "HX711.h"
const int LOADCELL_DOUT_PIN = 3;
const int LOADCELL_SCK_PIN = 2;
HX711 scale;
float calibration_factor = 8000; // Start with a rough guess
float known_weight = 3.0; // Your known weight (in kg)
float tolerance = 0.05; // Acceptable error margin
void setup() {
Serial.begin(9600);
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
Serial.println("HX711 Calibration");
scale.set_scale();
scale.tare(); // Reset scale to zero
Serial.println("Starting automatic calibration...");
}
void loop() {
// Adjust the calibration factor
scale.set_scale(calibration_factor);
// Get weight reading
float weight = scale.get_units();
Serial.print("Current Reading: ");
Serial.print(weight);
Serial.print(" kg | Calibration factor: ");
Serial.println(calibration_factor);
// Automatically adjust calibration factor to match the known weight
if (weight < known_weight - tolerance) {
calibration_factor += 10; // Increase calibration factor
} else if (weight > known_weight + tolerance) {
calibration_factor -= 10; // Decrease calibration factor
} else {
Serial.println("Calibration complete.");
while(1); // Stop the program when calibration is done
}
delay(500);
}