Remarks

Nau7802
Status Status badge: working
Source code GitHub
Datasheet(s) GitHub
NuGet package NuGet Gallery for Meadow.Foundation.Sensors.LoadCell.Nau7802

Code Example

private Nau7802 loadSensor;

public int CalibrationFactor { get; set; } = 16526649; // TODO: change this based on your scale (using the method provided below)
public Mass CalibrationWeight { get; set; } = new Mass(1600, Mass.UnitType.Grams); // TODO: enter the known-weight you used in calibration

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

    loadSensor = new Nau7802(Device.CreateI2cBus());

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

        // Set the current load to be zero
        loadSensor.SetCalibrationFactor(CalibrationFactor, CalibrationWeight);
        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 async Task GetAndDisplayCalibrationUnits(Nau7802 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...");

    await Task.Delay(5000);
    sensor.Tare();
    Resolver.Log.Info($"Place a known weight on the scale. Next step in 5 seconds...");

    await Task.Delay(500);
    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 ObservableBase<Mass> SamplingSensorBase<Mass> PollingSensorBase<Mass> ByteCommsSensorBase<Mass> > Nau7802
Implements ISamplingSensor<Mass> ISamplingSensor<Mass> IDisposable IMassSensor IDisposable
Inherited Members ByteCommsSensorBase<Mass>.Peripheral ByteCommsSensorBase<Mass>.ReadBuffer ByteCommsSensorBase<Mass>.WriteBuffer ByteCommsSensorBase<Mass>.Init(Int32, Int32) ByteCommsSensorBase<Mass>.Dispose(Boolean) ByteCommsSensorBase<Mass>.Dispose() PollingSensorBase<Mass>.StartUpdating(Nullable<TimeSpan>) PollingSensorBase<Mass>.StopUpdating() SamplingSensorBase<Mass>.samplingLock SamplingSensorBase<Mass>.Updated SamplingSensorBase<Mass>.SamplingTokenSource SamplingSensorBase<Mass>.Conditions SamplingSensorBase<Mass>.IsSampling SamplingSensorBase<Mass>.UpdateInterval SamplingSensorBase<Mass>.RaiseEventsAndNotify(IChangeResult<>) SamplingSensorBase<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 Nau7802.dll

Syntax

public class Nau7802 : ByteCommsSensorBase<Mass>, ISamplingSensor<Mass>, ISamplingSensor<Mass>, IDisposable, IMassSensor, IDisposable

Constructors

Nau7802(II2cBus)

Creates an instance of the NAU7802 Driver class

Declaration
public Nau7802(II2cBus i2cBus)

Parameters

Type Name Description
II2cBus i2cBus

The I2C bus

Properties

DefaultSamplePeriod

Default sample period

Declaration
public TimeSpan DefaultSamplePeriod { get; }

Property Value

Type Description
TimeSpan

Mass

The last read Mass

Declaration
public Mass? Mass { get; }

Property Value

Type Description
System.Nullable<Mass>

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

Declaration
public int CalculateCalibrationFactor()

Returns

Type Description
System.Int32

The calibration factor as an int

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()

Reads data from the sensor

Declaration
protected override Task<Mass> ReadSensor()

Returns

Type Description
Task<Mass>

The latest sensor reading

Overrides

Meadow.Foundation.SamplingSensorBase<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

Tare()

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

Declaration
public void Tare()

Events

MassUpdated

Raised when the mass value changes

Declaration
public event EventHandler<IChangeResult<Mass>> MassUpdated

Event Type

Type Description
EventHandler<IChangeResult<Mass>>