// https://wokwi.com/projects/407577936776846337
// https://github.com/bogde/HX711/blob/master/examples/HX711_timeout_example/HX711_timeout_example.ino
/*
Note that the Wokwi HX711 devices gives raw numbers in the 0-2100 or 0-21000 ranges
https://docs.wokwi.com/parts/wokwi-hx711
and https://github.com/wokwi/wokwi-docs/pull/208
*/
/**
HX711 library for Arduino - example file
https://github.com/bogde/HX711
MIT License
(c) 2018 Bogdan Necula
**/
#include "HX711.h"
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 2;
const int LOADCELL_SCK_PIN = 3;
HX711 scale;
void setup() {
Serial.begin(38400);
Serial.println("HX711 Demo");
Serial.println("Initializing the scale");
// Initialize library with data output pin, clock input pin and gain factor.
// Channel selection is made by passing the appropriate gain:
// - With a gain factor of 64 or 128, channel A is selected
// - With a gain factor of 32, channel B is selected
// By omitting the gain factor parameter, the library
// default "128" (Channel A) is used here.
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
calcScale();
Serial.println("Before setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight (not set yet)
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight (not set) divided
// by the SCALE parameter (not set yet)
// scale.set_scale(2280.f); // this value is obtained by calibrating the scale with known weights; see the README for details
scale.set_scale(2100.f / 5000); // this value is obtained by calibrating the scale with known weights; see the README for details
scale.tare(); // reset the scale to 0
Serial.println("After setting up the scale:");
Serial.print("scale: \t");
Serial.println(scale.get_scale());
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight, set with tare()
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight, divided
// by the SCALE parameter set with set_scale
Serial.println("Readings:");
}
void loop() {
Serial.print("one reading:\t");
Serial.print(scale.get_units(), 1);
Serial.print("\t| average:\t");
Serial.println(scale.get_units(10), 1);
scale.power_down(); // put the ADC in sleep mode
delay(5000);
scale.power_up();
}
void calcScale() {
// report on the expected calibration
const float SensorFullScale = 5.000; // kg at full scale
const float Amps[] = {32, 64, 128};
const float MaxVo[] = {0.080, 0.040, 0.020}; // +/-Voltage at INx+/INx-
const int AmpChoice = 2;
// the amplifications and limits make for about a +/-2.56V range
const float VCC = 5.0; // supply volts
const float ExcitationVoltage = VCC - 0.2 ; // Ve HX711 board transistor drop
//const float SensorOutput = 0.001; // Vo/Ve rated output at FS
const float SensorOutput = 0.001; // Vo/Ve rated output at FS
// Fudge factor
// per https://github.com/wokwi/wokwi-features/issues/872
const float WokwiAttenuation = 0.001956; // to reach 2100
const float Amplification = Amps[AmpChoice] * WokwiAttenuation; // 32, 64, or 128 from HX711
const float Vo_FS = ExcitationVoltage * SensorOutput; //
const float Vo_FSa = Vo_FS * Amplification; // ADC input voltage
const float OutputScaleV = ExcitationVoltage * SensorOutput / SensorFullScale * Amplification; // V/OutUnit
const long MaxADC = 1L << 23 - 1; // + side of 24 bit ADC
const long MinADC = -(1L << 23); // - side of 24 bit ADC
const float ADCCountPerV = MaxADC / (ExcitationVoltage/2);
const float ADCScale = ADCCountPerV * OutputScaleV;
Serial.print("###################\nCalculation of scale:\n");
Serial.print("Ve:");
Serial.print(ExcitationVoltage);
Serial.print("V, Vo_FS:");
Serial.print(Vo_FS, 6);
Serial.print("V @");
Serial.print(SensorFullScale);
Serial.print("OutputUnits, versus");
Serial.print(MaxVo[AmpChoice], 6);
Serial.print("Vo maximum at Amplification x");
Serial.print(Amps[AmpChoice]);
Serial.print("Effective \namped(x");
Serial.print(Amplification);
Serial.print("):");
Serial.print(Vo_FSa, 6);
Serial.print("Vo_(amped) into ADC, \nscale:");
Serial.print(OutputScaleV, 6);
Serial.print("V/outUnit into ADC, \nscale:");
Serial.print(ADCScale);
Serial.print(" AdcCounts/outputUnit");
Serial.println("\n#####################");
}