风筝
发表于: 2020-2-27 16:46:21 | 显示全部楼层

在本篇文章中,我们使用HX711称重传感器放大器模块将​​40Kg称重传感器连接到NodeMCU ESP8266。 HX711是一款精密的24位模数转换器(ADC),设计用于电子秤和工业控制应用,可直接与桥式传感器连接。 HX711称重传感器放大器用于从称重传感器和应变仪获取可测量的数据。


电子称重机使用称重传感器来测量由负载产生的重量,此处大多数称重传感器都参照应变计的方法,该应变计将压力转换为电信号,这些称重传感器具有四个应变计,嵌在惠斯通电桥结构(Wheatstone bridge)中。


我们将制作一台电子秤,该机器可以测量高达40KG重量的物品。首先需要校准称重传感器并找到校准系数。校准完成后,可以将该系数包括在代码中。这样,这将使秤精确而准确。重量越大,误差越大。因此,我们将尝试消除秤的误差。最后,我们将在1602 LCD显示屏中显示测得的重量。我们将在称为Blynk应用程序的IoT云平台上发送重量值。这样,只需在Blynk应用程序仪表板上进行观察,就可以从世界任何地方监视重量。


所需的组件

●    NodeMCU ESP8266

●    称重传感器40公斤

●    HX711放大器模块

●    1602 I2C LCD

●    按钮开关

●    称重传感器组件底座


称重传感器

称重传感器是一种力传感器。它将诸如张力、压力或扭矩之类的力转换为可以测量和标准化的电信号。随着施加到称重传感器上的力增加,电信号按比例变化。称重传感器用于测量重量。

40KG-Load-Cell.jpg

称重传感器通常由一个弹簧元件组成,在该弹簧元件上已放置了应变仪。弹簧元件通常由钢或铝制成。这意味着它非常坚固,但弹性也很小。顾名思义,弹簧元件表明,钢在载荷作用下会发生轻微变形,但随后会返回到其初始位置,对每种载荷都具有弹性。这些极小的变化可以通过应变仪获得。然后,最后由分析电子设备解释应变计的变形以确定重量。


HX711分线板模块

HX711双通道24位精密AD称重传感器放大器是HX711的小型转接板,可让您轻松读取称重传感器以测量重量。通过将模块连接到微控制器,您将能够读取称重传感器的电阻变化并进行一些校准。您将能够获得非常准确的重量测量值。

HX711-Module.jpg


这对于创建自己的工业电子称、过程控制或简单的状态检测非常方便。 HX711称重传感器使用两线接口(时钟和数据)进行通讯。任何微控制器的GPIO引脚都应能正常工作,从而轻松地从HX711读取数据。


每种颜色对应于称重传感器的常规颜色编码:

1.  红色(励磁+或VCC)。

2.  黑色(励磁或GND)。

3.  白色(放大器+,信号+或输出+)。

4.  绿色(A-、S-或O-)。

5.  黄色(外壳)。


黄色引脚用作可选输入,不连接到应变仪,而是用于接地和屏蔽外部EMI(电磁干扰)。


称重传感器和HX711的连接电路:

connections-between-Load-cell-and-HX711-module.gif


称重传感器底座

Load-Cell-Base.jpg


还需要使用螺母和螺栓将基座固定在其上方的称重传感器。在这里,我们使用了一个硬质胶合板作为框架,用于在其上放置物品,并使用了一块轻木板作为基础。这是必需的,因为在称重传感器上施加一定的重量时,称重传感器会略微弯曲。


HX711称重传感器和ESP8266的电子秤电路图

以下是将40KG称重传感器和HX711模块与NodeMCU ESP8266 12E开发板连接的电路图。

HX711-Load-Cell-NodeMCU-Circuit-.jpg


上面已经说明了称重传感器和HX711之间的连接。将称重传感器的DT和SCK引脚分别连接到ESP8266的D5和D6引脚。按钮开关连接到ESP8266的数字引脚D4。我使用了1602 LCD显示器来最大程度地减少连接。因此,将I2C LCD显示器的SDA和SCL引脚分别连接到Nodemcu的D2和D1。

