3.2.4. hx711 — HX711 ADC for weigh scales

The HX711 chipset performs ADC convertions on weigh scales. It can be used to measure weight in various ranges.

../../../_images/hx711.jpg

This driver provides methods to read ADC samples from given channel with given gain.

3.2.4.1. Example usage

This is a small example illustrating how to read one sample from each channel and gain combination.

struct hx711_driver_t hx711;
float weight_a_128;
float weight_a_64;
float weight_b_32;

/* Initialize and start the deivce. */
hx711_init(&hx711, &pin_d2_dev, &pin_d3_dev, 1.0, 0.0);
hx711_start(&hx711);

/* Read a few samples from the device. */
hx711_read(&hx711, &weight_a_128, hx711_channel_gain_a_128_t);
hx711_read(&hx711, &weight_a_64, hx711_channel_gain_a_64_t);
hx711_read(&hx711, &weight_b_32, hx711_channel_gain_b_32_t);

/* Print the samples. */
std_printf(OSTR("weight_a_128: %f, weight_a_64: %f, weight_b_32: %f\r\n"),
           weight_a_128,
           weight_a_64,
           weight_b_32);

/* Stop the deivce. */
hx711_stop(&hx711);

Source code: src/drivers/sensors/hx711.h, src/drivers/sensors/hx711.c

Test code: tst/drivers/software/sensors/hx711/main.c

Test coverage: src/drivers/sensors/hx711.c

Example code: examples/hx711/main.c


Enums

enum hx711_channel_gain_t

Values:

hx711_channel_gain_a_128_t = 1
hx711_channel_gain_b_32_t
hx711_channel_gain_a_64_t

Functions

int hx711_module_init(void)

Initialize the hx711 module. This function must be called before calling any other function in this module.

The module will only be initialized once even if this function is called multiple times.

Return
zero(0) or negative error code.

int hx711_init(struct hx711_driver_t *self_p, struct pin_device_t *pd_sck_p, struct pin_device_t *dout_p, float scale, float offset)

Initialize given driver object from given configuration.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object to initialize.
  • pd_sck_p: PD_SCK pin device.
  • dout_p: DOUT pin device.
  • scale: Scale value to multiply with read samples after the offset has been added.
  • offset: Offset value to add to read samples before they are scaled.

int hx711_start(struct hx711_driver_t *self_p)

Start the driver by configuring the pins and resetting the HX711.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object to start.

int hx711_stop(struct hx711_driver_t *self_p)

Stop given driver by setting pins as input.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object to stop.

int hx711_read(struct hx711_driver_t *self_p, float *weight_p, enum hx711_channel_gain_t channel_gain)

Read a offsetted and scaled weight from given channel and gain combination.

Return
zero(0) or negative error code.
Parameters
  • self_p: Initialized driver object.
  • weight_p: Measured offsetted and scaled weight.
  • channel_gain: Channel and gain combination.

int hx711_read_raw(struct hx711_driver_t *self_p, int32_t *sample_p, enum hx711_channel_gain_t channel_gain)

Read a sample from given channel and gain combination and output the sign extended raw read value. No offsetting or scaling is performed.

Return
zero(0) or negative error code.
Parameters
  • self_p: Initialized driver object.
  • sample_p: Sign extended read sample.
  • channel_gain: Channel and gain combination.

int hx711_set_scale(struct hx711_driver_t *self_p, float scale)

Set the scale value.

Return
zero(0) or negative error code.
Parameters
  • self_p: Initialized driver object.
  • scale: Scale value to multiply with read samples after the offset has been added.

int hx711_set_offset(struct hx711_driver_t *self_p, float offset)

Set the offset value.

Return
zero(0) or negative error code.
Parameters
  • self_p: Initialized driver object.
  • offset: Offset value to add to read samples before they are scaled.

struct hx711_driver_t

Public Members

struct pin_device_t *pd_sck_p
struct pin_device_t *dout_p
float scale
float offset