Can I use Arduino HX711 Library in ESP-IDF?

SteaLTH_INSteaD
Posts: 5
Joined: Sun Feb 16, 2020 4:20 am

Can I use Arduino HX711 Library in ESP-IDF?

Postby SteaLTH_INSteaD » Mon Mar 01, 2021 6:38 pm

Hi,

I am moving from Arduino to ESP-IDF and I was wondering if its possible to use Bogde library instead of UncleRus for ESP-IDF.
Arduino Bogde: https://github.com/bogde/HX711
ESP-IDF UncleRus: https://github.com/UncleRus/esp-idf-lib ... ents/hx711

I was able to get some raw ADC data using UncleRus Library using the sample code, but it seems easier to use Bodge because there are already methods for tare, calibration, etc. I already have this working in Arduino. Whereas if I use UncleRus library I will have to create these functions.

Any tips/ideas for easiest path moving forward?

Edit: Maybe its better to stick with the ESP-IDF library by UncleRus. Are there examples of how I can calibrate to get accurate weight data and how to implement functions to Tare?

UncleRus
Posts: 6
Joined: Wed Dec 30, 2015 1:10 am

Re: Can I use Arduino HX711 Library in ESP-IDF?

Postby UncleRus » Thu May 20, 2021 5:16 pm

Write this function yourself, it's simple.

First of all, write something like this to average measurements:

Code: Select all

esp_err_t hx711_read_average(hx711_t *dev, size_t times, bool rate, int32_t *data)
{
    int32_t v;
    *data = 0;
    for (size_t i = 0; i < times; i++)
    {
        esp_err_t res = hx711_read_data(dev, &v);
        if (res != ESP_OK) return res;
        *data += v;
        vTaskDelay(pdMS_TO_TICKS(rate ? 20 : 100)); // ~80 SPS for rate = 1, 10 SPS for rate = 0
    }
    *data /= times;
    
    return ESP_OK;
}
Taring is simply offseting of the read values.
That is, for taring, you need to read the value without weight (better use averaging function from above), remember it and then subtract it from the raw values.
To convert the obtained values into the desired measurement units, it is necessary to place a load of a known weight and read the corresponding raw value. Then the weight of the load must be divided by this value and you'll get conversion factor.

Code: Select all

...
int32_t offset;
// taring without weight, offset is average of 3 readings
// set rate argument to the logic value of your HX711 RATE pin (true for logic 1, false for logic 0)
ESP_ERROR_CHECK(hx711_read_average(dev, 3, true, &offset));
...
// Now you must place weight of 1kg (1 kg just for example, any known weight will do)
// you need to do this only once, than you can use result as a const
int32_t kg_weight;
ESP_ERROR_CHECK(hx711_read_average(dev, 3, true, &kg_weight));
float g_factor = 1000.0 / (kg_weight - offset);
....
// Now you can convert raw values to the grams
int32_t raw;
ESP_ERROR_CHECK(hx711_read_average(dev, 3, true, &raw));
float weight_g = (raw - offset) * g_factor;

Who is online

Users browsing this forum: Bing [Bot], nopnop2002 and 398 guests