| 在本篇文章中,我们使用HX711称重传感器放大器模块将40Kg称重传感器连接到NodeMCU ESP8266。 HX711是一款精密的24位模数转换器(ADC),设计用于电子秤和工业控制应用,可直接与桥式传感器连接。 HX711称重传感器放大器用于从称重传感器和应变仪获取可测量的数据。 
 电子称重机使用称重传感器来测量由负载产生的重量,此处大多数称重传感器都参照应变计的方法,该应变计将压力转换为电信号,这些称重传感器具有四个应变计,嵌在惠斯通电桥结构(Wheatstone bridge)中。 
 我们将制作一台电子秤,该机器可以测量高达40KG重量的物品。首先需要校准称重传感器并找到校准系数。校准完成后,可以将该系数包括在代码中。这样,这将使秤精确而准确。重量越大,误差越大。因此,我们将尝试消除秤的误差。最后,我们将在1602 LCD显示屏中显示测得的重量。我们将在称为Blynk应用程序的IoT云平台上发送重量值。这样,只需在Blynk应用程序仪表板上进行观察,就可以从世界任何地方监视重量。 
 所需的组件 ●    NodeMCU ESP8266 ●    称重传感器40公斤 ●    HX711放大器模块 ●    1602 I2C LCD  ●    按钮开关 ●    称重传感器组件底座 
 称重传感器 称重传感器是一种力传感器。它将诸如张力、压力或扭矩之类的力转换为可以测量和标准化的电信号。随着施加到称重传感器上的力增加,电信号按比例变化。称重传感器用于测量重量。 
  称重传感器通常由一个弹簧元件组成,在该弹簧元件上已放置了应变仪。弹簧元件通常由钢或铝制成。这意味着它非常坚固,但弹性也很小。顾名思义,弹簧元件表明,钢在载荷作用下会发生轻微变形,但随后会返回到其初始位置,对每种载荷都具有弹性。这些极小的变化可以通过应变仪获得。然后,最后由分析电子设备解释应变计的变形以确定重量。 
 HX711分线板模块 HX711双通道24位精密AD称重传感器放大器是HX711的小型转接板,可让您轻松读取称重传感器以测量重量。通过将模块连接到微控制器,您将能够读取称重传感器的电阻变化并进行一些校准。您将能够获得非常准确的重量测量值。 
  
 这对于创建自己的工业电子称、过程控制或简单的状态检测非常方便。 HX711称重传感器使用两线接口(时钟和数据)进行通讯。任何微控制器的GPIO引脚都应能正常工作,从而轻松地从HX711读取数据。 
 每种颜色对应于称重传感器的常规颜色编码: 1.  红色(励磁+或VCC)。 2.  黑色(励磁或GND)。 3.  白色(放大器+,信号+或输出+)。 4.  绿色(A-、S-或O-)。 5.  黄色(外壳)。 
 黄色引脚用作可选输入,不连接到应变仪,而是用于接地和屏蔽外部EMI(电磁干扰)。 
 称重传感器和HX711的连接电路:
 
  
 称重传感器底座 
  
 还需要使用螺母和螺栓将基座固定在其上方的称重传感器。在这里,我们使用了一个硬质胶合板作为框架,用于在其上放置物品,并使用了一块轻木板作为基础。这是必需的,因为在称重传感器上施加一定的重量时,称重传感器会略微弯曲。 
 HX711称重传感器和ESP8266的电子秤电路图 以下是将40KG称重传感器和HX711模块与NodeMCU ESP8266 12E开发板连接的电路图。 
  
 上面已经说明了称重传感器和HX711之间的连接。将称重传感器的DT和SCK引脚分别连接到ESP8266的D5和D6引脚。按钮开关连接到ESP8266的数字引脚D4。我使用了1602 LCD显示器来最大程度地减少连接。因此,将I2C LCD显示器的SDA和SCL引脚分别连接到Nodemcu的D2和D1。 
  
 设置Blynk IoT应用程序以进行远程体重监测 Blynk是一个运行在Android和IOS设备上的应用程序,可使用智能手机控制任何基于IoT的应用程序。它允许您为物联网应用程序创建图形用户界面。在这里,我们将设置Blynk应用程序,以使用NodeMCU ESP8266监视通过Wi-Fi测得的重量。 
 因此,请从Google Play商店下载并安装Blynk应用程序。 iOS用户可以从App Store下载。安装完成后,使用您的电子邮件ID和密码打开应用程序并登陆。 
  现在,按照下面的照片设置完整的Blynk应用程序。 
  因此,创建gauge和值显示。成功创建项目后,返回设置并单击“发送电子邮件”。您将在注册邮件中获得一个身份验证ID。保存身份验证ID。您将需要在代码中输入此内容。 
 校准称重传感器的源代码/程序 如上所述连接称重传感器后,您需要先进行校准,然后再进行最终设计。因此,首先要校准整个组装单元。您将需要添加HX711库编译代码。从下面下载HX711库。 ●    HX711库 ●    Blynk ESP8266库 ●    液晶I2C库 复制代码#include "HX711.h"  //You must have this library in your arduino library folder
 
#define DOUT  D5
#define CLK  D6
 
