想给储物格或者机器人加一个"超了就喊"的重量监测,最省事的路径就是称重传感器加HX711放大器,几十块钱能搭出一台能读克数的电子秤。这篇把接线、装库、校准系数怎么算、读重量,以及最后加一个简易阈值报警的思路一次讲完,关键参数都对着原教程逐项核过。

先弄清楚:称重传感器和HX711各干什么活
称重传感器(load cell)本体是一根金属梁,上面贴着应变片:受力和电阻变化成正比,梁内一般有四个应变片组成惠斯通电桥,输出的毫伏级差分信号没法直接读。线序是固定的四色:红线VCC(E+)、黑线GND(E-)、白线Output-(A-)、绿线Output+(A+)。
这套组合能干的事不少:监测一个物体重量随时间的变化、直接称重、判断"上面有没有放东西",还可以估算容器里的液位余量。想让机器人知道自己的载重变化,也是同一套逻辑。
信号太小,就要放大器出场。HX711就是专门干这事的模块:一侧接称重传感器的四根线,另一侧用两根线(Clock和Data)与主控通信,把微弱差分信号放大后转成数字量。按HX711数据手册,它是一颗24位ADC,这是这套方案精度的底子。买的时候注意量程,常见有1kg、5kg、10kg、20kg几档,按要称的东西选。

接线与安装:四根线、四个脚
先把传感器架起来。原教程用的是带两块亚克力板的套件:下板固定梁的一端,上板放重物,两端受力才能产生形变。没有套件用木板或3D打印一对板子也行,原理一样。

HX711模块的GND、DT、SCK、VCC四个脚需要自己焊排针,传感器的四根线则直接焊到E+、E-、A-、A+上。接线照表格插就行。主控这边原教程用ESP32、走Arduino框架写代码:HX711的DT接GPIO 16、SCK接GPIO 4;官方说明这两个脚可以换,换到经典Arduino开发板也一样,把代码里那两个引脚常量改成你用的脚即可。
| 称重传感器 | HX711 | HX711 | 主控(ESP32) |
|---|---|---|---|
| 红线(E+) | E+ | GND | GND |
| 黑线(E-) | E- | DT | GPIO 16 |
| 白线(A-) | A- | SCK | GPIO 4 |
| 绿线(A+) | A+ | VCC | 3.3V |

一个提醒:传感器引出的四根线又细又脆,焊接时手要稳,拽断了就只能换梁。
引脚选择上只有一条约束:DT和SCK是普通数字IO就行,ESP32侧官方说明任何GPIO都可用,具体避开哪些特殊功能脚可以对照它的引脚图确认;换到经典Arduino开发板,随便挑两个数字脚,同步改代码顶部的LOADCELL_DOUT_PIN和LOADCELL_SCK_PIN两个常量即可,其余一行不用动。
装库和校准:系数必须自己算
库用Bogdan Necula写的HX711库(bogde/HX711),明确兼容ESP32、ESP8266和Arduino。Arduino IDE里打开库管理器搜"HX711 Arduino Library",装Bogdan Necula那个;用PlatformIO的话在platformio.ini里加一行lib_deps = bogde/HX711@^0.7.5,再补monitor_speed = 115200。

校准这步不能跳,每只传感器的系数都不一样。流程是:准备一个已知重量的东西(原教程用厨房秤称了一杯300克的水),烧校准程序,先去皮再放重物,记下串口输出的原始读数,用读数除以已知重量就得到校准系数。原教程那组数据是-141449除以300,算出-471.497;你的数大概率是另一个值,别直接抄。单位随意,用克、千克、磅都行,读数和重量成比例。
烧好程序打开串口监视器,波特率选115200,复位开发板,按提示走:先移走秤上所有东西(程序自动去皮),等5秒,再放已知重量的物体,读数就出来了。算完系数先别急着用,拿这个已知重量的物体复称一遍,偏差明显就重校一次——厨房秤本身也有误差,两边对不上很常见。