1ld.jpg


设置Blynk IoT应用程序以进行远程体重监测

Blynk是一个运行在Android和IOS设备上的应用程序,可使用智能手机控制任何基于IoT的应用程序。它允许您为物联网应用程序创建图形用户界面。在这里,我们将设置Blynk应用程序,以使用NodeMCU ESP8266监视通过Wi-Fi测得的重量。


因此,请从Google Play商店下载并安装Blynk应用程序。 iOS用户可以从App Store下载。安装完成后,使用您的电子邮件ID和密码打开应用程序并登陆。

1.jpg

现在,按照下面的照片设置完整的Blynk应用程序。

Blynk1.jpg

因此,创建gauge和值显示。成功创建项目后,返回设置并单击“发送电子邮件”。您将在注册邮件中获得一个身份验证ID。保存身份验证ID。您将需要在代码中输入此内容。


校准称重传感器的源代码/程序

如上所述连接称重传感器后,您需要先进行校准,然后再进行最终设计。因此,首先要校准整个组装单元。您将需要添加HX711库编译代码。从下面下载HX711库。

●    HX711库

●    Blynk ESP8266库

●    液晶I2C库

  1. #include "HX711.h"  //You must have this library in your arduino library folder

  2. #define DOUT  D5
  3. #define CLK  D6

  4. HX711 scale(DOUT, CLK);

  5. //Change this calibration factor as per your load cell once it is found you many need to vary it in thousands
  6. float calibration_factor = -109525; //-106600 worked for my 40Kg max scale setup

  7. //=============================================================================================
  8. //                         SETUP
  9. //=============================================================================================
  10. void setup() {
  11.   Serial.begin(9600);
  12.   Serial.println("HX711 Calibration");
  13.   Serial.println("Remove all weight from scale");
  14.   Serial.println("After readings begin, place known weight on scale");
  15.   Serial.println("Press a,s,d,f to increase calibration factor by 10,100,1000,10000 respectively");
  16.   Serial.println("Press z,x,c,v to decrease calibration factor by 10,100,1000,10000 respectively");
  17.   Serial.println("Press t for tare");
  18.   scale.set_scale();
  19.   scale.tare(); //Reset the scale to 0

  20.   long zero_factor = scale.read_average(); //Get a baseline reading
  21.   Serial.print("Zero factor: "); //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
  22.   Serial.println(zero_factor);
  23. }

  24. //=============================================================================================
  25. //                         LOOP
  26. //=============================================================================================
  27. void loop() {

  28.   scale.set_scale(calibration_factor); //Adjust to this calibration factor

  29.   Serial.print("Reading: ");
  30.   Serial.print(scale.get_units(), 3);
  31.   Serial.print(" kg"); //Change this to kg and re-adjust the calibration factor if you follow SI units like a sane person
  32.   Serial.print(" calibration_factor: ");
  33.   Serial.print(calibration_factor);
  34.   Serial.println();

  35.   if(Serial.available())
  36.   {
  37.     char temp = Serial.read();
  38.     if(temp == '+' || temp == 'a')
  39.       calibration_factor += 10;
  40.     else if(temp == '-' || temp == 'z')
  41.       calibration_factor -= 10;
  42.     else if(temp == 's')
  43.       calibration_factor += 100;  
  44.     else if(temp == 'x')
  45.       calibration_factor -= 100;  
  46.     else if(temp == 'd')
  47.       calibration_factor += 1000;  
  48.     else if(temp == 'c')
  49.       calibration_factor -= 1000;
  50.     else if(temp == 'f')
  51.       calibration_factor += 10000;  
  52.     else if(temp == 'v')
  53.       calibration_factor -= 10000;  
  54.     else if(temp == 't')
  55.       scale.tare();  //Reset the scale to zero
  56.   }
  57. }
复制代码