HX711 scale(DOUT, CLK);
 
//Change this calibration factor as per your load cell once it is found you many need to vary it in thousands
float calibration_factor = -109525; //-106600 worked for my 40Kg max scale setup 
 
//=============================================================================================
//                         SETUP
//=============================================================================================
void setup() {
  Serial.begin(9600);
  Serial.println("HX711 Calibration");
  Serial.println("Remove all weight from scale");
  Serial.println("After readings begin, place known weight on scale");
  Serial.println("Press a,s,d,f to increase calibration factor by 10,100,1000,10000 respectively");
  Serial.println("Press z,x,c,v to decrease calibration factor by 10,100,1000,10000 respectively");
  Serial.println("Press t for tare");
  scale.set_scale();
  scale.tare(); //Reset the scale to 0
 
  long zero_factor = scale.read_average(); //Get a baseline reading
  Serial.print("Zero factor: "); //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
  Serial.println(zero_factor);
}
 
//=============================================================================================
//                         LOOP
//=============================================================================================
void loop() {
 
  scale.set_scale(calibration_factor); //Adjust to this calibration factor
 
  Serial.print("Reading: ");
  Serial.print(scale.get_units(), 3);
  Serial.print(" kg"); //Change this to kg and re-adjust the calibration factor if you follow SI units like a sane person
  Serial.print(" calibration_factor: ");
  Serial.print(calibration_factor);
  Serial.println();
 
  if(Serial.available())
  {
    char temp = Serial.read();
    if(temp == '+' || temp == 'a')
      calibration_factor += 10;
    else if(temp == '-' || temp == 'z')
      calibration_factor -= 10;
    else if(temp == 's')
      calibration_factor += 100;  
    else if(temp == 'x')
      calibration_factor -= 100;  
    else if(temp == 'd')
      calibration_factor += 1000;  
    else if(temp == 'c')
      calibration_factor -= 1000;
    else if(temp == 'f')
      calibration_factor += 10000;  
    else if(temp == 'v')
      calibration_factor -= 10000;  
    else if(temp == 't')
      scale.tare();  //Reset the scale to zero
  }
}
 上载校准代码后,请打开串口监视器,并用已知的重量调整比例系数,直到看到正确的读数。按a、s、d、f分别将校准系数增加10、100、100010000。按z、x、c、v分别将校准系数降低10、100、1000、10000。 
  
 一旦看到放置的重量与显示的重量相同,请记下校准系数,并将其用于秤的最终代码中。 
 在Blynk上使用电子称的源代码: 找到校准系数后,请在以下代码中对其进行更新。然后,您只需上传代码即可准备好电子秤。 确保同时在代码上更改WiFi SSID、密码和Blynk身份验证代码。 复制代码#include <ESP8266WiFi.h>;
#include "HX711.h"
#define BLYNK_PRINT Serial
#include <Blynk.h>
#include <BlynkSimpleEsp8266.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define BLYNK_PRINT Serial
const char *ssid = "Alexahome"; // replace with your wifi ssid and wpa2 key
const char *pass = "12345678";
char auth[] = "hePTG-hzhuAzU2h59R90SjQUc98u2Kpb";    // You should get Auth Token in the Blynk App.
WiFiClient client;
HX711 scale(D5, D6);
int rbutton = D4; // this button will be used to reset the scale to 0.
float weight;
float calibration_factor = -101525; // for me this vlaue works just perfect 419640
void setup() 
{
  Serial.begin(115200);
  pinMode(rbutton, INPUT_PULLUP); 
  scale.set_scale();
  scale.tare(); //Reset the scale to 0
  long zero_factor = scale.read_average(); //Get a baseline reading
  Blynk.begin(auth, ssid, pass);
  Wire.begin(D2, D1);
  lcd.begin();
  lcd.setCursor(6,0);
  lcd.print("IOT");
  lcd.setCursor(1,1);
  lcd.print("Weighing Scale");
  delay(3000);
  lcd.clear();
  lcd.print("Connecting Wifi");
  
  WiFi.begin(ssid, pass);
  {
  delay(1000);
  Serial.print(".");
  lcd.clear();
  }
  Serial.println("");
  Serial.println("WiFi connected");
  lcd.clear();
  lcd.print("WiFi connected");
  
  delay(2000);
}
void loop() 
{
  Blynk.run();
  scale.set_scale(calibration_factor); //Adjust to this calibration factor
  weight = scale.get_units(5); 
  lcd.setCursor(0, 0);
  lcd.print("Measured Weight");
  lcd.setCursor(0, 1);
  lcd.print(weight);
  lcd.print(" KG  ");
  Blynk.virtualWrite(V3, weight);
  delay(2000);
  lcd.clear();
  
  Serial.print("Weight: ");
  Serial.print(weight);
  Serial.println(" KG");
  Serial.println();
  
  if ( digitalRead(rbutton) == LOW)
{
  scale.set_scale();
  scale.tare(); //Reset the scale to 0
}
}
 测试结果 当未放置砝码时,显示屏将显示重量等于零。 
  放置一些砝码后,显示屏将显示等于物体重量的值。 
  现在,您可以启动Blynk应用程序,您将在标签显示和gauge上看到重量参数的变化。 
  |