Example Code for Arduino-Auto Calibration (Software)
Last revision 2026/01/18
This article offers an in-depth guide on using Arduino with the HX711 I2C library for auto calibration of weight sensors. It covers hardware and software preparation, wiring instructions, and provides sample code for achieving precise sensor calibration, ensuring accurate weight measurements.
Hardware Preparation
- DFRduino UNO R3 (or similar) x 1
- IO Expansion Shield for Arduino V7.1 x 1
- Weight Sensor Kit x 1
- Object with known weight (100g recommended)
Software Preparation
- Arduino IDE
- Download and install the DFRobot HX711 I2C Library. (About how to install the library?)
Wiring Diagram

Other Preparation Work
- Assemble the kit and remove the other objects on the sensor except the scale surface
- Connect the sensor correctly according to the wiring diagram, and upload the sample code. After the upload is successful, open the serial monitor and adjust the baud rate to 9600
- Ensure no objects are placed on the sensor before calibration
Sample Code
#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);
}
Result
- After the CAL light on the board turns on or the serial port prints "Please put the object within 5s", put an object weighing 100g on the scale
- After waiting for a few seconds, the serial monitor will output the calibrated weight value, e.g., "weight is: 100.0 g"
- If no object is placed within 7s, the serial monitor will display "The calibration failed this time, and no object is detected within 5S. The last calibration value will be used."
Additional Information
- When opening the serial monitor, the arduino will automatically reset and clear, so you must strictly follow the above steps
- The
enableCal()function is used to trigger software calibration, ensure the sensor is empty before starting calibration
Was this article helpful?
