Remarks

Hx711
Status Status badge: working
Source code GitHub
Datasheet(s) GitHub
NuGet package NuGet 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()
{
    Console.WriteLine("Initialize...");

    loadSensor = new Hx711(Device, 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) => Console.WriteLine($"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
    Console.WriteLine($"Beginning Calibration. First we'll tare (set a zero).");
    Console.WriteLine($"Make sure scale bed is clear. Next step in 5 seconds...");
    Thread.Sleep(5000);
    sensor.Tare();
    Console.WriteLine($"Place a known weight on the scale. Next step in 5 seconds...");
    Thread.Sleep(5000);
    var factor = sensor.CalculateCalibrationFactor();
    Console.WriteLine($"Your scale's Calibration Factor is: {factor}.  Enter this into the code for future use.");
}

Sample project(s) available on GitHub

Characteristic Locus
Inheritance System.Object ObservableBase<Mass> SensorBase<Mass> SamplingSensorBase<Mass> > Hx711
Implements IMassSensor IDisposable
Inherited Members SamplingSensorBase<Mass>.StartUpdating(Nullable<TimeSpan>) SamplingSensorBase<Mass>.StopUpdating() SensorBase<Mass>.Updated SensorBase<Mass>.samplingLock SensorBase<Mass>.SamplingTokenSource SensorBase<Mass>.Conditions SensorBase<Mass>.IsSampling SensorBase<Mass>.UpdateInterval SensorBase<Mass>.RaiseEventsAndNotify(IChangeResult<>) SensorBase<Mass>.Read() ObservableBase<Mass>.observers ObservableBase<Mass>.NotifyObservers(IChangeResult<>) Meadow.Foundation.ObservableBase<Mass>.Subscribe(IObserver<>) Meadow.Foundation.ObservableBase<Mass>.CreateObserver(Action<>, System.Nullable<Predicate<IChangeResult<UNIT>>>)
Namespace Meadow.Foundation.Sensors.LoadCell
Assembly Hx711.dll

Syntax

public class Hx711 : SamplingSensorBase<Mass>, IMassSensor, IDisposable

Constructors

Hx711(IDigitalInputOutputController, IPin, IPin, Nullable<UInt32>)

Creates an instance of the Hx711 Driver class

Declaration
public Hx711(IDigitalInputOutputController device, IPin sck, IPin dout, uint? tareValue = null)

Parameters

Type Name Description
IDigitalInputOutputController device
IPin sck
IPin dout
System.Nullable<System.UInt32> tareValue

Hx711(IDigitalOutputPort, IDigitalInputPort, Nullable<UInt32>)

Creates an instance of the Hx711 Driver class

Declaration
public Hx711(IDigitalOutputPort sck, IDigitalInputPort dout, uint? tareValue = null)

Parameters

Type Name Description
IDigitalOutputPort sck
IDigitalInputPort dout
System.Nullable<System.UInt32> tareValue

Properties

DefaultSamplePeriod

Declaration
public TimeSpan DefaultSamplePeriod { get; }

Property Value

Type Description
TimeSpan

Gain

Declaration
public Hx711.AdcGain Gain { get; }

Property Value

Type Description
Hx711.AdcGain

IsDisposed

Declaration
public bool IsDisposed { get; }

Property Value

Type Description
System.Boolean

IsSleeping

Declaration
public bool IsSleeping { get; }

Property Value

Type Description
System.Boolean

Mass

The last read Mass.

Declaration
public Mass? Mass { get; }

Property Value

Type Description
System.Nullable<Mass>

TareValue

Declaration
public uint TareValue { get; set; }

Property Value

Type Description
System.UInt32

Methods

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 before using the sensor.

Declaration
public int CalculateCalibrationFactor()

Returns

Type Description
System.Int32

Dispose()

Dispose managed resources

Declaration
public void Dispose()

Dispose(Boolean)

Declaration
protected virtual void Dispose(bool disposing)

Parameters

Type Name Description
System.Boolean disposing

RaiseEventsAndNotify(IChangeResult<Mass>)

Inheritance-safe way to raise events and notify observers.

Declaration
protected override void RaiseEventsAndNotify(IChangeResult<Mass> changeResult)

Parameters

Type Name Description
IChangeResult<Mass> changeResult

ReadSensor()

Gets the current sensor weight

Declaration
protected override Task<Mass> ReadSensor()

Returns

Type Description
Task<Mass>

Overrides

Meadow.Foundation.SensorBase<Mass>.ReadSensor()

SetCalibrationFactor(Int32, Mass)

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

Declaration
public void SetCalibrationFactor(int factor, Mass knownValue)

Parameters

Type Name Description
System.Int32 factor
Mass knownValue

Sleep()

Puts the device into low-power sleep mode

Declaration
public void Sleep()

Tare()

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

Declaration
public void Tare()

Wake()

Takes the device out of low-power sleep mode

Declaration
public void Wake()

Events

MassUpdated

Declaration
public event EventHandler<IChangeResult<Mass>> MassUpdated

Event Type

Type Description
EventHandler<IChangeResult<Mass>>