Skip to main content

Meadow.Foundation.Sensors.LoadCell.Hx711

Hx711
StatusStatus badge: working
Source codeGitHub
Datasheet(s)GitHub
NuGet packageNuGet Gallery for Meadow.Foundation.Sensors.LoadCell.Hx711

Code Example

Hx711 loadSensor;

public int CalibrationFactor { get; set; } = 0; //9834945 - 8458935; // TODO: change this based on your scale (using the method provided below)
public double CalibrationWeight { get; set; } = 1.6; // TODO: enter the known-weight (in units below) you used in calibration

public override async Task Initialize()
{
Resolver.Log.Info("Initialize...");

loadSensor = new Hx711(Device.Pins.D04, Device.Pins.D03);

if (CalibrationFactor == 0)
{
GetAndDisplayCalibrationUnits(loadSensor);
}
else
{ // wait for the ADC to settle
await Task.Delay(500);

// Set the current load to be zero
loadSensor.SetCalibrationFactor(CalibrationFactor, new Mass(CalibrationWeight, Mass.UnitType.Grams));
loadSensor.Tare();
}

loadSensor.MassUpdated += (sender, values) => Resolver.Log.Info($"Mass is now returned {values.New.Grams:N2}g");
}

public override Task Run()
{
loadSensor.StartUpdating(TimeSpan.FromSeconds(2));

return Task.CompletedTask;
}

public void GetAndDisplayCalibrationUnits(Hx711 sensor)
{ // first notify the user we're starting
Resolver.Log.Info($"Beginning Calibration. First we'll tare (set a zero).");
Resolver.Log.Info($"Make sure scale bed is clear. Next step in 5 seconds...");
Thread.Sleep(5000);
sensor.Tare();
Resolver.Log.Info($"Place a known weight on the scale. Next step in 5 seconds...");
Thread.Sleep(5000);
var factor = sensor.CalculateCalibrationFactor();
Resolver.Log.Info($"Your scale's Calibration Factor is: {factor}. Enter this into the code for future use.");
}

Sample project(s) available on GitHub

Class Hx711

24-Bit Dual-Channel ADC For Bridge Sensors

Assembly: Hx711.dll
View Source
Declaration
public class Hx711 : PollingSensorBase<Mass>, IObservable<IChangeResult<Mass>>, IMassSensor, ISamplingSensor<Mass>, ISensor<Mass>, ISensor, ISamplingSensor, IDisposable

Inheritance: System.Object -> Meadow.Foundation.ObservableBase<UNIT>

Implements:
System.IObservable<Meadow.IChangeResult<Meadow.Units.Mass>>, Meadow.Peripherals.Sensors.Mass.IMassSensor, Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.Mass>, Meadow.Peripherals.Sensors.ISensor<Meadow.Units.Mass>, Meadow.Peripherals.Sensors.ISensor, Meadow.Peripherals.Sensors.ISamplingSensor, System.IDisposable

Properties

IsDisposed

Is the peripheral disposed

View Source
Declaration
public bool IsDisposed { get; }

IsSleeping

Is the peripheral sleeping

View Source
Declaration
public bool IsSleeping { get; }

Gain

View Source
Declaration
public Hx711.AdcGain Gain { get; }

TareValue

Gets/Sets Tare value

View Source
Declaration
public uint TareValue { get; set; }

DefaultSamplePeriod

Gets default sample period (1 Second)

View Source
Declaration
public TimeSpan DefaultSamplePeriod { get; }

Mass

The last read Mass

View Source
Declaration
public Mass? Mass { get; }

Methods

Sleep()

Puts the device into low-power sleep mode

View Source
Declaration
public void Sleep()

Wake()

Takes the device out of low-power sleep mode

View Source
Declaration
public void Wake()

Tare()

Tares the sensor, effectively setting the current weight reading to relative zero.

View Source
Declaration
public void Tare()

CalculateCalibrationFactor()

Calculates the calibration factor of the load cell. Call this method with a known weight on the sensor, and then use the returned value in a call to Meadow.Foundation.Sensors.LoadCell.Hx711.SetCalibrationFactor(System.Int32%2cMeadow.Units.Mass) before using the sensor.

View Source
Declaration
public int CalculateCalibrationFactor()
Returns

System.Int32

SetCalibrationFactor(int, Mass)

Sets the sensor's calibration factor based on a factor calculated with a know weight by calling Meadow.Foundation.Sensors.LoadCell.Hx711.CalculateCalibrationFactor().

View Source
Declaration
public void SetCalibrationFactor(int factor, Mass knownValue)
Parameters
TypeName
System.Int32factor
Meadow.Units.MassknownValue

ReadSensor()

Gets the current sensor weight

View Source
Declaration
protected override Task<Mass> ReadSensor()
Returns

System.Threading.Tasks.Task<Meadow.Units.Mass>

Dispose(bool)

Dispose of the object

View Source
Declaration
protected virtual void Dispose(bool disposing)
Parameters
TypeNameDescription
System.BooleandisposingIs disposing

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

View Source
Declaration
public void Dispose()

Implements

  • System.IObservable<Meadow.IChangeResult<Meadow.Units.Mass>>
  • Meadow.Peripherals.Sensors.Mass.IMassSensor
  • Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.Mass>
  • Meadow.Peripherals.Sensors.ISensor<Meadow.Units.Mass>
  • Meadow.Peripherals.Sensors.ISensor
  • Meadow.Peripherals.Sensors.ISamplingSensor
  • System.IDisposable