# Arduino UNO + HX710B/HX711 Based Digital Weighing Machine

A: Hardware Block Diagram

Figure-12.1: Hardware Block Diagram for the Digital Weighing Machine (DWM)

1. Weight Acquisition and Display on Serial Monitor
The LC (Load Cell) has a capacity of 0 – 30 kg, and it produces a voltage of 0 – 10 mV corresponding to 0 kg – 30 kg load when biased by 5V supply at its EX+/EX- terminals. The output of the LC is amplified by and digitized by the HX710B chip (U4). The HX710B chip offers 64-time amplification to the input signal which is then digitized by a 24-bit Serial ADC. The calibration counts of the input device (LC + Frame + HX710B) are:

A(0 gm, 0x32656), B(500 gm, 0x4B4C4), C(1000 gm, 0x607A8), D(1500 gm, 0x79180), and E(W, C)

Based on these two known point responses: B(500 gm, 0x4B4C4) and D(1500 gm, 0x79180 ), we can find the equation for the unknown W in terms of C (the ADC value) as:

(W-1500)/(C-0x79180)=(1500-500)/(0x79180-0x4B4C4)

 W= (1500-500)/(0x79180-0x4B4C4)*C- (1500-500)/(0x79180-0x4B4C4)*0x79180+1500

 W= 0.005331C- 1146.176
 float W=(float)0.005331
C- 1146.176; //W is in gm
 W=(float)W/1000.00; //W is in kg

Program Codes:
This program takes 10 samples from LC + HX710B at 2-sec interval and then computes the * average and then shows the weight (in kg) on the Serial Monitor. This program takes 10 samples from LC + HX710B at 2-sec interval and then computes the average.

``````unsigned long x = 0, y=0;
unsigned long dataArray[10];
int j = 0;
void setup()
{
Serial.begin(9600);
pinMode(A1, INPUT); //data line  //Yellow cable
pinMode(A0, OUTPUT);  //SCK line  //Orange cable
}

void loop()
{

for (int j = 0; j < 10; j++)
{
while (digitalRead(A1) != LOW) //wait until Data Line goes LOW
;
{
for (int i = 0; i < 24; i++)  //read 24-bit data from HX711
{
clk();      //generate CLK pulse to get MSB-it at A1-pin
x = x << 1;
}
clk();
Serial.println(x, HEX);
y = x;
x = 0;
delay(2000);
}
dataArray[j] = y;
}

Serial.println("===averaging process=========");
unsigned long sum = 0;

for (j = 0; j < 10; j++)
{
sum += dataArray[j];
}
Serial.print("Average Count = ");
sum = sum / 10;
Serial.println(sum, HEX);
float W = (float)0.005331 * sum - 1146.176;
W = (float)W / 1000.00; //17 gm adjustment
Serial.println(W, 3);
}

void clk()
{
digitalWrite(A0, HIGH);
digitalWrite(A0, LOW);
}
``````

Why bother taking ten readings into an array and then summing the readings to take the mean, when you don't do anything else with the values in the array?

although it is and old topic, any correction is handy for everyone.
in schematics, the SDATA (yellow line) is A0 but in code it is modded for A1,
in schematics, the SCCK (orange line) is A1 but in code it is modded for A0.

consider this swapped situation when you are using this code.
thanks

Hello GolamMostafa,

Have you been figured out the things regarding Arduino UNO + HX710B/HX711 Based Digital Weighing Machine. At the moment, I am engaged in a similar project.

The following are library files and calibration sketch and example sketch:

HX711.h

``````#ifndef HX711_h
#define HX711_h

#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

class HX711
{
private:
byte PD_SCK;	// Power Down and Serial Clock Input Pin
byte DOUT;		// Serial Data Output Pin
byte GAIN;		// amplification factor
long OFFSET;	// used for tare weight
float SCALE;	// used to return weight in grams, kg, ounces, whatever

public:
// define clock and data pin, channel, and gain factor
// channel selection is made by passing the appropriate gain: 128 or 64 for channel A, 32 for channel B
// gain: 128 or 64 for channel A; channel B works with 32 gain factor only
HX711(byte dout, byte pd_sck, byte gain = 128);

virtual ~HX711();

// check if HX711 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.

// 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
void set_gain(byte gain = 128);

// waits for the chip to be ready and returns a reading

// returns an average reading; times = how many times to read

// returns (read_average() - OFFSET), that is the current value without the tare weight; times = how many readings to do
double get_value(byte times = 1);

// returns get_value() divided by SCALE, that is the raw value divided by a value obtained via calibration
// times = how many readings to do
float get_units(byte times = 1);

// set the OFFSET value for tare weight; times = how many times to read the tare value
void tare(byte times = 10);

// set the SCALE value; this value is used to convert the raw data to "human readable" data (measure units)
void set_scale(float scale = 1.f);

// get the current SCALE
float get_scale();

// set OFFSET, the value that's subtracted from the actual reading (tare weight)
void set_offset(long offset = 0);

// get the current OFFSET
long get_offset();

// puts the chip into power down mode
void power_down();

// wakes up the chip after power down mode
void power_up();
};

