hx711

package module
v0.0.0-...-b48a8f5 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2018 License: BSD-3-Clause Imports: 3 Imported by: 0

README

Reading from an hx711 24-Bit ADC on a Raspberry Pi in Golang

This is based off https://github.com/rajmaniar/hx711 it extends the implementation by adding the ability to tare and calibrate the device. It's non-opinionated about the units that you are converting e.g. grams, pounds etc.

The HX711Attributes struct is introduced to handle keeping track of known device attribute data.

Both the tare and calibration use the median of the numberOfReadings retrieved because there are situations where a reading from the load cell can be a large spike which could throw off an average.

To tare the device you can use Tare(numberOfReadings int). To calibrate the device you can use Calibrate(numberOfReadings int, knownWeight float64).

Example:
import "github.com/rajmaniar/hx711"

func main() {
    
    clock := "gpio23"
    data := "gpio24"
    
    // existing attributes are known
    attributes := &HX711.Attributes{Tare: 1000, CalibratedReading: 2000, CalibratedWeight: 1500}
    
    h,err := hx711.NewWithKnownAttributes(data, clock)
    
    if err != nil {
        fmt.Printf("Error: %v",err)
    }
    
    for err == nil {
        var data int32
    
        // returns the reading based on the calibration attributes
        data, err = h.ReadCalibratedData()
        fmt.Printf("Read from HX711: %v\n",data)
        time.Sleep(250 * time.Millisecond)
    }
    fmt.Printf("Stopped reading because of: %v\n",err)
}

NB
  • h.Reset() will reset the chip
  • h.Gain is set to hx711.GAIN_A_128 by default

Documentation

Index

Constants

View Source
const (
	GAIN_A_128 = 1
	GAIN_B_32  = 2
	GAIN_A_64  = 3
)

Variables

This section is empty.

Functions

func CalculateCalibratedReading

func CalculateCalibratedReading(rawValue int32, attribute *HX711Attributes) (float64, error)

CalculateCalibratedReading calculate the calibrated reading based on the raw value and tare/calibrated values

Types

type HX711

type HX711 struct {
	Clock      string
	Data       string
	Gain       int
	Attributes *HX711Attributes
	// contains filtered or unexported fields
}

func New

func New(data string, clock string) (*HX711, error)

New instantiates a new object

func NewWithKnownAttributes

func NewWithKnownAttributes(data string, clock string, attributes *HX711Attributes) (*HX711, error)

New initializes a new object with the known calibration values

func (*HX711) Calibrate

func (h *HX711) Calibrate(numberOfReadings int, knownWeight float64) error

Calibrate calibrate the device by using a known weight

func (*HX711) OnReady

func (h *HX711) OnReady() error

OnReady Blocks until the chip is ready to send data

func (*HX711) ReadCalibratedData

func (h *HX711) ReadCalibratedData() (float64, error)

ReadCalibratedData get the values from the device and transform them per the known attributes

func (*HX711) ReadData

func (h *HX711) ReadData() (int32, error)

ReadData gets a 24bit signed int from the chip

func (*HX711) Reset

func (h *HX711) Reset() error

Reset / Wakeup the chip

func (*HX711) SetGain

func (h *HX711) SetGain() error

SetGain sets the gain after data has been read

func (*HX711) Sleep

func (h *HX711) Sleep() error

Sleep the chip until you need

func (*HX711) Tare

func (h *HX711) Tare(numberOfReadings int) (float64, error)

Tare get the tare value and set it for the device for the "zero" reading

type HX711Attributes

type HX711Attributes struct {
	// Tare the reading at "zero"
	Tare float64 `json:"tare"`

	// CalibratedReading the raw reading from the device for the CalibratedWeight
	CalibratedReading float64 `json:"calibratedReading"`

	// CalibratedWeight the known weight when calibrating
	CalibratedWeight float64 `json:"calibratedWeight"`
}

Jump to

Keyboard shortcuts

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