Quickstart

Here you can find a short example how to use the library in your project.
You can also find an example in example folder.

Installation

ESP-IDF

The library contains a idf_compent.yml file. So you can install it with the esp-idf packagemanager to be aviable in your project.

To integerate it to your project create a file named idf_compent.yml and put following lines in it.

dependencies:
    idf: ">=4.4"

    hx711:
        git: https://github.com/avjui/hx711.git

The file was also aviable in the [example folder main dirctory](./example/main/).

After this installation is complete and you can build your project with th hx711 library.

Platformio

To use this library in platformio you can add lib_deps to your configuration file platformio.ini

lib_deps =
    git+https://github.com/avjui/hx711

Warning

Because platformio not regiester the Kconfig by him self you have to add Kconfig with editing CMakeLists.txt, or add the config variables to platfomrio.ini.

  • First option is to add following lines to CMakeLists.txt in the root directory.

get_filename_component(configName "${CMAKE_BINARY_DIR}" NAME)
list(APPEND kconfigs "${CMAKE_SOURCE_DIR}/.pio/libdeps/${configName}/hx711/Kconfig")
  • Second option is to add the config to platformio.ini.

[hx711]
; Build flags for hx711 library
build_flags=
    -D CONFIG_HX711_PIN_PDSCK=26 ; clock pin
    -D CONFIG_HX711_PIN_PDOUT=25 ; out pin
    -D CONFIG_HX711_SCALE=491    ; scale factor
    -D CONFIG_HX711_TARETIME=20  ; samples we use for tare
    -D CONFIG_HX711_SAMPLES=10   ; samples we use for avarge value
    -D CONFIG_HX711_WAIT_TIME=2  ; time to wait when toggle pins
    -D CONFIG_HX711_GAIN=1       ; Gain channel A 128=1, 32=3, channel B 64=2

[env:...]
....

build_flags =
    ${hx711.build_flags}

Initialization and setup

At first we must include the header files

#include <hx711.h>

Constructer with default config

After that we can create a Object with the standart config.
This means the configuration made by menuconfig well be used.

Note

There you can find a HX711 Configuration section.

/* Init with default config
*  or config make with 'menuconfig'
*/
HX711 load;

Constructer with custom config

If you want more control over the configuration you can use the overloaded constructer.
Ther you can use the hx711_conf_t struct.
/* Create custom config */
hx711_conf_t conf_hx711 = {
    .pin_pdsck = 26,
    .pin_dout = 27,
    .samples = 10,
    .gain = GAIN_128,
    .scale = SCALE,
};

/* Init with custom config */
HX711 load(&conf_hx711);

Tare the scale

We can tare automaticly to get the right offset

Note

You must be sure that you have nothing on the scale. Otherwise 0 gramm will be set within the weight staying on the scale when the tare function will be execute.

/* Tare the scale */
load.tare();

An other option is to set the offset manual. Maybe from stored value.

/* Set new offset */
float newoffset = 46709.0;
load.setOffset(newoffset);

Usage

Perform update of weight value

Here we have two options to update the cellload value.

One option can be to start the background task.

/* Start background task to periodicly read out value from hx711 modul */
load.startTask();

The other option is to poll it manuely. Be sure that you periodacly execute this function to get a new value.

/* Perform read out value from hx711 modul */
load.update();

Read load value

To read the load you can use the variable cellload. If you have started the background task you can read the value, without doing anything else.

/* Read value and print */
ESP_LOGI("MAIN", "Cell load: [ %.2f gram ] \n", load.cellload);

Note

Default we read the value for 10 times to get an average to have a more accurate result. When the hx711 modul is set to 10Mhz we get all second one result 10MHz/10. After some testing it resuslts that it looks like the modul i testet has about 5Mhz. So we get a result every 2 seconds. You can change this behavier by change the HX711_SAMPLES variable or by using the constructer with custom config.

If you have choose to poll it manuel you must call update before reading.

/* Update variable */
load.update();

/* Read value and print */
ESP_LOGI("MAIN", "Cell load: [ %.2f gram ] \n", load.cellload);