HX711称重传感器接Arduino:电子秤校准与读数

2026-11-15 20:35:01 53阅读 所属项目:ESP32 开发板

想给桌面加一台能精确到克的电子秤,最省的攒法就是应变片称重传感器加HX711放大模块:便宜到不像话,接线不到十根,串口一开就能出数。这篇把整条路从头理顺——器件原理、接线、装库、校准系数怎么算,最后升级到带OLED屏和去皮按键的成品秤。代码全部原样搬运,一个字符没动,照着抄就能跑。

HX711称重传感器接Arduino电子秤组合

称重传感器、HX711放大模块与主控的组合,就是一台电子秤的全部家当

称重传感器和HX711是怎么配合的

常见的称重传感器本质是一根金属条,里面贴着应变片,受力和电阻变化成正比。光有它不行——形变量实在太小,输出的差分信号只有毫伏级,主控的ADC根本读不干净。所以中间必须垫一层HX711:一头接传感器的四根线,一头用两根线(时钟加数据)跟主控通信,先把微弱信号放大再数字化。

称重传感器惠斯通电桥结构示意

应变片构成惠斯通电桥,受力后输出差分小信号

量程按需选,市面常见1kg、5kg、10kg、20kg几档。很多套件随附两块亚克力板和螺丝:下板固定传感器,上板放重物,装配基本零门槛,用木板或3D打印件也行。传感器那四根线极细,焊接时别反复弯折,一扯就断。

接线:四根应变线加两根信号脚

传感器到HX711按线色对号入座:红色接E+,黑色接E-,白色接A-,绿色接A+。这是最常见的配色,万一手里那颗线色不同,以实物丝印为准。

称重传感器与HX711接线图

HX711与主控的接线示意,DT和SCK各占一个GPIO

HX711到主控这边:VCC接3.3V,GND接GND,DT(数据)接GPIO 16,SCK(时钟)接GPIO 4。这两对引脚写在示例代码的宏定义里,想换引脚只改宏即可。用其他开发板也是同一个思路:挑两个空闲数字引脚,代码里同步改掉。

装HX711库,先跑校准拿系数

库管理器里搜"HX711 Arduino Library"直接安装,作者Bogdan Necula(GitHub上叫bogde),ESP32、ESP8266、Arduino通吃。

Arduino IDE库管理器安装HX711库

库管理器里搜HX711,选bogde维护的版本安装

装好后先跑下面这段校准代码,把原始读数摸出来(代码里的头文件从尖括号写法换成了双引号,两种写法在Arduino工具链下等价,除此之外逐字符未动):

/*
  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)

上传后打开串口监视器,波特率115200,板子复位一下。流程是全自动的:先提示清空秤面,五秒后去皮,再提示放已知重量的东西上去,然后打出原始读数。下面这张演示图里,读数是-141449,放上去的是一杯300克的水。

校准过程串口输出演示

串口读到的原始值-141449,对应300克已知重量

校准系数一除就有:原始读数除以已知重量,-141449 ÷ 300 ≈ -471.497。每颗传感器的系数都不一样,别抄别人的数,这步也千万别跳过——不做校准,出来的数字就是一堆没有意义的原始值。

把系数填进去,读数直接换算成克

系数填进set_scale()再烧一遍,串口里出来的就是克数了:

/*
 * 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();
}

这段代码好玩的地方在于它把库的核心方法串讲了一遍:setup()阶段依次打出裸读数read()、20次平均read_average()、减去皮重的get_value()、再除以系数的get_units(),等于把从原始值到克数的每一步都摊开给你看。loop()里每五秒用power_down()和power_up()让ADC睡一觉再醒,顺便省电。set_scale()括号里那行占位符记得换成你自己算出的系数。

称重代码串口输出演示

烧好称重代码后,串口直接打出换算成克的读数

还有一处细节:代码里rtc_clk开头那几行是把ESP32主频降到80MHz——有人反馈HX711的时序在某些主频下读数不稳,原作者实测降不降都正常,嫌碍事可以删掉。这几行是ESP32专属的,其他板子本来也没有。

升级成带OLED屏和去皮键的成品秤

串口秤只能算半成品,加一块SSD1306 OLED和一个按键才算顺手:OLED走I2C,SDA接GPIO 21、SCL接GPIO 22,I2C地址0x3C;按键一端接GPIO 19,另一端接3.3V,并经10kΩ电阻下拉到GND。库管理器里再装Adafruit SSD1306、Adafruit GFX和Pololu的Pushbutton三个库。

OLED电子秤成品演示

读数实时显示在OLED上,按键一按即去皮归零

完整代码如下:

/*
  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 pololu: 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.");
  }
}

几个值得抄的设计:校准系数定义成CALIBRATION_FACTOR宏,换秤只改一处;loop()里用wait_ready_timeout(200)做非阻塞等待,秤没接上程序也不会挂死;读数取整到克,比浮点稳得多——小数点后那几位晃来晃去只会让人焦虑,要小数可以自己换回float;OLED只在读数变化时重绘,刷屏不频繁;按键去皮交给Pushbutton库的getSingleDebouncedPress()消抖,不用自己写延时判断。

后续玩法不少:把读数挂到网页上做个物联网秤、往Firebase写历史曲线、重量低于阈值推送提醒,这套硬件顺手就能扩。液体余量估算、猫粮碗监控、蒸发速率测量这类需求,本质都是同一颗传感器换个装法。十几块钱的器件,校准对了比很多成品秤还能打,这就是开源硬件最迷人的地方。


作者:野生极客一枚
链接:https://www.aiddithome.com/p/5f9190c329ed7.html
本作版权归作者所有,未经允许请勿转载。若存在违规行为,您可以点击 “举报”。


登录 后发表评论
5条评论
阿伟不emo
1楼 · 2026-11-15 20:38:00

校准这一步真是整个方案的灵魂,我第一次玩称重传感器就是跳过了校准直接读数,出来一堆六位数的原始值,还以为是模块坏了。后来看了别人的教程才知道要用已知重量去除一下,拿家里厨房秤称了杯水当砝码,-141449除以300那个例子特别直观。建议新手别怕麻烦,校准做一次后面就全是快乐,去皮按键按一下归零的感觉太解压了。

探头看一眼
2楼 · 2026-11-15 20:38:00

那几行rtc降频的代码原来可删可留,我一直以为必须加,谢谢提醒

调参到凌晨
3楼 · 2026-11-15 20:38:00

wait_ready_timeout这个设计确实该学,我早期写的版本秤一断线程序就卡死在循环里,查了半天才定位到

隔壁工位的好奇宝
4楼 · 2026-11-15 20:38:00

线色对号入座那段很实用,红E+黑E-白A-绿A+,不过真遇到过杂牌线色乱来的,丝印为准没毛病

装过才知道
5楼 · 2026-11-15 20:38:00

十几块钱的秤校准完精度居然还行,血赚