上载校准代码后,请打开串口监视器,并用已知的重量调整比例系数,直到看到正确的读数。按a、s、d、f分别将校准系数增加10、100、100010000。按z、x、c、v分别将校准系数降低10、100、1000、10000。

Calibration.jpg


一旦看到放置的重量与显示的重量相同,请记下校准系数,并将其用于秤的最终代码中。


在Blynk上使用电子称的源代码:

找到校准系数后,请在以下代码中对其进行更新。然后,您只需上传代码即可准备好电子秤。

确保同时在代码上更改WiFi SSID、密码和Blynk身份验证代码。

  1. #include <ESP8266WiFi.h>;
  2. #include "HX711.h"
  3. #define BLYNK_PRINT Serial
  4. #include <Blynk.h>
  5. #include <BlynkSimpleEsp8266.h>
  6. #include <Wire.h>
  7. #include <LiquidCrystal_I2C.h>
  8. LiquidCrystal_I2C lcd(0x27, 16, 2);
  9. #define BLYNK_PRINT Serial

  10. const char *ssid = "Alexahome"; // replace with your wifi ssid and wpa2 key
  11. const char *pass = "12345678";
  12. char auth[] = "hePTG-hzhuAzU2h59R90SjQUc98u2Kpb";    // You should get Auth Token in the Blynk App.

  13. WiFiClient client;

  14. HX711 scale(D5, D6);

  15. int rbutton = D4; // this button will be used to reset the scale to 0.
  16. float weight;
  17. float calibration_factor = -101525; // for me this vlaue works just perfect 419640

  18. void setup()
  19. {
  20.   Serial.begin(115200);
  21.   pinMode(rbutton, INPUT_PULLUP);
  22.   scale.set_scale();
  23.   scale.tare(); //Reset the scale to 0
  24.   long zero_factor = scale.read_average(); //Get a baseline reading
  25.   Blynk.begin(auth, ssid, pass);
  26.   Wire.begin(D2, D1);
  27.   lcd.begin();
  28.   lcd.setCursor(6,0);
  29.   lcd.print("IOT");
  30.   lcd.setCursor(1,1);
  31.   lcd.print("Weighing Scale");
  32.   delay(3000);
  33.   lcd.clear();

  34.   lcd.print("Connecting Wifi");
  35.   
  36.   WiFi.begin(ssid, pass);
  37.   {
  38.   delay(1000);
  39.   Serial.print(".");
  40.   lcd.clear();
  41.   }
  42.   Serial.println("");
  43.   Serial.println("WiFi connected");
  44.   lcd.clear();
  45.   lcd.print("WiFi connected");
  46.   
  47.   delay(2000);
  48. }

  49. void loop()

  50. {

  51.   Blynk.run();
  52.   scale.set_scale(calibration_factor); //Adjust to this calibration factor

  53.   weight = scale.get_units(5);

  54.   lcd.setCursor(0, 0);
  55.   lcd.print("Measured Weight");
  56.   lcd.setCursor(0, 1);
  57.   lcd.print(weight);
  58.   lcd.print(" KG  ");
  59.   Blynk.virtualWrite(V3, weight);
  60.   delay(2000);
  61.   lcd.clear();
  62.   
  63.   Serial.print("Weight: ");
  64.   Serial.print(weight);
  65.   Serial.println(" KG");
  66.   Serial.println();
  67.   

  68.   if ( digitalRead(rbutton) == LOW)
  69. {
  70.   scale.set_scale();
  71.   scale.tare(); //Reset the scale to 0
  72. }

  73. }
复制代码

测试结果

当未放置砝码时,显示屏将显示重量等于零。

2ws.jpg

放置一些砝码后,显示屏将显示等于物体重量的值。

3s.jpg

现在,您可以启动Blynk应用程序,您将在标签显示和gauge上看到重量参数的变化。

Blynk.jpg

跳转到指定楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 700 | 回复: 1479



手机版|

GMT+8, 2024-3-28 16:58 , Processed in 0.148400 second(s), 6 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

快速回复 返回顶部 返回列表