BO
boltays/hx711-stm32-driver
HX711 load cell amplifier driver library for STM32 microcontrollers using HAL (C language).
HX711 STM32 Driver
This repository contains a C source file that implements an HX711 load cell amplifier driver for STM32 microcontrollers using the HAL API.
The driver provides basic functions for reading data from the HX711 module, setting gain, calibration offset, scale adjustment, and power control.
Features
- Gain selection for channel A (128, 64) and channel B (32)
- Offset (tare) and scale value adjustment
- Average reading for noise reduction
- Power down and power up control
- Compatible with STM32 HAL GPIO interface
Pin Configuration
| HX711 Pin | STM32 Pin | Description |
|---|---|---|
| DT (DOUT) | PE13 | Data line |
| SCK (PD_SCK) | PE11 | Clock line |
API Overview
| Function | Description |
|---|---|
void begin(uint8_t gain) |
Initializes the HX711 with the specified gain. |
bool is_ready() |
Checks if the HX711 is ready for data retrieval. |
void set_gain(uint8_t gain) |
Sets the gain and channel selection. |
long read() |
Reads 24-bit raw data from HX711. |
long read_average(uint8_t times) |
Returns the average of multiple readings. |
double get_value(uint8_t times) |
Returns the raw value after subtracting offset. |
float get_units(uint8_t times) |
Returns the scaled value based on calibration. |
void tare(uint8_t times) |
Sets the current average reading as offset. |
void set_scale(float scale) |
Sets the scale factor. |
float get_scale() |
Returns the current scale factor. |
void set_offset(long offset) |
Sets the offset manually. |
long get_offset() |
Returns the current offset. |
void power_down() |
Powers down the HX711. |
void power_up() |
Powers up the HX711. |
Example Usage
#include "HX711_2.h"
int main(void) {
HAL_Init();
begin(128); // Initialize with gain 128
tare(10); // Set current load as offset
while (1) {
float weight = get_units(5);
HAL_Delay(500);
}
}On this page
Contributors
Created June 23, 2019
Updated November 6, 2025