hx711

package module
v0.0.0-...-195239c Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2019 License: MIT Imports: 7 Imported by: 0

README

Go HX711 interface

Golang HX711 interface using periph.io driver

GoDoc Reference Go Report Card

Please note

Please make sure to setup your HX711 correctly. Do a search on the internet to find guide. Here is an example of a guide:

https://learn.sparkfun.com/tutorials/load-cell-amplifier-hx711-breakout-hookup-guide

The examples below are from using a Raspberry Pi 3 with GPIO 6 for clock and GPIO 5 for data. Your setup may be different, if so, your pin names would need to change in each example.

If you need to read from channel B, make sure to call hx711.SetGain(32)

Side note, in my testing using 3V input had better consistency then using a 5V input.

Get

go get github.com/netixx/go-hx711

Simple test to make sure scale is working

Run the following program to test your scale. Add and remove weight. Make sure there are no errors. Also make sure that the values go up when you add weight and go down when you remove weight. Don't worry about if the values match the weight, just that they go up and down in value at the correct time.

package main

import (
	"fmt"
	"time"

	"github.com/netixx/go-hx711"
)

func main() {
	err := hx711.HostInit()
	if err != nil {
		fmt.Println("HostInit error:", err)
		return
	}

	hx711, err := hx711.NewHx711("GPIO6", "GPIO5")
	if err != nil {
		fmt.Println("NewHx711 error:", err)
		return
	}

	defer hx711.Shutdown()

	err = hx711.Reset()
	if err != nil {
		fmt.Println("Reset error:", err)
		return
	}

	var data int
	for i := 0; i < 10000; i++ {
		time.Sleep(200 * time.Microsecond)

		data, err = hx711.ReadDataRaw()
		if err != nil {
			fmt.Println("ReadDataRaw error:", err)
			continue
		}

		fmt.Println(data)
	}

}

Calibrate the readings / get AdjustZero & AdjustScale values

To get the values needed to calibrate the scale's readings will need at least one weight of known value. Having two weights is preferred. In the below program change weight1 and weight2 to the known weight values. Make sure to set it in the unit of measurement that you prefer (pounds, ounces, kg, g, etc.). To start, make sure there are no weight on the scale. Run the program. When asked, put the first weight on the scale. Then when asked, put the second weight on the scale. It will print out the AdjustZero and AdjustScale values. Those are using in the next example.

Please note that temperature affects the readings. Also if you are planning on reading the weight often, maybe want to do that for about 20 minutes before calibration.

package main

import (
	"fmt"

	"github.com/netixx/go-hx711"
)

func main() {
	err := hx711.HostInit()
	if err != nil {
		fmt.Println("HostInit error:", err)
		return
	}

	hx711, err := hx711.NewHx711("GPIO6", "GPIO5")
	if err != nil {
		fmt.Println("NewHx711 error:", err)
		return
	}

	// SetGain default is 128
	// Gain of 128 or 64 is input channel A, gain of 32 is input channel B
	// hx711.SetGain(128)

	var weight1 float64
	var weight2 float64

	weight1 = 100
	weight2 = 200

	hx711.GetAdjustValues(weight1, weight2)
}

or

go build -v -o getAdjustValues github.com/netixx/go-hx711/getAdjustValues

Simple program to get weight

Take the AdjustZero and AdjustScale values from the above program and plug them into the below program. Run program. Put weight on the scale and check the values. The AdjustZero and AdjustScale may need to be adjusted to your liking.

package main

import (
	"fmt"
	"time"

	"github.com/netixx/go-hx711"
)

func main() {
	err := hx711.HostInit()
	if err != nil {
		fmt.Println("HostInit error:", err)
		return
	}

	hx711, err := hx711.NewHx711("GPIO6", "GPIO5")
	if err != nil {
		fmt.Println("NewHx711 error:", err)
		return
	}

	// SetGain default is 128
	// Gain of 128 or 64 is input channel A, gain of 32 is input channel B
	// hx711.SetGain(128)

	// make sure to use your values from calibration above
	hx711.AdjustZero = -123
	hx711.AdjustScale = 456

	var data float64
	for i := 0; i < 10000; i++ {
		time.Sleep(200 * time.Microsecond)

		data, err = hx711.ReadDataMedian(11)
		if err != nil {
			fmt.Println("ReadDataMedian error:", err)
			continue
		}

		fmt.Println(data)
	}
}

ReadDataMedianThenMovingAvgs

The function ReadDataMedianThenMovingAvgs gets the number of reading you pass in, in the below example, 11 readings. Then it finds the median reading, adjusts that number with AdjustZero and AdjustScale. Then it will do a rolling average of the last readings in the weights slice up to the number of averages passed in, which in the below example is 5 averages.

