Scale doesn't calibrate correctly

First of all excuse my english, but I have an Arduino Uno and I'm trying to make a scale, It already works besides the fact that it doesn't make a good calibration. It's connected to a loadcell and I already have the weightfactor (1kg=813900.0, and 2kg=153711.0)

could you guys help me find what I'm missing? I put the code below.

thank you for your help in advance!

#include "HX711.h"
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

const int DOUT_PIN = 2; // DAT-pin van de HX711 aangesloten op digitale pin 2
const int SCK_PIN = 3; // CLK-pin van de HX711 aangesloten op digitale pin 3

HX711 scale;
LiquidCrystal_I2C lcd(0x27, 20, 4);

const float scaleFactor1kg = 813900.0; // Vervang dit met je eigen weegschaalfactor voor 1kg
const float scaleFactor2kg = 153711.0;      // Vul de weegschaalfactor voor 2kg in wanneer beschikbaar
const float offset = 0.0;               // Offset, pas aan indien nodig

void setup() {
  Serial.begin(9600);
  scale.begin(DOUT_PIN, SCK_PIN);

  // Initialiseer het lcd-scherm
  lcd.init();
  // Zet de achtergrondverlichting aan
  lcd.backlight();

  lcd.setCursor(0, 0);
  lcd.print("Weegschaal");

  delay(2000);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Voeg gewicht toe");
}

void loop() {
  if (scale.is_ready()) {
    long rawValue = scale.get_value(); // Lees de ruwe sensorwaarde

    // Voer de lineaire vergelijking uit
    float weight = (rawValue / scaleFactor1kg) - offset;

    lcd.setCursor(0, 1);
    lcd.print("Gewicht: ");
    lcd.print(weight, 3); // Toon het gewicht met twee decimalen
    lcd.print(" kg      ");

    // Optioneel: Serial monitor voor het weergeven van het gewicht in de seriële monitor
    Serial.print("Ruwe waarde: ");
    Serial.print(rawValue);
    Serial.print(", Gewicht: ");
    Serial.print(weight, 3); // Toon het gewicht met twee decimalen
    Serial.println(" kg");
  } else {
    lcd.setCursor(0, 1);
    lcd.print("Fout bij het lezen");
  }

  delay(500); // Wacht 0,5 seconden voordat de volgende meting wordt uitgevoerd
}

You don't have any calibration function, you have hardcoded magic values. And from the 2 magic values you use only one.

Often, one subtracts a tare offset from the raw number before one applies a scale factor.

Looking at your two raw values,

I would guess the raw value for zero to be 813900.0 + (813900.0-153711.0) = 1474089.

You should plot them out on a graph of weight vs raw reading.

I think you'd do better by taking the raw number, subtracting a 1474089 offset, and then dividing by a (153711.0 - 813900.0) scale factor.

1 Like

My raw value for zero is 76552, will it still be the same? and if so does it mean this is better in the code written down here?
Now when I add 2kg it says its 19,2KG and 0kg is being read as 1,1kg. Should I adapt the offset or the scalefactor?


const float scaleFactor0kg = 76552.0; // Vervang dit met je eigen weegschaalfactor voor 0kg
const float offset = 0.0;               // Offset, pas aan indien nodig

How could I add a calibration function?

The scale should be linear, so a two point calibration should be fine.

I'd add "tare" process for the scale at power on and save the unloaded raw value in offset at power on, then prompt for placing a known calibration weight and use that to set the calibration factor

   void setup() {
      ...
     delay(1000);
     offset = scale.get_value(); // save the zero reading
     lcd.setCursor(0, 0);
     lcd.print("Calib. w 1kg");
     delay(2000);
     scaleFactor1kg = (scale.get_value() - offset);  
     ...
}

and then subtract the offset later:

void loop() {
  if (scale.is_ready()) {
    long rawValue = scale.get_value(); // Lees de ruwe sensorwaarde
    // Voer de lineaire vergelijking uit
    float weight = (rawValue - offset) / scaleFactor1kg;
    ...
 

If you measure the offset in raw units, you should subtract it before you convert the raw values to useful units.

Could you provide links to all the parts you are using, and a schematic?

I assume this are the raw values that you measured when placing a calibration weight of 1 kg resp. 2 kg on your scale, if not let us know exactly how you got those values.

Basic maths gives you a line through those two points: (x = 1, y = 813900) and (x = 2, y = 153711).

From that you get a slope of (153711 - 813900) / (2 - 1) = -660189 (mind the negative sign), this would be your scaleFactor1kg, and an offset of 1474089.

There is also the mechanics - cheap load cells drift and the mechanics of the installation can give you significant non linearity and hysteresis .
The electrical side , wiring , power supplies , load cell “amplifier/ADC “ can have an effect .

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