Hx711 scale program and added a cable check error

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();
  }
}

Show your circuit wiring. Are the devices connected correctly? Also check the power for the circuit - correct voltage? enough current?

I use the Rob Tillard HX711 library, it works.

Are you using the simulator with a noiseless sensor?

LoadCell.start() tares the device to 0. LoadCellUpdate()'s return value is the reading, which, if still 0, will match on the if (!LoadCell.update()) test. oops.

update() is a one-shot test, and if it triggered on the last check, it started a new conversion and won't be ready for a new update yet.

I've tested it and the cable works fine but when this sign! I delete the if (!LoadCell.update()) it's normal

I already know my code. When the cable is unplugged or plugged in it still gives a cable error message

You are saying nothing is wrong with your code or your wiring? Fixed?

Did you swap 32 and 33?

I just looked at the library and it turns out that what is called to check is not !LoadCell.update()

  LoadCell.begin();
  unsigned long stabilizingtime = 2000; 
  boolean _tare = true; 
  
  LoadCell.start(stabilizingtime, _tare);
  
  if (LoadCell.getTareTimeoutFlag()) {
    lcd.clear();
    lcd.print("HX711 ERROR");
    Serial.println("Timeout, check MCU>HX711 wiring");
  }
  else {
    LoadCell.setCalFactor(calibrationValue);
    lcd.clear();
    lcd.print("Startup Complete");
  }
  
  while (!LoadCell.update());

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.