An Arduino library to interface the Avia Semiconductor HX711 24-Bit Analog-to-Digital Converter (ADC) for reading load cells / weight scales.
It supports the architectures atmelavr
, espressif8266
, espressif32
,
atmelsam
, teensy
and ststm32
by corresponding PlatformIO targets.
The library is usually used in blocking mode, i.e. it will wait for the hardware becoming available before returning a reading.
#include "HX711.h"
HX711 loadcell;
// 1. HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 2;
const int LOADCELL_SCK_PIN = 3;
// 2. Adjustment settings
const long LOADCELL_OFFSET = 50682624;
const long LOADCELL_DIVIDER = 5895655;
// 3. Initialize library
loadcell.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
loadcell.set_scale(LOADCELL_DIVIDER);
loadcell.set_offset(LOADCELL_OFFSET);
// 4. Acquire reading
Serial.print("Weight: ");
Serial.println(loadcell.get_units(10), 2);
It is also possible to define a maximum timeout to wait for the hardware to be initialized. This won't send the program into a spinlock when the scale is disconnected and will probably also account for hardware failures.
// 4. Acquire reading without blocking
if (loadcell.wait_ready_timeout(1000)) {
long reading = loadcell.get_units(10);
Serial.print("Weight: ");
Serial.println(reading, 2);
} else {
Serial.println("HX711 not found.");
}
See examples
directory in this repository.
- Arduino AVR core
- Arduino core for ESP8266
- Arduino core for ESP32
- Arduino core for SAMD21 (untested)
- Arduino core for SAMD51 (untested)
- Arduino core for STM32
The library has been tested successfully on the following hardware.
- ATmega328: Arduino Uno
- ESP8266: WeMos D1 mini, Adafruit HUZZAH
- ESP32: ESP32 DEVKIT V1, Heltec WiFi Kit 32, Adafruit Feather HUZZAH32
- STM32 F1 (Cortex-M3): STM32F103C8T6 STM32 Blue Pill Board
Thanks, @bogde and @ClemensGruber!
-
It provides a
tare()
function, which "resets" the scale to 0. Many other implementations calculate the tare weight when the ADC is initialized only. I needed a way to be able to set the tare weight at any time. Use case: Place an empty container on the scale, calltare()
to reset the readings to 0, fill the container and get the weight of the content. -
It provides a
power_down()
function, to put the ADC into a low power mode. According to the datasheet,When PD_SCK pin changes from low to high and stays at high for longer than 60μs, HX711 enters power down mode.
Use case: Battery-powered scales. Accordingly, there is a
power_up()
function to get the chip out of the low power mode. -
It has a
set_gain(byte gain)
function that allows you to set the gain factor and select the channel. According to the datasheet,Channel A can be programmed with a gain of 128 or 64, corresponding to a full-scale differential input voltage of ±20mV or ±40mV respectively, when a 5V supply is connected to AVDD analog power supply pin. Channel B has a fixed gain of 32.
The same function is used to select the channel A or channel B, by passing 128 or 64 for channel A, or 32 for channel B as the parameter. The default value is 128, which means "channel A with a gain factor of 128", so one can simply call
set_gain()
.This function is also called from the initializer method
begin()
. -
The
get_value()
andget_units()
functions can receive an extra parameter "times", and they will return the average of multiple readings instead of a single reading.
- Call
set_scale()
with no parameter. - Call
tare()
with no parameter. - Place a known weight on the scale and call
get_units(10)
. - Divide the result in step 3 to your known weight. You should
get about the parameter you need to pass to
set_scale()
. - Adjust the parameter in step 4 until you get an accurate reading.
This will spawn a Python virtualenv in the current directory,
install platformio
into it and then execute platformio run
,
effectively building for all environments defined in platformio.ini
.
make build-all
Environment feather_328 [SUCCESS]
Environment atmega_2560 [SUCCESS]
Environment huzzah [SUCCESS]
Environment lopy4 [SUCCESS]
Environment teensy31 [SUCCESS]
Environment teensy36 [SUCCESS]
Environment feather_m0 [SUCCESS]
Environment arduino_due [SUCCESS]
Environment feather_m4 [SUCCESS]
Environment bluepill [SUCCESS]
https://gist.github.com/amotl/5ed6b3eb1fcd2bc78552b218b426f6aa
You can run a build for a specific architecture by specifying the appropriate platform label on the command line.
# Build for LoPy4
make build-env environment=lopy4
# Build for Feather M0
make build-env environment=feather_m0
This library received some spring-cleaning in February 2019 (#123), removing the pin definition within the constructor completely, as this was not timing safe. (#29) Please use the new initialization flavor as outlined in the example above.
Thanks to Weihong Guan who started the first version of this library in 2012
already (see [arduino|module]Hx711 electronic scale kit,
sources), Bogdan Necula
who took over in 2014 and last but not least all others who contributed to this
library over the course of the last years, see also CONTRIBUTORS.rst
in this
repository.
There are other libraries around, enjoy:
You should consider getting into the details of strain-gauge load cell sensors when expecting reasonable results. The range of topics is from sufficient and stable power supply, using the proper excitation voltage to the Seebeck effect and temperature compensation.
See also:
- Overview about real world effects
- Thermoelectric effect (Seebeck effect)
- Temperature compensation: Resource collection, DIY research
- Power management for HX711