I am working on an ESP32-based BMI machine using HX711 for weight measurement, TFT_eSPI for display, and a BluetoothSerial interface. I have implemented an `auto_calibration()` function that is supposed to calibrate the HX711 on startup.
However, I am facing a strange issue:
The `auto_calibration()` function keeps looping even if the weight scale is physically disconnected.
The display continuously shows **"Please Wait" → "Initializing"** without stopping.
It seems the `hx_read` value is always above the `raw_initial` threshold, which keeps triggering the `error_log()` function, resulting in a loop overflow.
Here is the relevant part of my code:
Code: Select all
void auto_calibration() {
float factor_add = 0;
unsigned long compare = 0;
tft.fillScreen(TFT_BLACK);
cr_y = 115;
tft_print_wrapped("Please Wait");
delay(500);
tft_print_wrapped("Initializing");
delay(500);
for (i = 0; i < 10; i++) {
hx_read = read();
}
if (hx_read > raw_initial) {
error_log();
} else {
for (i = 0; i < 10; i++) {
hx_data1 = read();
}
compare = hx_data1 - offset;
if (compare > 3000 || compare < 0) {
offset = offset + compare - 10000;
for (i = 0; i < 10; i++) w_adc = read();
factor_add = (w_adc - offset) / w_factor;
w_factor += factor_add;
tft.fillScreen(TFT_BLACK);
cr_y = 115;
tft_print_wrapped("Finished");
delay(500);
}
}
}1. How can I make sure `auto_calibration()` exits gracefully when the HX711 or weight scale is not connected?
2. Should I add a timeout or a safety check before going into `error_log()`?
3. Is there a better way to verify that the HX711 is connected and returning valid data before running calibration?
Any advice, sample code, or best practices for handling HX711 initialization on ESP32 will be greatly appreciated.
Thank you!