Another weight issue

Hi all
Im using an ESP32 devkit with a 50kg load cell and a spark fun HX711.
All is wired fine and when using the calibration code as follows I can get the correct weight from the load cell. i.e. 5kg

[code]
/*
 Example using the SparkFun HX711 breakout board with a scale
 By: Nathan Seidle
 SparkFun Electronics
 Date: November 19th, 2014
 License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
 
 This is the calibration sketch. Use it to determine the calibration_factor that the main example uses. It also
 outputs the zero_factor useful for projects that have a permanent mass on the scale in between power cycles.
 
 Setup your scale and start the sketch WITHOUT a weight on the scale
 Once readings are displayed place the weight on the scale
 Press +/- or a/z to adjust the calibration_factor until the output readings match the known weight
 Use this calibration_factor on the example sketch
 
 This example assumes pounds (lbs). If you prefer kilograms, change the Serial.print(" lbs"); line to kg. The
 calibration factor will be significantly different but it will be linearly related to lbs (1 lbs = 0.453592 kg).
 
 Your calibration factor may be very positive or very negative. It all depends on the setup of your scale system
 and the direction the sensors deflect from zero state

 This example code uses bogde's excellent library: https://github.com/bogde/HX711
 bogde's library is released under a GNU GENERAL PUBLIC LICENSE

 Arduino pin 2 -> HX711 CLK
 3 -> DOUT
 5V -> VCC
 GND -> GND
 
 Most any pin on the Arduino Uno will be compatible with DOUT/CLK.
 
 The HX711 board can be powered from 2.7V to 5V so the Arduino 5V power should be fine.
 
*/

#include "HX711.h" //This library can be obtained here http://librarymanager/All#Avia_HX711

#define LOADCELL_DOUT_PIN  23
#define LOADCELL_SCK_PIN  22

HX711 scale;

float calibration_factor = -40050; //-7050 worked for my 440lb max scale setup

void setup() {
  Serial.begin(9600);
  Serial.println("HX711 calibration sketch");
  Serial.println("Remove all weight from scale");
  Serial.println("After readings begin, place known weight on scale");
  Serial.println("Press + or a to increase calibration factor");
  Serial.println("Press - or z to decrease calibration factor");

  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale();
  scale.tare();	//Reset the scale to 0

  long zero_factor = scale.read_average(); //Get a baseline reading
  Serial.print("Zero factor: "); //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
  Serial.println(zero_factor);
}

void loop() {

  scale.set_scale(calibration_factor); //Adjust to this calibration factor

  Serial.print("Reading: ");
  Serial.print(scale.get_units(), 1);
  Serial.print(" Kgs"); //Change this to kg and re-adjust the calibration factor if you follow SI units like a sane person
  Serial.print(" calibration_factor: ");
  Serial.print(calibration_factor);
  Serial.println();

  delay(1000);

  if(Serial.available())
  {
    char temp = Serial.read();
    if(temp == '+' || temp == 'a')
      calibration_factor += 10;
    else if(temp == '-' || temp == 'z')
      calibration_factor -= 10;
  }
}
[/code]

When I then use the following code with the same calibration factor I get a different reading for the weight. See screen shot

[code]

// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
#include "DHT.h"
#include "HX711.h"

//#ifdef ESP32
//  #include <WiFi.h>
//  #include <HTTPClient.h>
//#else
//  #include <ESP8266WiFi.h>
//  #include <ESP8266HTTPClient.h>
//  #include <WiFiClient.h>
//#endif
//
//// Replace with your network credentials
//const char* ssid     = "";
//const char* password = "";
//
//// REPLACE with your Domain name and URL path or IP address with path
//const char* serverName = "";
//
//// Keep this API Key value to be compatible with the PHP code provided in the project page. 
//// If you change the apiKeyValue value, the PHP file /post-data.php also needs to have the same key 
//String apiKeyValue = "";

#define DHTPIN 4     // Digital pin connected to the DHT sensor

