HX711 Weight Sensor Kit Arduino

DFPlayer Mini

Introduction

The weight sensor is a practical sensor close to daily life scenes. It can make many interesting applications by acquiring the weight information of objects, such as household electronic scales, daily necessities remaining alarms, and Internet of Things weighing platforms.

This weight sensor kit is equipped with all the accessories needed to build the scale, which reduces the difficulty of structure construction, so that anyone can quickly create an electronic scale to obtain the weight information of the object. In addition, because everyone has different installation methods and usage habits, the sensor needs to be calibrated. The traditional method is to use a separate calibration code to obtain the calibration value. To optimize this problem, the signal adapter board in this kit is built-in The calibration algorithm can calibrate the weight information with one key, and there is no need to run a separate code for calibration.

Note: The maximum range of the sensor is 1kg, please do not exceed the range, otherwise the sensor will be permanently damaged, which will cause inaccurate data.

Features

Application

Specification

After the address is modified, it will take effect after the sensor is powered off and restarted

A0 A1 Address
0 0 0X64
1 0 0X65
0 1 0X66
1 1 0X67

Board Overview

Board Overview

SN Name Description SN Name Description
1 VCC + 6 E+ Sensor red wire
2 GND - 7 E- Sensor black wire
3 SCL I2C Clock Line 8 S- Sensor white wire
4 SDA I2C Data Line 9 S+ Sensor green wire
5 CAL Button Automatic Calibration 10 RST Button Tare

Operation Instruction

Assembly

Tutorial

Requirements

/*!
   * @brief Constructor 
   * @param pWire I2c controller
   * @param addr  I2C address(0x64/0x65/0x660x67)
   */
  DFRobot_HX711_I2C(TwoWire * pWire = &Wire,uint8_t addr = HX711_I2C_ADDR);

  /**
   * @brief Initialize function 
   * @return Return 1 when initialization succeeds. Otherwise, return error code 
   */
  int begin(void);

  /**
   * @brief Get object weight 
   * @param The number of time to take avereage 
   * @return Return the obtained weight,Unit: g
   */
  float readWeight(uint8_t times = 12);

  /**
   * @brief Get calibration value 
   * @return Return the obtained calibration value 
   */
  float getCalibration();

  /**
   * @brief Set/update calibration value 
   * @param Value 
   */
  void  setCalibration(float value);

  /**
   * @brief Set the sensor auto-calibration trigger threshold(g)
   * @param Threshold data 
   */
  void  setThreshold(uint16_t threshold);

  /**
   * @brief Set the sensor calibration weight for auto-calibration(g)
   * @param Weight data
   */
  void  setCalWeight(uint16_t triWeight);

  /**
   * @brief Enable calibration (by software)
   */
  void enableCal();

  /**
   * @brief Tare (by software)
   */
  void peel();

  /**
   * @brief Wait for the sensor to complete calibration 
   * @return Calibation completed, return: True
   *         Calibration failed, return: False
   */
  bool getCalFlag();

Connection Diagram

Connection

Auto calibration - Button

Calibration can be performed at any time by button while the sensor is working. The calibration steps are as follows:

(After the object is placed, there will be a delay of 1 second for the anti-shake time, so do not worry. After the object is placed, do not touch the object before the calibration is completed)

Download and install the DFRobot HX711 I2C Library.

#include <DFRobot_HX711_I2C.h>

//DFRobot_HX711_I2C MyScale(&Wire,/*addr=*/0x64);
DFRobot_HX711_I2C MyScale;

float Weight = 0;

void setup() {
  Serial.begin(9600);
  while (!MyScale.begin()) {
    Serial.println("The initialization of the chip is failed, please confirm whether the chip connection is correct");
    delay(1000);
  }
  //// Set the calibration weight when the weight sensor module is automatically calibrated (g)
  MyScale.setCalWeight(100);
  // Set the trigger threshold (G) for automatic calibration of the weight sensor module. When only the weight of the object on the scale is greater than this value, the module will start the calibration process
  // This value cannot be greater than the calibration weight of the setCalWeight() setting
  MyScale.setThreshold(30);
  // Obtain the calibration value. The accurate calibration value can be obtained after the calibration operation is completed
  Serial.print("the calibration value of the sensor is: ");
  Serial.println(MyScale.getCalibration());
  MyScale.setCalibration(MyScale.getCalibration());
  delay(1000);
}

void loop() {
  Weight = MyScale.readWeight();
  Serial.print("weight is: ");
  if(Weight > 0.5){
    Serial.print(Weight, 1);
  }
  else{
    Serial.print(0, 1);
  }
  Serial.println(" g");
  delay(1000); 
}

Auto calibration - Software

you can also calibrate the sensor through software by using the enableCal () function. (Button calibration method, you can calibrate at any time when the sensor is working, please see the previous step for the method).

#include <DFRobot_HX711_I2C.h>

//DFRobot_HX711_I2C MyScale(&Wire,/*addr=*/0x64);
DFRobot_HX711_I2C MyScale;