previousReadings := []float64{}
movingAvg, err := hx711.ReadDataMedianThenMovingAvgs(11, 8, &previousReadings)
if err != nil {
	fmt.Println("ReadDataMedianThenMovingAvgs error:", err)
}

// moving average
fmt.Println(movingAvg)

BackgroundReadMovingAvgs

The function BackgroundReadMovingAvgs is basically the same as ReadDataMedianThenMovingAvgs but runs in the background in a Goroutine. Set stop to true for BackgroundReadMovingAvgs to quit

var movingAvg float64
stop := false
stopped = make(chan struct{})
go hx711.BackgroundReadMovingAvgs(11, 8, &movingAvg, &stop, stopped)

// wait for data
time.sleep(time.Second)

// moving average
fmt.Println(movingAvg)

// when done set stop to true to quit BackgroundReadMovingAvgs
stop = true

// wait for BackgroundReadMovingAvgs to stop
<-stopped

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HostInit

func HostInit() error

HostInit calls periph.io host.Init(). This needs to be done before Hx711 can be used.

Types

type Hx711

type Hx711 struct {

	// AdjustZero should be set to an int that will zero out a raw reading
	AdjustZero int
	// AdjustScale should be set to a float64 that will give output units wanted
	AdjustScale float64
	// contains filtered or unexported fields
}

Hx711 struct to interface with the hx711 chip. Call NewHx711 to create a new one.

func NewHx711

func NewHx711(clockPinName string, dataPinName string) (*Hx711, error)

NewHx711 creates new Hx711. Make sure to set clockPinName and dataPinName to the correct pins. https://cdn.sparkfun.com/datasheets/Sensors/ForceFlex/hx711_english.pdf

func (*Hx711) BackgroundReadMovingAvgs

func (hx711 *Hx711) BackgroundReadMovingAvgs(numReadings, numAvgs int, movingAvg *float64, stop *bool, stopped chan struct{})

BackgroundReadMovingAvgs it meant to be run in the background, run as a Goroutine. Will continue to get readings and update movingAvg until stop is set to true. After it has been stopped, the stopped chan will be closed. Note when scale errors the movingAvg value will not change. Do not call Reset before or Shutdown after. Reset and Shutdown are called for you. Will panic if movingAvg or stop are nil

func (*Hx711) GetAdjustValues

func (hx711 *Hx711) GetAdjustValues(weight1 float64, weight2 float64)

GetAdjustValues will help get you the adjust values to plug in later. Do not call Reset before or Shutdown after. Reset and Shutdown are called for you.

func (*Hx711) ReadDataMedian

func (hx711 *Hx711) ReadDataMedian(numReadings int) (float64, error)

ReadDataMedian will get median of numReadings raw readings, then will adjust number with AdjustZero and AdjustScale. Do not call Reset before or Shutdown after. Reset and Shutdown are called for you.

func (*Hx711) ReadDataMedianRaw

func (hx711 *Hx711) ReadDataMedianRaw(numReadings int) (int, error)

ReadDataMedianRaw will get median of numReadings raw readings. Do not call Reset before or Shutdown after. Reset and Shutdown are called for you.

func (*Hx711) ReadDataMedianThenAvg

func (hx711 *Hx711) ReadDataMedianThenAvg(numReadings, numAvgs int) (float64, error)

ReadDataMedianThenAvg will get median of numReadings raw readings, then do that numAvgs number of time, and average those. then will adjust number with AdjustZero and AdjustScale. Do not call Reset before or Shutdown after. Reset and Shutdown are called for you.

func (*Hx711) ReadDataMedianThenMovingAvgs

func (hx711 *Hx711) ReadDataMedianThenMovingAvgs(numReadings, numAvgs int, previousReadings *[]float64) (float64, error)

ReadDataMedianThenMovingAvgs will get median of numReadings raw readings, then will adjust number with AdjustZero and AdjustScale. Stores data into previousReadings. Then returns moving average. Do not call Reset before or Shutdown after. Reset and Shutdown are called for you. Will panic if previousReadings is nil

func (*Hx711) ReadDataRaw

func (hx711 *Hx711) ReadDataRaw() (int, error)

ReadDataRaw will get one raw reading from chip. Usually will need to call Reset before calling this and Shutdown after.

func (*Hx711) Reset

func (hx711 *Hx711) Reset() error

Reset starts up or resets the chip. The chip needs to be reset if it is not used for just about any amount of time.

func (*Hx711) SetGain

func (hx711 *Hx711) SetGain(gain int) error

SetGain can be set to gain of 128, 64, or 32. Gain of 128 or 64 is input channel A, gain of 32 is input channel B. Default gain is 128. Note change only takes affect after one reading.

func (*Hx711) Shutdown

func (hx711 *Hx711) Shutdown() error

Shutdown puts the chip in powered down mode. The chip should be shutdown if it is not used for just about any amount of time.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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