跳到主要內容

選修分組-專題 / OLD / 電子秤

Arduino HX711電子秤完整範例

專題程式下載( 電子秤 )

要注意電子秤、LCD接接腳必須正確。  

1

LiquidCrystal_PCF8574.h

1

 

1

 

/*
  本範程式HX711完整範例,會把重量顯示在LCD中  
 */
 
#include "HX711.h"
#include
LiquidCrystal_PCF8574 lcd(0x27);  // 設定i2c位址,一般情況就是0x27和0x3F兩種
 
// 接線如下:
const int DT_PIN = 6;
const int SCK_PIN = 5;
const int scale_factor = 452; //從實際量測值取得
HX711 scale;
void setup() {
  Serial.begin(9600);
  Serial.println("初始化電子秤 ");
 
  scale.begin(DT_PIN, SCK_PIN); //初始化電子秤
  lcd.begin(16, 2);             // 初始化LCD
  lcd.setBacklight(255);
  lcd.clear();                  //清除LCD字幕
  Serial.println("設定電子秤參數前:"); 
  Serial.println(scale.get_units(5), 0); //顯示未設定比例參數前的數值
  scale.set_scale(scale_factor);          // 設定比例參數
  scale.tare();                     // 歸零
  Serial.println("設定比例參數後的數值:"); 
  Serial.println(scale.get_units(5), 0);  //設定比例參數後的數值
  Serial.println("校正中:");  //在這個訊息之前都不要放東西在電子稱上
}
 
void loop() {  
  Serial.println(scale.get_units(10), 0); 
  lcd.clear();
  lcd.setCursor(0, 0);  //設定游標位置 (字,行)
  lcd.print("Weight: ");
  lcd.setCursor(9, 0);
  float weight = scale.get_units(10);
  //避免出現負數
  if(weight<=0) weight = 0;
  lcd.print(weight,0);
  lcd.setCursor(13, 0);
  lcd.print("g");
  scale.power_down();         // 進入睡眠模式
  delay(200);
  scale.power_up();               // 結束睡眠模式
}

 

 

 

時間類別單位標題發佈點閱
跳至網頁頂部