秤重感測器-HX711-ESP32

 秤重感測器-HX711-ESP32

簡單介紹:

稱重傳感器將力轉換為可測量的電信號。電信號與施加的力成比例變化。有不同類型的稱重傳感器:應變計、氣動和液壓。在本教程中,我們將介紹應變式稱重傳感器。
應變計稱重傳感器由一個帶有應變計的金屬棒組成(在上圖中的白膠下方)。應變計是一種電子傳感器,用於測量物體上的力或應變。當對物體施加外力時,應變儀的電阻會發生變化,從而導致物體的形狀(在本例中為金屬棒)變形。電阻的變化與施加的負載成正比,這使我們能夠計算物體的重量。

通常,稱重傳感器有四個應變計連接在惠斯通電橋中(如下所示),使我們能夠獲得準確的電阻測量值。

接線方式:

紅色:VCC (E+)
黑色:GND (E-)
白色:輸出 - (A-)
綠色:輸出 + (A+)



安裝所須的函式庫

使用Arduino IDE
打開 Arduino IDE 並轉到 Sketch >Include Library> Manage Libraries。
搜索“HX711 Arduino Library”並安裝 Bogdan Necula 的庫。


或者也可以使用vsCode

可以在以下路徑看到波特率設制成15200,以及剛安裝好的庫


程式碼:

#include <Arduino.h>
#include "HX711.h"

// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 33;
const int LOADCELL_SCK_PIN = 32;

HX711 scale;

void setup() {
  Serial.begin(115200);
 
  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(475.4);
  //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();
}




以上的程式碼,可以發現數值並未完全歸零,而且秤出來和實際的重量會有誤差

解析:

匯入HX711.h模組
#include "HX711.h"

腳位設置
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 33;
const int LOADCELL_SCK_PIN = 32;

setup()
通過調用 scale 對象的 begin() 方法並將 GPIO 作為參數傳遞來初始化稱重傳感器。
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);


它調用幾個方法,您可以使用這些方法來使用庫獲取讀數。

read():從傳感器獲取原始讀數
read_average(number of readings):獲取最新定義的讀數的平均值
get_value(number of readings):得到最後定義的讀數減去皮重的平均值;
get_units(讀數):獲取最後定義的讀數數減去皮重除以校準因子的平均值——這將以您所需的單位輸出讀數
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)

在下面的行中,不要忘記插入您的校準因子。它使用 set_scale() 方法。
其挎號裡的值,視自己使用標準電秤量測出來的值填入挎號中。

scale.set_scale(475.4);
  //scale.set_scale(475.4);

然後,調用 tare() 方法對秤進行去皮。
 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:");


loop()
在 loop() 中,該示例以兩種不同的方式調用 get_units() 方法:獲取單個讀數(不帶任何參數)和獲取最後 10 個讀數的平均值。

Serial.print("one reading:\t");
  Serial.print(scale.get_units(), 1);
  Serial.print("\t| average:\t");
  Serial.println(scale.get_units(10), 5);

它使用 power_down() 方法關閉讀取傳感器的 ADC。然後,它等待 5 秒,為 ADC 加電 (power_up()),然後循環 () 重複。因此,您將每 5 秒在串行監視器上獲得新讀數。

scale.power_down();             // put the ADC in sleep mode
  delay(5000);
  scale.power_up();

最後把它做個簡化:

#include "HX711.h"

// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 33;
const int LOADCELL_SCK_PIN = 32;

HX711 scale;

void setup() {
  Serial.begin(115200);
  Serial.println("HX711 Demo");
  Serial.println("Initializing the scale");
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale(475.5);  
  scale.tare();               // reset the scale to 0
  Serial.println("Readings:");
}

void loop() {
  Serial.print("秤重數值:\t");
  Serial.print(scale.get_units(10), 1);
  Serial.println("g");
  scale.power_down();             // put the ADC in sleep mode
  delay(5000);
  scale.power_up();
}


留言

這個網誌中的熱門文章