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()
{
    Console.WriteLine("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) => Console.WriteLine($"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
    Console.WriteLine($"Beginning Calibration. First we'll tare (set a zero).");
    Console.WriteLine($"Make sure scale bed is clear. Next step in 5 seconds...");

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

    await Task.Delay(500);
    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<Units.Mass> SensorBase<Units.Mass> SamplingSensorBase<Units.Mass> ByteCommsSensorBase<Units.Mass> > Nau7802
Implements IDisposable IMassSensor IDisposable
Inherited Members ByteCommsSensorBase<Units.Mass>.Peripheral ByteCommsSensorBase<Units.Mass>.ReadBuffer ByteCommsSensorBase<Units.Mass>.WriteBuffer ByteCommsSensorBase<Units.Mass>.Init(Int32, Int32) ByteCommsSensorBase<Units.Mass>.Dispose(Boolean) ByteCommsSensorBase<Units.Mass>.Dispose() SamplingSensorBase<Units.Mass>.StartUpdating(Nullable<TimeSpan>) SamplingSensorBase<Units.Mass>.StopUpdating() SensorBase<Units.Mass>.Updated SensorBase<Units.Mass>.samplingLock SensorBase<Units.Mass>.SamplingTokenSource SensorBase<Units.Mass>.Conditions SensorBase<Units.Mass>.IsSampling SensorBase<Units.Mass>.UpdateInterval SensorBase<Units.Mass>.RaiseEventsAndNotify(IChangeResult<>) SensorBase<Units.Mass>.Read() ObservableBase<Units.Mass>.observers ObservableBase<Units.Mass>.NotifyObservers(IChangeResult<>) Meadow.Foundation.ObservableBase<Units.Mass>.Subscribe(IObserver<>) Meadow.Foundation.ObservableBase<Units.Mass>.CreateObserver(Action<>, System.Nullable<Predicate<IChangeResult<UNIT>>>)
Namespace Meadow.Foundation.Sensors.LoadCell
Assembly Nau7802.dll

Syntax

public class Nau7802 : ByteCommsSensorBase<Units.Mass>, IDisposable, IMassSensor, IDisposable

Constructors

Nau7802(II2cBus, Int32)

Creates an instance of the NAU7802 Driver class

Declaration
public Nau7802(II2cBus bus, int updateIntervalMs = 1000)

Parameters

Type Name Description
II2cBus bus
System.Int32 updateIntervalMs

Properties

DefaultSamplePeriod

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

Declaration
public int CalculateCalibrationFactor()

Returns

Type Description
System.Int32

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

Declaration
protected override Task<Mass> ReadSensor()

Returns

Type Description
Task<Mass>

Overrides

Meadow.Foundation.SensorBase<Units.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

Declaration
public event EventHandler<IChangeResult<Mass>> MassUpdated

Event Type

Type Description
EventHandler<IChangeResult<Mass>>