#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

#define ONE_WIRE_BUS 15 // Data wire is connected to GPIO15


#define LOADCELL_DOUT_PIN  23
#define LOADCELL_SCK_PIN  22

HX711 scale;

float calibration_factor = -40050; //-7050 worked for my 440lb max scale setup


// Setup a oneWire instance to communicate with a OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor 
DallasTemperature sensors(&oneWire);

DeviceAddress sensor1 = { 0x28, 0x6A, 0x48, 0x7C, 0x0C, 0x00, 0x00, 0xC4 };
DeviceAddress sensor2 = { 0x28, 0xFA, 0x14, 0x7B, 0x0C, 0x00, 0x00, 0x47 };

DHT dht(DHTPIN, DHTTYPE);



//// Time to sleep
//uint64_t uS_TO_S_FACTOR = 1000000;  // Conversion factor for micro seconds to seconds
//// sleep for 30 minutes = 1800 seconds
//uint64_t TIME_TO_SLEEP = 1800;

// Get Sensor Readings 
String getSensorReadings(){
  sensors.requestTemperatures();
} 
void setup() {
  Serial.begin(9600);
  sensors.begin();
  dht.begin();
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  //scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
 

//  WiFi.begin(ssid, password);
//  Serial.println("Connecting");
//  while(WiFi.status() != WL_CONNECTED) { 
//    delay(500);
//  Serial.print(".");
//  }
//  Serial.println("");
//  Serial.print("Connected to WiFi network with IP Address: ");
//  Serial.println(WiFi.localIP());
  
//#ifdef ESP32
//    // enable timer deep sleep
//    esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);    
//    Serial.println("Going to sleep now");
//    // start deep sleep for 3600 seconds (60 minutes)
//   
//  #else
//    // Deep sleep mode for 3600 seconds (60 minutes)
//    Serial.println("Going to sleep now");
//    ESP.deepSleep(TIME_TO_SLEEP * uS_TO_S_FACTOR); 
//  #endif
}

void loop(){ 
// //Check WiFi connection status
//  if(WiFi.status()== WL_CONNECTED){
//    HTTPClient http;
//    
//    // Your Domain name with URL path or IP address with path
//    http.begin(serverName);
//    
//    // Specify content-type header
//    http.addHeader("Content-Type", "application/x-www-form-urlencoded");
//    
//    // Prepare your HTTP POST request data
//    String httpRequestData = "api_key=" + apiKeyValue + "&value1=" + String(dht.readTemperature())
//                           + "&value2=" + String(dht.readHumidity()) + "&value3=" + String(sensors.getTempC(sensor1)) + "&value4=" + String(sensors.getTempC(sensor2)) + "&value5=" + String(scale.get_units())+"";
//    Serial.print("httpRequestData: ");
//    Serial.println(httpRequestData);
//    
//    // You can comment the httpRequestData variable above
//    // then, use the httpRequestData variable below (for testing purposes without the BME280 sensor)
//    //String httpRequestData = "api_key=&value1=24.75&value2=49.54&value3=1005.14";
//
//    // Send HTTP POST request
//    int httpResponseCode = http.POST(httpRequestData);
//     
//    // If you need an HTTP request with a content type: text/plain
//    //http.addHeader("Content-Type", "text/plain");
//    //int httpResponseCode = http.POST("Hello, World!");
//    
//    // If you need an HTTP request with a content type: application/json, use the following:
//    //http.addHeader("Content-Type", "application/json");
//    //int httpResponseCode = http.POST("{\"value1\":\"19\",\"value2\":\"67\",\"value3\":\"78\"}");
//    
//    if (httpResponseCode>0) {
//      Serial.print("HTTP Response code: ");
//      Serial.println(httpResponseCode);
//    }
//    else {
//      Serial.print("Error code: ");
//      Serial.println(httpResponseCode);
//    }
//    // Free resources
//    http.end();
//  }
//  else {
//    Serial.println("WiFi Disconnected");
//  }
//  //Send an HTTP POST request every 30 seconds
//  delay(30000);  
//   // Wait a few seconds between measurements.
//  delay(20000);
//  
  //Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); 
 // Serial.println("DONE");
  
//  Serial.print("8 Frame Hive: ");
//  Serial.print(sensors.getTempC(sensor1)); 
//  Serial.print("°C:    ");
//  
// 
//  Serial.print("10 Frame Hive: ");
//  Serial.print(sensors.getTempC(sensor2)); 
//  Serial.print("°C:    ");

  Serial.print("Weight ");
  Serial.print(scale.get_units(), 1);
  Serial.print(" Kgs      "); 

   // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println(F(    "Failed to read from DHT sensor!"));
    return;
  }
     
//  Serial.print(F("8 Frame Roof Temp: "));
//  Serial.print(t);
//  Serial.print(F("°C   "));
//  Serial.print(F("8 Frame Roof Humidity: "));
//  Serial.print(h);
//  Serial.print(F("%   "));
  Serial.print('\n');

  
//  esp_deep_sleep_start();
}
[/code]

Can some one help as to why this is happening.

I would print out the Raw HX711 output in both code examples and do the math to see what factor(s) are needed.

I would also test for repeatability with either code. As a rule load sensors are great for scale factor but the Zero tests to be much less stable, so for any calibration you should check the zero reading first.

Having said that you might find you need two calibration factors; zero and scale factor.

Do you have Excel or Libraoffice?

One difference I can see is that you are including additional libraries.
Your readings make me wonder if the arduino is seeing the HX711 at all.

I'd also agree with @JohnRob - print out the raw readings.

Thanks guys
Yes I have excel
How do I get the raw readings.
The extra libraries are for a full project I'm working on that will involve all these libraries.

I'm only guessing but I think

dth.read() will return the raw reading as a long

If you plot the two points in excel and add a trendline (mX+B) it will give you the gain and offset (tare) values.

Also know the HX711 has two inputs an A and B
The A channel has selectable gain
The B channel is fixed at gain = 32

You should sample a the slow rate 10 / sec. The way ΣΔ converters work the longer the conversion time the more noise is rejected.

so don't really know how to get raw data
Can I write a line of code that takes 208.2 off the reading to get it to the correct weight. As the weight readings are correct just that its 208.2 out...
How would I do that?

I'm not familiar with the library you are using but try this:

Your code comment says you're using "bogde's excellent library" and has a link to it. Look at the full example at that link. There's a raw read a few lines into setup().

So I've used the Full example with my Cal factor at -20600. The reading is now at -404.6. When I add 3kg it goes to -401.6 then add another 3kg it goes to -398.6. Then I added 2kg, weight goes to -396.6 and then another 2kg and its -394.5.
So the weight adding is correct its just off as a base value.
When I change the cal factor it goes way of both in start weight and added weight values.
There are raw values at top of the reading but I'm not smart enough to understand what they mean
Can't I simply add a line of code that adds the 404.6 to the scale reading to get the correct weight..
See screen shot

[code]
/**
 *
 * HX711 library for Arduino - example file
 * https://github.com/bogde/HX711
 *
 * MIT License
 * (c) 2018 Bogdan Necula
 *
**/
#include "HX711.h"


// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 2;
const int LOADCELL_SCK_PIN = 3;


HX711 scale;

