重量传感器套件
简介
重量传感器是一个比较实用、贴近生活场景的传感器,通过获取物体的重量信息能够制作很多有趣的应用,例如家用电子秤、日用品余量报警、物联网称台等。DFRobot商城中已经有了重量传感器和HX711信号转接模块,但是由于结构搭建难度较大,很多无法搭建结构的朋友们就没法使用这款传感器。
这款重量传感器套件,就是为了解决结构搭建难度大的问题,让您能够便捷的制作出一个电子秤,非常容易的就可以获取物体的重量信息。另外,由于每个人的安装方式、使用习惯都不相同,所以传感器需要校准,信号转接板中内置了校准算法,可一键校准重量信息,重量传感器V2.0版本解决了V1.0需要使用单独的校准代码去校准的问题。
注:传感器最大量程为1kg,请勿过多的超出量程,否则会永久性的损坏传感器,导致数据不准
特性
- 搭配支架套件,快速搭建
- 重量校准按键,一键校准重量
- 去皮按键,一键去皮
应用场景
- 简易电子秤
- 日用品余量报警
- 物联网称台
技术规格
测量范围:1g-1000g
工作电压:3.3V/5V
工作电流:<20mA
AD转换芯片:HX711
合成误差:<0.5g(全量程)
组装后整体尺寸:长x宽x高:120X100X30mm
信号转接板尺寸:31*37mm
通信方式:I2C
I2C地址:0x64/0x65/0x660x67
A0 | A1 | 地址 |
---|---|---|
0 | 0 | 0X64 |
1 | 0 | 0X65 |
0 | 1 | 0X66 |
1 | 1 | 0X67 |
引脚及按键

序号 | 名称 | 功能描述 | 序号 | 名称 | 功能描述 |
---|---|---|---|---|---|
1 | VCC | 正极 | 6 | E+ | 传感器红色线 |
2 | GND | 公共地 | 7 | E- | 传感器黑色线 |
3 | SCL | I2C时钟线 | 8 | S- | 传感器白色线 |
4 | SDA | I2C数据线 | 9 | S+ | 传感器绿色线 |
5 | CAL按键 | 自动校准 | 10 | RST按键 | 去皮 |
使用说明
在校准和测量时,物体放在称面中间位置附近会获得最准确的数据
按下rst键后,不管传感器上放了多重的物体,传感器都会清零当前重量值,后面再放上的物体重量会从0开始累积,实现“去皮”功能
Arduino每次复位/重启后,系统都会默认“去皮”一次
传感器校准方法为:往传感器上放一个已知重量的物体,然后“告诉”传感器这个物体的重量是多少,这时传感器就会进行校准(单点校准)
- 其他说明见FAQ
使用教程
准备
硬件
软件
- Arduino IDE,点击下载Arduino IDE
- 点击下载DFRobot_HX711_I2C库文件.关于如何安装库文件,点击链接
常用API接口函数
/*!
* @brief Constructor
* @param pWire I2c controller
* @param addr I2C address(0x64/0x65/0x660x67)
*/
DFRobot_HX711_I2C(TwoWire * pWire = &Wire,uint8_t addr = HX711_I2C_ADDR);
/**
* @brief 初始化函数
* @return 如果初始化成功则返回1,否则返回错误码。
*/
int begin(void);
/**
* @brief 获取物体重量信息
* @param 取平均值的次数
* @return 返回获取的重量值,单位:g
*/
float readWeight(uint8_t times = 12);
/**
* @brief 获取校准值
* @return 返回获取的校准值
*/
float getCalibration();
/**
* @brief 设置/更新校准值
* @param 数值
*/
void setCalibration(float value);
/**
* @brief 设置重量传感器模块自动校准时的触发阈值(g)
* @param 阈值数据
*/
void setThreshold(uint16_t threshold);
/**
* @brief 设置重量传感器模块自动校准时的校准重量(g)
* @param 重量数据
*/
void setCalWeight(uint16_t triWeight);
/**
* @brief 启动校准(软件方式)
*/
void enableCal();
/**
* @brief 去皮(软件方式)
*/
void peel();
/**
* @brief 等待传感器校准完成
* @return 校正完成返回:True
* 校正失败返回:False
*/
bool getCalFlag();
接线图

组装步骤
- step1.将四个垫脚安装在底座的四个角;传感器转接板使用5mm螺丝和螺母固定在底座上;按照接线图接好线;秤杆使用5mm螺丝和亚克力垫片固定在底座上;
注意要使秤杆前面的箭头朝下,连接传感器和秤杆的四根线尽量不要弯折,否则会影响数据精度


- step2.使用4mm螺丝和亚克力垫片将秤面固定在秤杆上方。