校准程序如下,头文件一律写成引号形式(与尖括号写法编译等价,入库时不会被剥掉):
/*
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)读重量:库自带的示例代码
拿到系数之后,烧库自带的示例,把set_scale()里那句INSERT YOUR CALIBRATION FACTOR换成你自己的数。这段示例把库的常用函数几乎演示了一遍:read()是原始ADC读数,read_average(20)是20次平均,get_value()去掉了皮重,get_units()再除以校准系数,也就是真正的重量。最后那段power_down()和power_up()是ADC休眠,电池供电场景用得上。
/*
* 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();
}示例特意在set_scale()前后各打印了一轮读数,这个对比值得看:set_scale之前,get_units()除的是默认系数,数字没有物理意义;set_scale(校准系数)加上tare()之后,同样的原始ADC值就换算成了克。理解了这条换算链,后面不管换什么量程的梁,调参思路都一样。

想做成品秤,原教程还有一个进阶版:加一块SSD1306的OLED屏(I2C总线,地址0x3C)显示实时克数,再在GPIO 19上接一个按键做去皮,按键库用的是pololu的Pushbutton。核心改动就是loop()里轮询按键、读到变化就刷新屏幕,代码如下:
/*
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.
*/
// Library HX711 by Bogdan Necula: https://github.com/bogde/HX711
// Library: pushbutton by polulu: https://github.com/pololu/pushbutton-arduino
#include "Arduino.h"
#include "HX711.h"
#include "soc/rtc.h"
#include "Wire.h"
#include "Adafruit_GFX.h"
#include "Adafruit_SSD1306.h"
#include "Pushbutton.h"
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 16;
const int LOADCELL_SCK_PIN = 4;
HX711 scale;
int reading;
int lastReading;
//REPLACE WITH YOUR CALIBRATION FACTOR
#define CALIBRATION_FACTOR -471.497
//OLED Display
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//Button
#define BUTTON_PIN 19
Pushbutton button(BUTTON_PIN);
void displayWeight(int weight){
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 10);
// Display static text
display.println("Weight:");
display.display();
display.setCursor(0, 30);
display.setTextSize(2);
display.print(weight);
display.print(" ");
display.print("g");
display.display();
}
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);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
display.setTextColor(WHITE);
Serial.println("Initializing the scale");
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(CALIBRATION_FACTOR); // this value is obtained by calibrating the scale with known weights; see the README for details
scale.tare(); // reset the scale to 0
}
void loop() {
if (button.getSingleDebouncedPress()){
Serial.print("tare...");
scale.tare();
}
if (scale.wait_ready_timeout(200)) {
reading = round(scale.get_units());
Serial.print("Weight: ");
Serial.println(reading);
if (reading != lastReading){
displayWeight(reading);
}
lastReading = reading;
}
else {
Serial.println("HX711 not found.");
}
}OLED走I2C,SDA、SCL对应接上就行,四根线解决显示。重点看loop()里这三行:button.getSingleDebouncedPress()检测按键触发去皮,scale.wait_ready_timeout(200)等模块就绪,reading = round(scale.get_units())拿到取整后的克数。阈值报警要用的也是这个reading,思路接着就来了。
简易阈值报警怎么做,外加几个坑
报警不需要加什么硬件逻辑,思路就是把loop()里scale.get_units()返回的克数和一个设定阈值做比较:超过就点亮某个GPIO上接的LED或蜂鸣器,串口同时打一条提示,低于阈值再关掉。想稳一点就改用get_units(10)取十次平均再判断,避免毛刺误触发。这些都是在示例loop()里加几句判断的事,库的核心调用一行不用动。
阈值怎么定看量程和用途:5kg的梁做储物格超重提醒,可以设在4kg附近留余量;做"有没有放东西"的检测,阈值就压得很低。判断逻辑建议做一点滞回,比如超过4000克触发、低于3800克才解除,避免读数在阈值附近来回抖导致报警反复横跳。
几个坑提前说:第一,校准系数人人不同,数值离谱先回校准那步;第二,串口波特率是115200,开错只会看到乱码;第三,setup()里那一串rtc_clk_cpu_freq调用是把ESP32主频固定到80MHz,库文档给的稳定做法,别当成废代码删了;第四,用wait_ready_timeout()判断模块在不在,比直接read()友好——模块没接好会提示HX711 not found,而不是卡死;第五,换量程的传感器后整套装上下都要重新校准,20kg的梁配1kg的系数读出来必然不对。
整套下来硬件成本不高,功夫主要花在校准上。储物格超重提醒、容器液位余量估算、给机械臂末端加载重反馈,都是现成的落点,把校准做扎实、读数取平均,这套小秤就能稳定干活。











校准系数这块写得很实在,第一次玩HX711的人十有八九会直接抄别人的系数,然后纳闷为什么读数不对。负数系数也挺反直觉,其实就跟你接传感器时把激励电压接反了一个道理,读数符号跟着反,除出来自然带负号,不影响使用。
那个80MHz主频固定的骚操作我在别处没见过人讲,原来HX711时序对主频敏感,学到了
线细易断这条是真的,我上回焊称重传感器直接拽断一根白线,最后飞了根漆包线救回来,心疼
阈值报警做滞回这个建议好,不做的话读数卡在阈值附近,蜂鸣器能给你吵到拆秤