void setup() {
  Serial.begin(9600);
  while (!MyScale.begin()) {
    Serial.println("The initialization of the chip is failed, please confirm whether the chip connection is correct");
    delay(1000);
  }
  //Set the calibration weight when the weight sensor module is automatically calibrated (g)
  MyScale.setCalWeight(100);
  // Set the trigger threshold (G) for automatic calibration of the weight sensor module. When only the weight of the object on the scale is greater than this value, the module will start the calibration process
  // This value cannot be greater than the calibration weight of the setCalWeight() setting
  MyScale.setThreshold(30);  

  //Start sensor calibration
  Serial.println("Please put the object within 5S");
  MyScale.enableCal();
  long time1 = millis();
  //Wait for sensor calibration to complete
  while(!MyScale.getCalFlag()){
       delay(1000);
       if((millis()-time1) > 7000){ 
          Serial.println("The calibration failed this time, and no object is detected within 5S. The last calibration value will be used.");
          break;
       }
  }
  //Obtain the calibration value. The accurate calibration value can be obtained after the calibration operation is completed
  Serial.print("the calibration value of the sensor is: ");
  Serial.println(MyScale.getCalibration());
  MyScale.setCalibration(MyScale.getCalibration());
}

void loop(){
  float Weight = MyScale.readWeight();
  Serial.print("weight is: ");
  if(Weight > 0.5){
    Serial.print(Weight, 1);
  }
  else{
    Serial.print(0, 1);
  }
  Serial.println(" g");
  delay(1000);  
}

Quick Start Example

This method is suitable for quick experience, taking the most common DFRduino UNO R3 + IO sensor expansion board V7.1 as an example for quick demonstration (this method requires at least two main controller board)

It is recommended to use 100g-500g objects for calibration in actual use, the data will be more accurate.

#include <DFRobot_HX711_I2C.h>

//DFRobot_HX711_I2C MyScale(&Wire,/*addr=*/0x64);
DFRobot_HX711_I2C MyScale;

float Weight = 0;

void setup() {
  Serial.begin(9600);
  while (!MyScale.begin()) {
    Serial.println("The initialization of the chip is failed, please confirm whether the chip connection is correct");
    delay(1000);
  }
  //// Set the calibration weight when the weight sensor module is automatically calibrated (g)
  MyScale.setCalWeight(50);
  // Set the trigger threshold (G) for automatic calibration of the weight sensor module. When only the weight of the object on the scale is greater than this value, the module will start the calibration process
  // This value cannot be greater than the calibration weight of the setCalWeight() setting
  MyScale.setThreshold(30);
  // Obtain the calibration value. The accurate calibration value can be obtained after the calibration operation is completed
  Serial.print("the calibration value of the sensor is: ");
  Serial.println(MyScale.getCalibration());
  MyScale.setCalibration(MyScale.getCalibration());
  delay(1000);
}

void loop() {
  Weight = MyScale.readWeight();
  Serial.print("weight is: ");
  if(Weight > 0.5){
    Serial.print(Weight, 1);
  }
  else{
    Serial.print(0, 1);
  }
  Serial.println(" g");
  delay(1000); 
}

Compatibility

MCU Work Well Work Wrong Untested Remarks
Arduino uno
FireBeetle-ESP8266
FireBeetle-ESP32
mPython
Raspberry Pi
micro:bit

FAQ

A: Replace the loop function in the code.

void loop() {
  Serial.print("weight is: ");
  Serial.print(MyScale.readWeight(), 1);
  Serial.println(" g");
  delay(1000);
}
- Q2. How to judge whether the automatic calibration is successful?
A: When the calibration is successful, the indicator above the calibration button will flash three times and go out. Otherwise, the calibration fails.
- Q3. Although the automatic calibration is successful, the output weight information has a small deviation (<0.5g).
A: Sometimes the object is not stably placed, which will cause a small error, you can calibrate it again.
- Q4. After the automatic calibration is successful, the output weight information has a large deviation (>3g), although multiple calibrations have been performed.
A: Please check that the four leads of the sensor (weighing beam) are not entangled too much. Too much winding will affect the accuracy of the data. Make sure that the scale surface is not tilted.
- Q5. What should the weight of the calibration object be?
A: Take the middle value. For example, if the measurement range is about 0-600g, the weight of the calibration object is preferably 300g (other weights are also possible, the middle value is the ideal value).
- Q6: When calibrating a 100g object, why the calibration is not started after the 100g object is placed, and the calibration fails all the time.
A: A "trigger weight" is required for calibration. Press the calibration button and the sensor will only start calibration when it detects that something is put on and exceeds the "trigger weight". When in the first use, because there is no calibration, the data read of the 100g object by sensor may only be 20g. At this time, you need to reduce the value of MyScale.setThreshold(30) (change 30 to 10 or lower), and then upload the code again.
- Q7: No matter whether a object is placed or not, the sensor output data is always 0.
A: This is caused by placing the object on the scale before the Arduino completes the reset. Solution: Take off the object on the scale, press the reset button of Arduino or the rst button of the sensor, and put the object on again, then it can be used normally.
Q8: Why use the setCalibration() function to set the calibration value to 2000, but use the getCalibration() function to check the current calibration value is not 2000.
A: The getCalibration() function can only obtain the calibration value of the "key-press calibration" or enableCal() function, but cannot obtain the calibration value set by the setCalibration() function

More Documents