#endif /* HX711_h */
``````

HX711.cpp

``````#include <Arduino.h>
#include <HX711.h>

HX711::HX711(byte dout, byte pd_sck, byte gain) {
PD_SCK 	= pd_sck;
DOUT 	= dout;

pinMode(PD_SCK, OUTPUT);
pinMode(DOUT, INPUT);

set_gain(gain);
}

HX711::~HX711() {

}

}

void HX711::set_gain(byte 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;
}

digitalWrite(PD_SCK, LOW);
}

// wait for the chip to become ready

unsigned long value = 0;
byte data[3] = { 0 };
byte filler = 0x00;

// pulse the clock pin 24 times to read the data
data[2] = shiftIn(DOUT, PD_SCK, MSBFIRST);
data[1] = shiftIn(DOUT, PD_SCK, MSBFIRST);
data[0] = shiftIn(DOUT, PD_SCK, MSBFIRST);

// set the channel and the gain factor for the next reading using the clock pin
for (unsigned int i = 0; i < GAIN; i++) {
digitalWrite(PD_SCK, HIGH);
digitalWrite(PD_SCK, 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<unsigned long>(filler) << 24
| static_cast<unsigned long>(data[2]) << 16
| static_cast<unsigned long>(data[1]) << 8
| static_cast<unsigned long>(data[0]) );

return static_cast<long>(++value);
}

long sum = 0;
for (byte i = 0; i < times; i++) {
}
return sum / times;
}

double HX711::get_value(byte times) {
}

float HX711::get_units(byte times) {
return get_value(times) / SCALE;
}

void HX711::tare(byte times) {
set_offset(sum);
}

void HX711::set_scale(float scale) {
SCALE = scale;
}

float HX711::get_scale() {
return SCALE;
}

void HX711::set_offset(long offset) {
OFFSET = offset;
}

long HX711::get_offset() {
return OFFSET;
}

void HX711::power_down() {
digitalWrite(PD_SCK, LOW);
digitalWrite(PD_SCK, HIGH);
}

void HX711::power_up() {
digitalWrite(PD_SCK, LOW);
}
``````

Calibration sketch:

``````/*
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
Arduino pin 5 -> HX711 CLK
Arduino pin 6 -> HX711 DOUT
Arduino pin 5V -> HX711 VCC
Arduino pin GND -> HX711 GND
*/

#include "HX711.h"

HX711 scale(6, 5);

float calibration_factor = -400; // this calibration factor is adjusted according to my load cell
float units;
float ounces;

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.set_scale();
scale.tare();  //Reset the scale to 0

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

units = scale.get_units(), 10;
if (units < 0)
{
units = 0.00;
}
ounces = units * 0.035274;
Serial.print(units);
Serial.print(" grams");
Serial.print(" calibration_factor: ");
Serial.print(calibration_factor);
Serial.println();

if(Serial.available())
{
if(temp == '+' || temp == 'a')
calibration_factor += 1;
else if(temp == '-' || temp == 'z')
calibration_factor -= 1;
}
delay(2000);
}
``````

Example sketch:

``````#include "HX711.h"

HX711 scale(6, 5); //HX711 scale(6, 5);

float calibration_factor = -58;
float units;
float ounces;

void setup()
{
Serial.begin(9600);
Serial.println("HX711 weighing");
scale.set_scale(calibration_factor);
scale.tare();
}

void loop()
{
units = scale.get_units(10);
if (units < 0)
{
units = 0.00;
}
ounces = units * 0.035274;
Serial.print(units);
Serial.println(" grams");
// delay(10);
}
``````

I have been using these library files and sketches, but the readings are not accurate. I used calibration sketch to find out calibration factor. Even though there is no weight on load cell it’s indicating non zero value but it’s somewhat close to zero and whenever I place the 200 gram load on cell, its indicating 195.67 or 196.67 or something close to 200 but not accurate enough. Moreover, it’s indicating different readings at different positions of weight on load cell.

It’s a humble request to you to help me in this regard considering to get accurate and stable readings.

Thank you.