I made an hx711 scale program and added a cable check but this code always gives the words "HX711 Error: Please check the connection." When I tested it on the device, the reading was immediately unstable, there was no load, the numbers immediately went up
WOKWI TEST
#include <HX711_ADC.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// HX711 Constructor (dout pin, sck pin)
HX711_ADC LoadCell(32, 33);
// LCD Configuration
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Calibration value
float calibrationValue = 9000.0; // This needs to be determined for your specific load cell
// Low Pass Filter Configuration
const int FILTER_WINDOW_SIZE = 10;
float weightBuffer[FILTER_WINDOW_SIZE];
int bufferIndex = 0;
bool bufferFilled = false;
unsigned long t = 0;
void setup() {
Serial.begin(115200);
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.clear();
// Initial LCD message
lcd.setCursor(0, 0);
lcd.print("Weight Scale");
lcd.setCursor(0, 1);
lcd.print("Initializing...");
delay(2000);
// Initialize LoadCell
LoadCell.begin();
LoadCell.start(2000); // 2 seconds stabilization time
// Check HX711 connection
lcd.clear();
lcd.print("Checking HX711...");
unsigned long start = millis();
while (!LoadCell.update() && millis() - start < 2000) {
delay(10); // Wait and keep updating
}
if (!LoadCell.update()) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("HX711 Error!");
lcd.setCursor(0, 1);
lcd.print("Check cables!");
Serial.println("HX711 Error: Please check the connection.");
while (1); // Halt the program
}
// Calibration sequence
lcd.clear();
lcd.print("Calibrating...");
LoadCell.setCalFactor(calibrationValue); // Set calibration value
// Perform tare (zero)
LoadCell.tare();
lcd.clear();
// Initialize weight buffer
for (int i = 0; i < FILTER_WINDOW_SIZE; i++) {
weightBuffer[i] = 0.0;
}
}
void loop() {
static float lastWeight = 0;
// Must be called at least once per loop for library to work
LoadCell.update();
// Get weight measurement
if (millis() - t > 100) { // Update every 100ms
float rawWeight = LoadCell.getData();
// Low Pass Filter: Moving Average
weightBuffer[bufferIndex] = rawWeight;
bufferIndex = (bufferIndex + 1) % FILTER_WINDOW_SIZE;
// Calculate filtered weight
float filteredWeight = 0;
for (int i = 0; i < FILTER_WINDOW_SIZE; i++) {
filteredWeight += weightBuffer[i];
}
filteredWeight /= (bufferFilled ? FILTER_WINDOW_SIZE : bufferIndex + 1);
// Mark buffer as filled after first complete cycle
if (bufferIndex == 0) {
bufferFilled = true;
}
// Only update LCD if weight change is significant
if (abs(filteredWeight - lastWeight) > 0.001) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Weight:");
lcd.setCursor(0, 1);
lcd.print(filteredWeight, 3);
lcd.print(" g");
// Optional: Serial output for debugging
Serial.print("Raw Weight: ");
Serial.print(rawWeight, 3);
Serial.print(" g, Filtered Weight: ");
Serial.print(filteredWeight, 3);
Serial.println(" g");
lastWeight = filteredWeight;
}
t = millis();
}
}