自动校准 - 按键
按键校准方式,可以在传感器工作的任意时刻进行校准
step1.安装好套件,清空传感器上除秤面以外其他物体
step2.按照接线图正确连接传感器,并上传示例代码,上传成功后打开串口监视器,波特率调节为9600
step3.按下板子的cal按键,cal旁边的指示灯会亮起,此时进入等待校准状态
step4.在5S内往称台上放一个重100g的物体(100g为默认值,可修改),放上物体1S后cal灯会熄灭,此时进入校准状态,校准完成后指示灯会闪烁三次并熄灭(物体放上后会有1S的延时防抖动时间,所以不要着急,物体放好后在校准完成之前不要触碰)
step5.若按下cal键后,5S内没有放上物体,则指示灯会持续点亮5S并在5S后熄灭,此次校准无效
#include <DFRobot_HX711_I2C.h>
//DFRobot_HX711_I2C MyScale(&Wire,/*addr=*/0x64);
DFRobot_HX711_I2C MyScale;
float Weight = 0;
void setup() {
Serial.begin(9600);
while (!MyScale.begin()) {
Serial.println("The initialization of the chip is failed, please confirm whether the chip connection is correct");
delay(1000);
}
//// Set the calibration weight when the weight sensor module is automatically calibrated (g)
MyScale.setCalWeight(100);
// Set the trigger threshold (G) for automatic calibration of the weight sensor module. When only the weight of the object on the scale is greater than this value, the module will start the calibration process
// This value cannot be greater than the calibration weight of the setCalWeight() setting
MyScale.setThreshold(50);
// Obtain the calibration value. The accurate calibration value can be obtained after the calibration operation is completed
Serial.print("the calibration value of the sensor is: ");
Serial.println(MyScale.getCalibration());
MyScale.setCalibration(MyScale.getCalibration());
delay(1000);
}
void loop() {
Weight = MyScale.readWeight();
Serial.print("weight is: ");
if(Weight > 0.5){
Serial.print(Weight, 1);
}
else{
Serial.print(0, 1);
}
Serial.println(" g");
delay(1000);
}
自动校准 - 软件
软件校准方式,向控制器发送enableCal()指令即可开始校准
step1.安装好套件,清空传感器上除秤面以外其他物体
step2.按照接线图正确连接传感器,并上传示例代码,上传成功后打开串口监视器,波特率调节为9600
step3.等待板子上的CAL灯亮起后往称台上放一个重100g的物体(100g为默认值,可修改)
step4.等待几秒后即可输出校准后的重量值
#include <DFRobot_HX711_I2C.h>
//DFRobot_HX711_I2C MyScale(&Wire,/*addr=*/0x64);
DFRobot_HX711_I2C MyScale;
void setup() {
Serial.begin(9600);
while (!MyScale.begin()) {
Serial.println("The initialization of the chip is failed, please confirm whether the chip connection is correct");
delay(1000);
}
//Set the calibration weight when the weight sensor module is automatically calibrated (g)
MyScale.setCalWeight(100);
// Set the trigger threshold (G) for automatic calibration of the weight sensor module. When only the weight of the object on the scale is greater than this value, the module will start the calibration process
// This value cannot be greater than the calibration weight of the setCalWeight() setting
MyScale.setThreshold(50);
//Start sensor calibration
Serial.println("Start automatic calibration, put the object after the \"CAL\" light is on");
delay(2000);
MyScale.enableCal();
long time1 = millis();
//Wait for sensor calibration to complete
while(!MyScale.getCalFlag()){
delay(1000);
if((millis()-time1) > 7000){
Serial.println("Calibration failed, no weight was detected on the scale,use the last calibration value");
break;
}
}
//Obtain the calibration value. The accurate calibration value can be obtained after the calibration operation is completed
Serial.print("the calibration value of the sensor is: ");
Serial.println(MyScale.getCalibration());
MyScale.setCalibration(MyScale.getCalibration());
}
void loop(){
float Weight = MyScale.readWeight();
Serial.print("weight is: ");
if(Weight > 0.5){
Serial.print(Weight, 1);
}
else{
Serial.print(0, 1);
}
Serial.println(" g");
delay(1000);
}
兼容性
MCU | Work Well | Work Wrong | Untested | Remarks |
Arduino uno | √ | |||
FireBeetle-ESP8266 | √ | |||
FireBeetle-ESP32 | √ | |||
掌控板 | √ | |||
树莓派 | √ | |||
microbit | √ |
FAQ
F1:如何判断自动校准是否成功。
Q1:成功后校准按键上方的指示灯会闪烁三下并熄灭,如果熄灭前没有闪烁三下则表示没有校准成功。
F2:为什么在校准100g物体时,放上了100g物体后没有启动校准,一直校准失败。
Q2:校准时需要有一个“触发重量”,按下校准键,传感器检测到有东西放上去并且超过“触发重量”时才会开始校准。在第一次使用时,因为没有校准,所以放上100g的物体传感器读到的数据可能只有10g,这时需要降低MyScale.setThreshold(50)的值(50改为10或更低),然后重新上传代码。
F3:自动校准成功后输出的重量信息有较大误差(>3g),而且进行了多次校准每次校准成功后输出的重量信息都有较大误差。
Q3:请检查传感器(秤杆)的四根引线不要有过多缠绕,缠绕过多会影响数据。确保秤面已经放平。
F4:不管放不放物体,传感器输出数据一直为0。
Q4:这是由于在Arduino完成复位前就将物体放在秤面上所导致的。解决办法:将秤面上物体取下,按一下Arduino的复位键或者传感器的rst键,重新放上物体即可正常使用。