Arduino library · Sensors

HX711_ADC: a load cell scale that never blocks loop()

Yes. You can use the HX711_ADC library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (Load cell with HX711). The example below, "A kitchen scale that never waits", runs on the Arduino Uno R3.

Built in on Mokxi: #include <HX711_ADC.h> and it builds, with nothing to install. Free, and no account needed.

What HX711_ADC does

HX711_ADC by Olav Kallhovd is the second common library for the HX711 load cell amplifier, next to bogde’s HX711. The difference is in how it waits. bogde’s read_average() sits in a loop until it has its readings; HX711_ADC never waits. You call update() on every pass of loop(), it takes a conversion only when the chip has one ready, and getData() returns the smoothed weight whenever you ask.

The smoothing is a moving average of the last 16 conversions by default, with the single highest and lowest left out to throw away spikes. The HX711 on the common green board converts ten times a second, so a fresh average takes nearly two seconds to fill. That is why start() takes a settling time: it waits that long, then tares the scale so whatever sits on it now reads zero.

Calibration is one number, the counts per gram (or per whatever unit you like). Put a known weight on the tared scale and getNewCalibration() works the factor out for you, or use the value you found once and hard-code it with setCalFactor(), as the example does. The example wires DOUT to pin 3 and SCK to pin 2, starts empty, then puts 500 g on the scale so you can watch the reading climb to 500.

Library Manager nameHX711_ADC
AuthorOlav Kallhovd
Include#include <HX711_ADC.h>
Boardsevery board Mokxi compiles for but ATtiny85
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesLoad cell with HX711

Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <HX711_ADC.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "HX711_ADC" by Olav Kallhovd from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add HX711_ADC to lib_deps in PlatformIO. Mokxi follows the calls of release 1.2.12, so a sketch written here builds there too.

The calls you will use

In the order a sketch meets them.

HX711_ADC LoadCell(HX711_dout, HX711_sck)

The amplifier on two digital pins, DOUT first, then SCK. Any pins work.

LoadCell.begin()

Sets up the pins and selects channel A at gain 128, the setting for a load cell.

LoadCell.start(2000, true)

Lets the readings settle for 2000 ms, then tares if the second argument is true. Blocks for that long, once, in setup().

LoadCell.setCalFactor(429.5)

Counts per unit. getData() divides the tared reading by this, so a larger factor gives a smaller number.

LoadCell.update()

Takes a conversion if one is ready and returns true when new data went into the average. Call it on every pass of loop().

if (LoadCell.update()) {
  Serial.println(LoadCell.getData(), 1);
}

LoadCell.getData()

The averaged weight, tared and divided by the calibration factor, as a float.

LoadCell.tareNoDelay()

Starts a tare over the next updates without blocking; getTareStatus() returns true once when it is done.

LoadCell.getNewCalibration(500.0)

With a known 500 g on the tared scale, works out and sets the calibration factor, and returns it so you can save it.

Example: A kitchen scale that never waits

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
#include <HX711_ADC.h>

const int HX711_dout = 3;
const int HX711_sck = 2;
HX711_ADC LoadCell(HX711_dout, HX711_sck);

unsigned long t = 0;

void setup() {
  Serial.begin(57600);
  Serial.println("Starting...");
  LoadCell.begin();
  unsigned long stabilizingtime = 2000;
  boolean _tare = true;  // zero whatever is on the scale now
  LoadCell.start(stabilizingtime, _tare);
  LoadCell.setCalFactor(429.5);  // counts per gram, from a known weight
  Serial.println("Startup is complete");
}

void loop() {
  if (LoadCell.update() && millis() > t + 500) {
    Serial.print("Load_cell output val: ");
    Serial.println(LoadCell.getData(), 0);
    t = millis();
  }
}

Wiring

  • Arduino Uno R3 3 to Load cell with HX711 DT
  • Arduino Uno R3 2 to Load cell with HX711 SCK
  • Arduino Uno R3 5V to Load cell with HX711 VCC
  • Arduino Uno R3 GND to Load cell with HX711 GND

Open it in the editor, wired and ready to run

Common errors and how to fix them

The weight never changes, or getData() stays at 0

update() is not being called often enough, or a delay() in loop() holds it off. Call it on every pass and print on a millis() timer instead, as the example does with its 500 ms check.

The reading is negative when weight is added

The load cell is mounted the other way up or its signal wires are swapped. Flip the cell, swap the green and white wires at the HX711, or make the calibration factor negative.

The weight drifts slowly upward over minutes

Load cells creep under a steady load and warm up after power on. Let the scale run for a minute before taring, and tare again with tareNoDelay() whenever the scale is empty.

Questions and answers

HX711 or HX711_ADC: which should I use?

Use HX711_ADC when the sketch must keep doing other things, such as a display or buttons, while it weighs. bogde’s HX711 is simpler for a sketch that only weighs and prints.

How do I keep the calibration after a restart?

Save the factor that getNewCalibration() returns to EEPROM with EEPROM.put(), and read it back with EEPROM.get() before setCalFactor() in setup().

Try HX711_ADC without installing anything

Open the example, change the sketch, and keep your version in a free account.