Arduino 电子秤 HX711

Arduino 电子秤 HX711

1、称重模块采用HX711芯片。

2、代码。

#include "HX711.h" //调用24bitAD HX711库

HX711 HX711_CH0(2, 3, 345); //SCK,DT,GapValue

//SCK引脚用于arduino和HX711模块通讯的时序提供
//DT引脚用于从HX711读取AD的数据
//GapValue用于校准输出的重量值,如果数值偏大就加大该值,如果数据偏小就减小该值
int LED = 13;


long Weight = 0;		//定义一个变量用于存放承重的重量,单位为g


void setup()
{
  Serial.begin(9600);						//设定串口输出波特率
  Serial.println("Welcome to use!");
  Serial.println("Init...");

  HX711_CH0.begin();					//读取传感器支架毛重
  delay(3000);						//延时3s用于传感器稳定
  HX711_CH0.begin();					//重新读取传感器支架毛重用于后续计算

  Serial.println("Init OK!!!");
}

void loop()
{

  Weight = HX711_CH0.Get_Weight();		//采样当前传感器重量,该重量已经自动去皮,去皮值根据初始化程序中采样的值计算。
  Serial.print(Weight);			//串口输出当前重量
  Serial.println(" g");			//单位为g
  delay(1000);
}

【2023-04-02 更新】

有多位同学咨询HX711库文件,因此将库文件上传。HX711库文件下载

①HX711.h

#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

#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);
}

 

4条评论

发表回复

您的电子邮箱地址不会被公开。