Driver for the HX711 24-bit ADC, the chip on the cheap load-cell boards.
The HX711 is not a general-purpose ADC with a register interface: it is a sigma-delta converter that streams one 24-bit two's complement word at a time over two wires, and the number of clock pulses after the word selects the channel and gain of the next conversion.
- Channel A with gain 128 or 64, channel B with gain 32.
- Blocking bit-bang read of the raw 24-bit value, average of several samples.
- Polling for the data-ready signal with a timeout.
- Power-down and power-up of the chip through the clock line.
| HX711 | ESP32 | Note |
|---|---|---|
| VCC | 3V3 | 2.7-5.5 V, the board works from 3.3 V |
| GND | GND | common ground |
| DT | GPIO | data output, the chip pulls it low when a conversion is ready |
| SCK | GPIO | clock, also used for power-down |
The load cell is wired to E+, E-, A+ and A- on the board, the shield
goes to ground. A 1 kg cell gives a few millivolts at full load, so the board
amplifies it by 128 and the result is a 24-bit number, not grams.
Both pins are configured by the driver and must be different. The data pin is
an input without pull-up: the chip drives it actively, and hx711_wait() polls
it, so no interrupt is involved.
hx711_wait(dev, timeout_ms) returns when the chip is ready, and
hx711_read_data(dev, &raw) clocks the word out. The value is signed,
-8388608 ... 8388607. hx711_read_average(dev, times, &raw) waits for every
sample and returns the mean, which is the usual way to reduce the noise.
The read itself takes about 50 us and runs inside a critical section: the
protocol has no start bit, a lost clock edge shifts every following bit, so the
sequence can not be interrupted. hx711_read_data() therefore refuses to run
when the chip is still converting and returns ESP_ERR_INVALID_STATE; the
average function waits first and does not hit this.
hx711_set_gain() does not change a register. It clocks out the pending
conversion and sends 25, 26 or 27 pulses in total, and the chip remembers the
channel for the next conversion:
| Value | Channel | Gain | Pulses |
|---|---|---|---|
HX711_GAIN_A_128 |
A | 128 | 25 |
HX711_GAIN_B_32 |
B | 32 | 26 |
HX711_GAIN_A_64 |
A | 64 | 27 |
The value read by hx711_set_gain() itself is discarded, so expect one sample
to be lost when the channel changes.
The chip enters power-down when the clock line stays high for more than 60 us,
and hx711_power_down(dev, true) does exactly that. Power-up releases the line
and blocks for 400 ms: the datasheet requires that much before the first
conversion after power-up is valid. hx711_init() powers the chip up and
therefore blocks for about half a second.
The driver returns raw counts; the conversion is a tare and a scale factor:
int32_t zero, raw;
hx711_read_average(&dev, 10, &zero); // nothing on the scale
// put a known weight, read again
hx711_read_average(&dev, 10, &raw);
float scale = (float)(raw - zero) / known_grams; // counts per gram
float grams = (raw - zero) / scale;The zero point drifts with temperature and with the mechanical setup, so tare again after the scale has warmed up. The chip also has no notion of units and no calibration registers: everything above lives in the application.
The scale example does the same with the calibration component: it takes
two points from the serial console (tare, then cal <grams>) and prints the
mass afterwards. examples/scale/README.md explains the procedure.
The board runs at 10 samples per second by default and at 80 samples per second
when the RATE pin is tied high. Cheap boards often carry a 12 MHz crystal
instead of 11.0592 MHz, which shifts both numbers by about 8 percent. A
conversion period is therefore about 100 ms (or 12.5 ms) plus the read time,
and a timeout of a few hundred milliseconds is enough for the slow rate on any
board.
- Only bit-banging over GPIO is implemented; the SPI-like mode of the chip does not exist, the two wires are its whole interface.
- The driver is not thread safe, the descriptor has no mutex.
- The chip has no identification register, so
hx711_init()can only tell that a conversion arrived, not which chip answered.
idf.py add-dependency esp-idf-lib/hx711For questions and discussions about the component, please use Discussions at esp-idf-lib/core.
Please read CONTRIBUTING.md at esp-idf-lib/core.