hx711

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 15, 2023 License: MIT Imports: 3 Imported by: 0

README

hx711 LoadCell helpers for tinygo

This is a set of helpers to use a Load cell through a hx711 using tinygo, even though according to most device docs and C codes available indicate this should work in any version of these devices, I only tried it in one.

Special thanks to:

Usage

package main

import (
	"tinygo.perri.to/hx711"
	"machine"
)

// Instantiate a device, this should be thread safe but don't push it
// 100 is a good smoothing factor, it is the number of reads that will be made in raw per Read call
// and averaged.
dev := hx711.New(machine.D4, machine.D5, hx711.Gain128, 100, 400) // in millis, is a good time to wait for the device

// the device is ready to use but i recommend calibrating:
// Once the device has been instantiated (that is a blocking call)
// Put a known weight and make a call to
dev.Calibrate(100.10) // weight in grams

// if you do this with multiple weights multiple times it should be more accurate.

// Finally get a read
weight := dev.Read()
fmt.Printf("whatever is on the scale is %d milligrams", weight)

The results are in centigrams, I assume this is true for all models, I only own one hx711 with a 1kg load cell.

Documentation

Index

Constants

View Source
const (
	Gain128 gainLVL = 1 // channel A, gain factor 128
	Gain64  gainLVL = 2 // channel A, gain factor 64
	Gain32  gainLVL = 3 //  channel B, gain factor 32
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DT

type DT interface {
	Get() bool
}

DT represents a pin set as in, this is satisfied by a machine.D# pin definition in tinyGo before using you should invoke machine.D#.Configure(machine.PinConfig{Mode: machine.PinInputPullup}) CPP code indicates this is not safe in some Espressif boards and you should use machine.PinInputPulldown instead.

type Device

type Device struct {
	// contains filtered or unexported fields
}

Device represents a hx711 with a load cell hooked. I recommend that you power off between flashes if you use this device as reset causes weird states.

func New

func New(sck SCK, dt DT, gain gainLVL, smoothingFactor int, settlingWait int) *Device

New returns a device configured and initialized with the passed ports if the device is not appropriately connected this might hang

func (*Device) Calibrate

func (d *Device) Calibrate(weightInGrams float64) (float64, error)

Calibrate takes the known correct weight of the current load and calculates a factor to correct for drift. It is recommended that you save this value once and set it on each ue of a new Device instance for a given hardware to avoid having to perform the calibration again. Performing this process with various weights improves accuracy.... supposedly, depends on the quality of the cell.

func (*Device) GetCalibrationFactor

func (d *Device) GetCalibrationFactor() float64

GetCalibrationFactor returns the factor by which results are multiplied to fine tune weight.

func (*Device) Read

func (d *Device) Read() int64

Read performs avg of <SmoothingFactor> reads and returns that, adjusted for offset and tare.

func (*Device) ReadCalibrated added in v0.1.2

func (d *Device) ReadCalibrated() int64

ReadCalibrated performs avg of <SmoothingFactor> reads and returns that, adjusted for offset, tare and calibration. accuracy lost is intentional

func (*Device) SetCalibrationFactor

func (d *Device) SetCalibrationFactor(factor float64)

SetCalibrationFactor sets a number by which reads will be multiplied to obtain a more accurate weight

func (*Device) SetGainAndChannel

func (d *Device) SetGainAndChannel(g gainLVL)

func (*Device) Tare

func (d *Device) Tare()

Tare performs ... well.. tare? https://en.wikipedia.org/wiki/Tare_weight

func (*Device) Zero

func (d *Device) Zero()

Zero re-sets offset and tare for the load cell.

type SCK

type SCK interface {
	High()
	Low()
}

SCK represents a pin set as out, this is satisfied by a machine.D# pin definition in tinyGo before using you should invoke machine.D#.Configure(machine.PinConfig{Mode: machine.PinOutput})

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL