Hii Everyone,I'm unable to get correct values of proportional weight w.r.t the actual weight using HX711 module with a3kg load cell, can someone please tell me how to calibrate load cell and also how to get proper values?

I have used the below mentioned library as a reference and made some changes in it which are mentioned below:
source library : HX711 - Library to read out HX711 24-Bit Analog-to-Digita… | Mbed

My library files:
Hx711.cpp:

#include “mbed.h”
#include “Hx711.h”

void Hx711::set_gain(uint8_t gain) {
switch (gain) {
case 128: // channel A, gain factor 128
gain_ = 1;
break;
case 64: // channel A, gain factor 64
gain_ = 3;
break;
case 32: // channel B, gain factor 32
gain_ = 2;
break;
}

sck_.write(LOW);
read();

}

uint32_t Hx711::readRaw() {
// wait for the chip to become ready
// TODO: this is not ideal; the programm will hang if the chip never
// becomes ready…
while (!is_ready());

uint32_t value = 0;
uint8_t data[3] = { 0 };
uint8_t filler = 0x00;

// pulse the clock pin 24 times to read the data
data[2] = shiftInMsbFirst();
data[1] = shiftInMsbFirst();
data[0] = shiftInMsbFirst();

// set the channel and the gain factor for the next reading using the clock pin
for (unsigned int i = 0; i < gain_; i++) {
    sck_.write(HIGH);
    wait_us(1);
    sck_.write(LOW);
}

/*// Datasheet indicates the value is returned as a two's complement value
// Flip all the bits
data[2] = ~data[2];
data[1] = ~data[1];
data[0] = ~data[0];*/

// Replicate the most significant bit to pad out a 32-bit signed integer
if ( data[2] & 0x80 ) {
    filler = 0xFF;
} //else if ((0x7F == data[2]) && (0xFF == data[1]) && (0xFF == data[0])) {
   // filler = 0xFF; } 
else {
    filler = 0x00;
}

// Construct a 32-bit signed integer
value = ( static_cast<uint32_t>(filler)  << 24
        | static_cast<uint32_t>(data[2]) << 16
        | static_cast<uint32_t>(data[1]) << 8
        | static_cast<uint32_t>(data[0]) );

// ... and add 1
return static_cast<int>(value);

}

uint8_t Hx711::shiftInMsbFirst() {
uint8_t value = 0;

for (uint8_t i = 0; i < 8; ++i) {
    sck_.write(HIGH);
    value |= dt_.read() << (7 - i);
    sck_.write(LOW);
}
return value;

}

My Hx711.h file :

#ifndef HX711_H
#define HX711_H
#include “mbed.h”
/**

public:

/**
 * Create an Hx711 ADC object
 * @param pin_sck PinName of the clock pin (digital output)
 * @param pin_dt PinName of the data pin (digital input)
 * @param offset offset for sensor values
 * @param scale scale factor to obtain real values
 * @param gain channel selection is made by passing the appropriate gain: 
 *      128 or 64 for channel A, 32 for channel B
 */
Hx711(PinName pin_sck, PinName pin_dt, int offset, float scale, uint8_t gain = 128) :
    sck_(pin_sck),
    dt_(pin_dt) {
    set_offset(offset);
    set_scale(scale);
    set_gain(gain);
}

/**
 * Create an Hx711 ADC object with zero offset and unit scaling
 * @param pin_sck PinName of the clock pin (digital output)
 * @param pin_dt PinName of the data pin (digital input)
 * @param gain channel selection is made by passing the appropriate gain: 
 *      128 or 64 for channel A, 32 for channel B
 * TODO: constructor overloading is not allowed?
 */
Hx711(PinName pin_sck, PinName pin_dt, uint8_t gain = 128) :
    sck_(pin_sck),
    dt_(pin_dt) {
    set_offset(0);
    set_scale(1.0f);
    set_gain(gain);
}

/**
 * Check if the sensor is ready
 * from the datasheet: When output data is not ready for retrieval, 
 * digital output pin DOUT is high. Serial clock input PD_SCK should be low. 
 * When DOUT goes to low, it indicates data is ready for retrieval.
 * @return true if dt_.read() == LOW
 * TODO: this is not ideal; the programm will hang if the chip never
 * becomes ready...
 */
bool is_ready() {
    return dt_.read() == LOW;
}

/**
 * Waits for the chip to be ready and returns a raw int reading
 * @return int sensor output value
 */
uint32_t readRaw();

float averageValue(uint8_t times) {
int sum = 0;
for (int i = 0; i < times; i++)
{
    sum += read();
    wait(.01);
}

return sum / times ;
}
/**
 * Obtain offset and scaled sensor output; i.e. a real value
 * @return float
 */
float read() {
    return convert_to_real(readRaw());
}

/**
 * Convert integer value from chip to offset and scaled real value
 * @param val integer value
 * @return (val - get_offset()) * get_scale()
 */
float convert_to_real(int val) {
    return ((float)(val - get_offset())) / get_scale() ;
}

/**
 * Puts the chip into power down mode
 */
void power_down() {
    sck_.write(LOW);
    sck_.write(HIGH);
}

/**
 * Wakes up the chip after power down mode
 */
void power_up() {
    sck_.write(LOW);
}

/**
 * Set the gain factor; takes effect only after a call to read()
 * channel A can be set for a 128 or 64 gain; channel B has a fixed 32 gain
 * depending on the parameter, the channel is also set to either A or B
 * Ensures that gain_ = 128, 64 or 32
 * @param gain 128, 64 or 32
 */
void set_gain(uint8_t gain = 128);

/**
 * Obtain current gain
 * @return gain_
 */
uint8_t get_gain() {
    return gain_;
}

/**
 * Set the scale factor
 * @param scale desired scale
 */
void set_scale(float scale = 1.0f) { 
    scale_ = scale; 
};

/**
 * Get sensor scale factor
 * @return scale_
 */
float get_scale() { 
    return scale_; 
}

/**
 * Set the sensor offset
 * @param offset the desired offset
 */
void set_offset(int offset = 0) { 
    offset_ = offset; 
}

/**
 * Get current sensor offset
 * @return offset_
 */
int get_offset() { return offset_; }

private:

static const uint8_t LOW      = 0; // digital low
static const uint8_t HIGH     = 1; // digital high

DigitalOut sck_;    // clock line
DigitalIn dt_;      // data line 

uint8_t gain_;      // amplification factor at chip
int offset_;        // offset chip value
float scale_;       // scale output after offset

/**
 * Port of the Arduino shiftIn function; shifts a byte one bit at a time
 * @return incoming but
 */
uint8_t shiftInMsbFirst();

};

#endif

Datasheet of HX711 used :
https://cdn.sparkfun.com/datasheets/Sensors/ForceFlex/hx711_english.pdf

My Main.cpp code :

#include “mbed.h”
#include “platform/mbed_thread.h”
#include “math.h”
#include “string”
#include “Hx711.h”

Serial pc(USBTX, USBRX);
Hx711 scale(PC_3,PF_3,128);

int main()
{
float weight ;
float avgweight ;
while(1)
{
pc.printf(“Started to Weigh\n”);
weight = scale.averageValue(20);
pc.printf(“ActualValue = %.3f grams\n”, weight);
scale.power_down();
wait(.0000001);
scale.power_up();

wait_us(300000); 
}

I’m getting some values as output but which are not increasing/decreasing w.r.t the actual know weights, there is something wrong ,please look at my code and library used and help me.

@JohnnyK please look into this issue,if you find any solution, please help me with me

Hello,

constructor of that library contains offset, scale and gain, I believe that is settings to achive what you want.

Usually if you want calibrate a sensor, the weight sensor in this case (device for more), then you need a sample of known weight - for example a cup/mug that you can weigh on a kitchen scale.
With an object, our cup (for more just an object), with a known weight you are know the requested result from you device, then you need to take a current value from your device and compare it with the real weigh of your object and make a correction.

BR, Jan

If you do not know, then it is usually good to ask Google if someone else did something same/similar or alternatively with arduino. You can not use it 1:1 because of differentiations between platforms and voltage levels, but it is good for imagine where you need to go.

Here you can found some basics and also similar method what I described above.
Arduino with Load Cell and HX711 Amplifier (Digital Scale) | Random Nerd Tutorials

BR, Jan

Thanks @JohnnyK

Actually I have tried all the options available over internet for calibrating the sensor, viewing the available arduino-counterparts.

Also tried with actual known weights but the factor that I obtained for one weight is not working for the other.

The loadcell that I’m using is of 3kg capacity but the major problem that I’m facing is that as I increase the values of known weights the proportional values are getting reset after certain weight( i, e some 750 grams).

Is there any issue with the library.cpp file and my main.cpp file, please can you once have a look over it ,if possible.

First of all I do not have any similar hardware so I can not test it and tell you “do this”. And in our previous discussion I recommended to you another library because I am sure it is working because I know its maintainer.

Second, why you have here the number 128?

Your values are not correctly set. Wrong gain or something liek that.

BR, Jan

Thanks a lot @JohnnyK .

Yeah ,I’II try with other library you mentioned and also will try with varying gain.