Remarks
Hx711 | |
---|---|
Status | |
Source code | GitHub |
Datasheet(s) | GitHub |
NuGet package |
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
Characteristic | Locus |
---|---|
Inheritance | System.Object ObservableBase<Mass> SamplingSensorBase<Mass> PollingSensorBase<Mass> > Hx711 |
Implements | ISamplingSensor<Mass> ISamplingSensor<Mass> IMassSensor IDisposable |
Inherited Members | 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 | Hx711.dll |
Syntax
public class Hx711 : PollingSensorBase<Mass>, ISamplingSensor<Mass>, ISamplingSensor<Mass>, IMassSensor, IDisposable
Constructors
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 |
Hx711(IPin, IPin, Nullable<UInt32>)
Creates an instance of the Hx711 Driver class
Declaration
public Hx711(IPin sck, IPin dout, uint? tareValue = null)
Parameters
Type | Name | Description |
---|---|---|
IPin | sck | Serial clock pin |
IPin | dout | Digital output pin |
System.Nullable<System.UInt32> | tareValue | Tare value threshold |
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> |
Overrides
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>> |