想做咖啡机称重、蜂箱秤、料斗余量监测,都绕不开称重传感器加HX711这套组合。称重传感器本体是个贴了应变片的铝梁,输出每伏激励只有毫伏级的差分信号,直接进ADC根本读不动,HX711就是为它生的:内置24位ADC、128倍增益、可编程采样率,一颗芯片十块钱。这篇从接线到标定系数走一遍ESP32的实现,Arduino同理。

认识两个主角
常见的1千克或5千克铝梁称重传感器是半桥或全桥应变片结构,四根线:红的是激励正E+,黑的是激励负E-,白的是信号正A+,绿或蓝的是信号负A-。梁中间受力、两端固定,受力时应变片阻值变化破坏电桥平衡,产生与重量成正比的差分电压。

HX711板上两排焊盘一一对应:E+、E-、A+、A-接传感器四线,VCC、GND、DT(数据)、SCK(时钟)接主控。模块供电3.3到5V都行,ESP32用3.3V即可。
接线
默认接法:DT接GPIO16,SCK接GPIO4,VCC接3V3,GND共地。数据脚和时钟脚可以换任意数字口,改代码里的定义就行。

第一步:读原始值
库管理器装bogde的HX711库。下面的标定脚本读出原始计数值,用于计算标定系数:
/*
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp32-load-cell-hx711/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
// Calibrating the load cell
#include "Arduino.h"
#include "soc/rtc.h"
#include "HX711.h"
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 16;
const int LOADCELL_SCK_PIN = 4;
HX711 scale;
void setup() {
Serial.begin(115200);
rtc_cpu_freq_config_t config;
rtc_clk_cpu_freq_get_config(&config);
rtc_clk_cpu_freq_to_config(RTC_CPU_FREQ_80M, &config);
rtc_clk_cpu_freq_set_config_fast(&config);
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
}
void loop() {
if (scale.is_ready()) {
scale.set_scale();
Serial.println("Tare... remove any weights from the scale.");
delay(5000);
scale.tare();
Serial.println("Tare done...");
Serial.print("Place a known weight on the scale...");
delay(5000);
long reading = scale.get_units(10);
Serial.print("Result: ");
Serial.println(reading);
}
else {
Serial.println("HX711 not found.");
}
delay(1000);
}
//calibration factor will be the (reading)/(known weight)
烧录后空载读一次计数,再放一个已知重量的东西(比如一瓶未开封的500毫升矿泉水,算上瓶子按实际克数)再读一次,标定系数等于两次读数之差除以已知克数。源教程的例子是-141449除以300克约等于-471.5,负号说明受力方向与库默认方向相反,属正常现象。
第二步:直接出克数
把标定系数填进下面这个电子秤示例,串口直接输出克和千克:
/*
* Complete project details at https://RandomNerdTutorials.com/esp32-load-cell-hx711/
*
* HX711 library for Arduino - example file
* https://github.com/bogde/HX711
*
* MIT License
* (c) 2018 Bogdan Necula
*
**/
#include "Arduino.h"
#include "HX711.h"
#include "soc/rtc.h"
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 16;
const int LOADCELL_SCK_PIN = 4;
HX711 scale;
void setup() {
Serial.begin(115200);
rtc_cpu_freq_config_t config;
rtc_clk_cpu_freq_get_config(&config);
rtc_clk_cpu_freq_to_config(RTC_CPU_FREQ_80M, &config);
rtc_clk_cpu_freq_set_config_fast(&config);
Serial.println("HX711 Demo");
Serial.println("Initializing the scale");
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
Serial.println("Before setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight (not set yet)
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight (not set) divided
// by the SCALE parameter (not set yet)
scale.set_scale(INSERT YOUR CALIBRATION FACTOR);
//scale.set_scale(-471.497); // this value is obtained by calibrating the scale with known weights; see the README for details
scale.tare(); // reset the scale to 0
Serial.println("After setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight, set with tare()
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight, divided
// by the SCALE parameter set with set_scale
Serial.println("Readings:");
}
void loop() {
Serial.print("one reading:\t");
Serial.print(scale.get_units(), 1);
Serial.print("\t| average:\t");
Serial.println(scale.get_units(10), 5);
scale.power_down(); // put the ADC in sleep mode
delay(5000);
scale.power_up();
}

精度和漂移的实话
这套方案日常分辨到1克没问题,但要稳定到0.1克就得认真对待两件事:一是机械安装,传感器必须按箭头方向悬空受力,两端螺丝拧紧,中间不能碰到任何东西;二是温度漂移,HX711便宜版温漂明显,开机预热十分钟再标定,环境温度变了要重标。另外采样率默认10赫兹,调到80赫兹会快但噪声也大,滤波策略(滑动平均或中位数)自己权衡。
上电读数为0但放东西不动:先查E+和E-有没有接反;数值乱跳:把DT、SCK的杜邦线换短的,HX711对时钟线干扰挺敏感。











机械悬空受力这条最容易被忽略,中间蹭一下全白标
标定系数用矿泉水这个办法聪明,500ml随手就有
温漂真的明显,冬天标完夏天就偏,重标最实在
E+接反那次读数一直负数,看了这篇才明白
80Hz采样噪声大这个提醒到位,加了滑动平均稳多了