[Arduino]電子秤平 重量感測條+HX711AD模組
※此重量感測條為3kg
接法:
重量感測條-->HX711-->ARDUINO
重量感測條 HX711
紅------------>E
黑------------>E
白------------>A-
綠------------>A+
HX711 ARDUINO
GND-------->GND
DT----------->3
SCK--------->2
VCC-------->5V
#include "HX711.h" HX711 HX711_CH0(2, 3, 750); //SCK,DT,GapValue //SCK引腳用於arduino和HX711模塊通訊的時序提供 //DT引腳用於從HX711讀取AD的數據 //GapValue用於校準輸出的重量值,如果數值偏大就加大該值,如果數據偏小就減小該值 int LED = 13; //ARDUINO LED燈,亮起時代表初始化完成可以負重 long Weight = 0; //定義一個變量用於存放承重的重量,單位為g void setup() { pinMode(LED, OUTPUT); //設定LED是輸出引腳 digitalWrite(LED, LOW); //LED熄滅 Serial.begin(9600); //設定串口輸出波特率 HX711_CH0.begin(); //讀取傳感器重量 delay(3000); //延時3s用於傳感器穩定 HX711_CH0.begin(); //重新讀取傳感器支架毛重用於後續計算 digitalWrite(LED, HIGH); //板載LED點亮,說明可以承重 } void loop() { Weight = HX711_CH0.Get_Weight(); //當前傳感器重量,該重量已經扣除支架重量 Serial.print(Weight); //串口輸出當前重量 Serial.println(" g"); //單位為g delay(1000); }
調用函式庫:
HX711.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __HX711__H__ | |
#define __HX711__H__ | |
#include <Arduino.h> | |
class HX711 | |
{ | |
public: | |
HX711(int SCK_PIN,int DT_PIN,float GapValueIn=44); | |
long Get_Weight(); | |
void begin(); | |
int Pressed(int AlarmValue); | |
int HX711_SCK; | |
int HX711_DT; | |
float ValueGap; | |
long HX711_Buffer; | |
long Weight_Maopi; | |
long Weight_Shiwu; | |
int CurrentAlarm; | |
private: | |
void Get_Maopi(); | |
unsigned long HX711_Read(); | |
}; | |
#endif |
---
HX711.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "hx711.h" | |
HX711::HX711(int SCK_PIN,int DT_PIN,float GapValueIn) | |
{ | |
HX711_SCK = SCK_PIN; | |
HX711_DT = DT_PIN; | |
ValueGap = GapValueIn; | |
} | |
//**************************************************** | |
//初始化HX711 | |
//**************************************************** | |
void HX711::begin() | |
{ | |
pinMode(HX711_SCK, OUTPUT); | |
pinMode(HX711_DT, INPUT); | |
Get_Maopi(); | |
} | |
int HX711::Pressed(int AlarmValue) | |
{ | |
if(Get_Weight() >= AlarmValue && CurrentAlarm == 0) | |
{ | |
CurrentAlarm = 1; | |
return 1; | |
} | |
else if(Get_Weight() < AlarmValue) | |
{ | |
CurrentAlarm = 0; | |
return 0; | |
} | |
return 0; | |
} | |
//**************************************************** | |
//獲取毛皮重量 | |
//**************************************************** | |
void HX711::Get_Maopi() | |
{ | |
Weight_Maopi = HX711_Read(); | |
} | |
//**************************************************** | |
//稱重 | |
//**************************************************** | |
long HX711::Get_Weight() | |
{ | |
HX711_Buffer = HX711_Read(); | |
Weight_Shiwu = HX711_Buffer; | |
Weight_Shiwu = Weight_Shiwu - Weight_Maopi; //獲取實物的AD采樣數值。 | |
Weight_Shiwu = (long)((float)Weight_Shiwu/ValueGap+0.05); | |
return Weight_Shiwu; | |
} | |
//**************************************************** | |
//讀取HX711 | |
//**************************************************** | |
unsigned long HX711::HX711_Read() //增益128 | |
{ | |
unsigned long count; | |
unsigned char i; | |
bool Flag = 0; | |
digitalWrite(HX711_DT, HIGH); | |
delayMicroseconds(1); | |
digitalWrite(HX711_SCK, LOW); | |
delayMicroseconds(1); | |
count=0; | |
while(digitalRead(HX711_DT)); | |
for(i=0;i<24;i++) | |
{ | |
digitalWrite(HX711_SCK, HIGH); | |
delayMicroseconds(1); | |
count=count<<1; | |
digitalWrite(HX711_SCK, LOW); | |
delayMicroseconds(1); | |
if(digitalRead(HX711_DT)) | |
count++; | |
} | |
digitalWrite(HX711_SCK, HIGH); | |
delayMicroseconds(1); | |
digitalWrite(HX711_SCK, LOW); | |
delayMicroseconds(1); | |
count ^= 0x800000; | |
return(count); | |
} |
---
請問這三段程式是一次打下來嗎?
回覆刪除