Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/simonwaldherr/hx711go
Golang package to interface hx711 load cell amplifier
https://github.com/simonwaldherr/hx711go
go golang hx711 load-cell raspberry raspberry-pi raspberrypi scale wiegesensor
Last synced: about 5 hours ago
JSON representation
Golang package to interface hx711 load cell amplifier
- Host: GitHub
- URL: https://github.com/simonwaldherr/hx711go
- Owner: SimonWaldherr
- License: mit
- Created: 2022-09-16T17:58:16.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2022-09-20T17:59:52.000Z (about 2 years ago)
- Last Synced: 2024-10-12T16:43:15.980Z (about 1 month ago)
- Topics: go, golang, hx711, load-cell, raspberry, raspberry-pi, raspberrypi, scale, wiegesensor
- Language: Go
- Homepage:
- Size: 51.8 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go HX711 interface
Golang HX711 interface using periph.io driver
[![GoDoc Reference](https://godoc.org/github.com/SimonWaldherr/hx711go?status.svg)](http://godoc.org/github.com/SimonWaldherr/hx711go)
[![Go Report Card](https://goreportcard.com/badge/github.com/SimonWaldherr/hx711go)](https://goreportcard.com/report/github.com/SimonWaldherr/hx711go)## Get an HX711
you can order a nice hx711 with a load cell directly from amazon: https://amzn.to/3Lls0u6
or https://www.bastelgarage.ch/load-cell-amplifier-hx711-wiegesensor-24-bit ,
https://www.exp-tech.de/sensoren/waegezelle/7507/sparkfun-load-cell-amplifier-hx711 ,
https://www.sparkfun.com/products/13879## 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 4 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/SimonWaldherr/hx711go`
## Tags
It is possible to use `sysfs` or `/dev/gpiomem` GPIO access.
* `sysfs` is implemented via [Periph](https://periph.io).
* `/dev/gpiomem` is implemented via [go-rpio](https://github.com/stianeikeland/go-rpio)`sysfs` is enabled by default. To use `/dev/gpiomem` mappings, the tag `gpiomem` needs to be provided.
```
go build -tags=gpiomem
```## 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.
```go
package mainimport (
"fmt"
"time""github.com/SimonWaldherr/hx711go"
)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.
```go
package mainimport (
"fmt""github.com/SimonWaldherr/hx711go"
)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 float64weight1 = 100
weight2 = 200hx711.GetAdjustValues(weight1, weight2)
}
```or
```
go build -v -o getAdjustValues github.com/SimonWaldherr/hx711go/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.
```go
package mainimport (
"fmt"
"time""github.com/SimonWaldherr/hx711go"
)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 = 456var 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.
```go
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
```go
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
```## Performance considerations
`sysfs` is more standard way across multiple platforms, yet is has some performance bottlenecks.
`/dev/gpiomem` demonstrates better performance for IO operations ( some [benchmarks](https://github.com/warthog618/gpio#benchmarks) ) but is specific to Raspberry PI / Broadcom chip.While Periph-related bindings work fine on Raspberry Pi 3 ( and probably Raspberry Pi 2 ) - using HX711 chip with Raspberry Pi Zero / Raspberry Pi Zero W is challenging, because the timings are off ( https://github.com/MichaelS11/go-hx711/issues/1 for some information / metrics ).