/*
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp32-load-cell-hx711/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
// Calibrating the load cell
//#include <Arduino.h>
//#include "soc/rtc.h"
#include "HX711.h"
HX711 myScale;
uint8_t dataPin = 2;
uint8_t clockPin = 4;
uint32_t start, stop;
volatile float f;
void setup()
{
Serial.begin(115200);
Serial.println(_FILE_);
Serial.print("LIBRARY VERSION: ");
Serial.println(HX711_LIB_VERSION);
Serial.println();
myScale.begin(dataPin, clockPin);
calibrate();
}
void calibrate()
{
Serial.println("\n\nCALIBRATION\n===========");
Serial.println("remove all weight from the loadcell");
// flush Serial input
while (Serial.available()) Serial.read();
Serial.println("and press enter\n");
while (Serial.available() == 0);
Serial.println("Determine zero weight offset");
myScale.tare(20); // average 20 measurements.
uint32_t offset = myScale.get_offset();
Serial.print("OFFSET: ");
Serial.println(offset);
Serial.println();
Serial.println("place a weight on the loadcell");
// flush Serial input
while (Serial.available()) Serial.read();
Serial.println("enter the weight in (whole) grams and press enter");
uint32_t weight = 0;
while (Serial.peek() != '\n')
{
if (Serial.available())
{
char ch = Serial.read();
if (isdigit(ch))
{
weight *= 10;
weight = weight + (ch - '0');
}
}
}
Serial.print("WEIGHT: ");
Serial.println(weight);
myScale.calibrate_scale(weight, 20);
float scale = myScale.get_scale();
Serial.print("SCALE: ");
Serial.println(scale, 6);
Serial.print("\nuse scale.set_offset(");
Serial.print(offset);
Serial.print("); and scale.set_scale(");
Serial.print(scale, 6);
Serial.print(");\n");
Serial.println("in the setup of your project");
Serial.println("\n\n");
myScale.set_offset(offset);
myScale.set_scale(scale);
}
void loop()
{
if (myScale.is_ready())
{
Serial.println(myScale.get_units(1));
}
}