void setup() {
  Serial.begin(38400);
  Serial.println("HX711 Demo");

  Serial.println("Initializing the scale");

  // Initialize library with data output pin, clock input pin and gain factor.
  // Channel selection is made by passing the appropriate gain:
  // - With a gain factor of 64 or 128, channel A is selected
  // - With a gain factor of 32, channel B is selected
  // By omitting the gain factor parameter, the library
  // default "128" (Channel A) is used here.
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);

  Serial.println("Before setting up the scale:");
  Serial.print("read: \t\t");
  Serial.println(scale.read());			// print a raw reading from the ADC

  Serial.print("read average: \t\t");
  Serial.println(scale.read_average(20));  	// print the average of 20 readings from the ADC

  Serial.print("get value: \t\t");
  Serial.println(scale.get_value(5));		// print the average of 5 readings from the ADC minus the tare weight (not set yet)

  Serial.print("get units: \t\t");
  Serial.println(scale.get_units(5), 1);	// print the average of 5 readings from the ADC minus tare weight (not set) divided
						// by the SCALE parameter (not set yet)

  scale.set_scale(2280.f);                      // this value is obtained by calibrating the scale with known weights; see the README for details
  scale.tare();				        // reset the scale to 0

  Serial.println("After setting up the scale:");

  Serial.print("read: \t\t");
  Serial.println(scale.read());                 // print a raw reading from the ADC

  Serial.print("read average: \t\t");
  Serial.println(scale.read_average(20));       // print the average of 20 readings from the ADC

  Serial.print("get value: \t\t");
  Serial.println(scale.get_value(5));		// print the average of 5 readings from the ADC minus the tare weight, set with tare()

  Serial.print("get units: \t\t");
  Serial.println(scale.get_units(5), 1);        // print the average of 5 readings from the ADC minus tare weight, divided
						// by the SCALE parameter set with set_scale

  Serial.println("Readings:");
}

void loop() {
  Serial.print("one reading:\t");
  Serial.print(scale.get_units(), 1);
  Serial.print("\t| average:\t");
  Serial.println(scale.get_units(10), 1);

  scale.power_down();			        // put the ADC in sleep mode
  delay(5000);
  scale.power_up();
}
[/code]

Hi All
Another weight issue here.
I have calibrated my 100kg load cell and have a cal value of -20600 using the code below.
The weight readout is as per the screen shot. The weight is out when there is no load on the scale but when I put a weight on the weight increase is correct. As in when I put 3kg on the increase is 3kg and so on.
Can I add a line to my code to add the 404.6 to the scale value so the the weight reading is correct?

If so how?

#include "HX711.h" 
#define LOADCELL_DOUT_PIN  23
#define LOADCELL_SCK_PIN  22

HX711 scale;

float calibration_factor = -20600; 

void setup() {
  Serial.begin(9600);
//  Serial.println("HX711 calibration sketch");
//  Serial.println("Remove all weight from scale");
//  Serial.println("After readings begin, place known weight on scale");
//  Serial.println("Press + or a to increase calibration factor");
//  Serial.println("Press - or z to decrease calibration factor");

  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale();
  scale.tare();	//Reset the scale to 0

  long zero_factor = scale.read_average(); //Get a baseline reading
  Serial.print("Zero factor: "); //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
  Serial.println(zero_factor);
}

void loop() {

  scale.set_scale(calibration_factor); //Adjust to this calibration factor

  Serial.print("Reading: ");
  Serial.print(scale.get_units(), 1);
  Serial.print(" Kgs"); //Change this to kg and re-adjust the calibration factor if you follow SI units like a sane person
  Serial.print(" calibration_factor: ");
  Serial.print(calibration_factor);
  Serial.println();

  delay(1000);

  if(Serial.available())
  {
    char temp = Serial.read();
    if(temp == '+' || temp == 'a')
      calibration_factor += 10;
    else if(temp == '-' || temp == 'z')
      calibration_factor -= 10;
  }
}

Answer 1 is probably.
Answer 2 who knows, you only show a snippet of the calibration code.
Does your load cell documentation show a graph of the resistance? If seems to be VERY non-linear.
Paul

If the scale shows -404.6 with nothing loaded, just add 404.6 to subsequent measurements. That is called "taring".

Once the scale reports 0.0 with nothing loaded, check the scaling factor by loading it with a known weight, and see if the scale reports the correct value.

You followed the instructions for calibrating the device here, which is missing from your code ? :