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()
{
    Resolver.Log.Info("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) => 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

Characteristic Locus
Inheritance System.Object > Hx711
Implements IMassSensor IDisposable
Namespace Meadow.Foundation.Sensors.LoadCell
Assembly Hx711.dll

Syntax

public class Hx711 : PollingSensorBase<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

Digital Input Output Controller to create digital input and output ports

IPin sck

Serial clock pin

IPin dout

Digital output pin

System.Nullable<System.UInt32> tareValue

Tare value threshold

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

Gets default sample period (1 Second)

Declaration
public TimeSpan DefaultSamplePeriod { get; }

Property Value

Type Description
TimeSpan

Gain

Declaration
public Hx711.AdcGain Gain { get; }

Property Value

Type Description
Hx711.AdcGain

IsDisposed

Is the peripheral disposed

Declaration
public bool IsDisposed { get; }

Property Value

Type Description
System.Boolean

IsSleeping

Is the peripheral sleeping

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

Gets/Sets Tare value

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 SetCalibrationFactor(Int32, Mass) before using the sensor.

Declaration
public int CalculateCalibrationFactor()

Returns

Type Description
System.Int32

Dispose()

Dispose managed resources

Declaration
public void Dispose()

Dispose(Boolean)

Dispose managed resources

Declaration
protected virtual void Dispose(bool disposing)

Parameters

Type Name Description
System.Boolean disposing

Is 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>

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

Mass changed event

Declaration
public event EventHandler<IChangeResult<Mass>> MassUpdated

Event Type

Type Description
EventHandler<IChangeResult<Mass>>