Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on Dec 13, 2023. It is now read-only.
/go-hx711Public archive

Golang HX711 interface using periph.io driver

License

NotificationsYou must be signed in to change notification settings

MichaelS11/go-hx711

Repository files navigation

Golang HX711 interface using periph.io driver

GoDoc ReferenceGo 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/MichaelS11/go-hx711

Tags

It is possible to usesysfs or/dev/gpiomem GPIO access.

  • sysfs is implemented viaPeriph.
  • /dev/gpiomem is implemented viago-rpio

sysfs is enabled by default. To use/dev/gpiomem mappings, the taggpiomem 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.

package mainimport ("fmt""time""github.com/MichaelS11/go-hx711")funcmain() {err:=hx711.HostInit()iferr!=nil {fmt.Println("HostInit error:",err)return}hx711,err:=hx711.NewHx711("GPIO6","GPIO5")iferr!=nil {fmt.Println("NewHx711 error:",err)return}deferhx711.Shutdown()err=hx711.Reset()iferr!=nil {fmt.Println("Reset error:",err)return}vardataintfori:=0;i<10000;i++ {time.Sleep(200*time.Microsecond)data,err=hx711.ReadDataRaw()iferr!=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 mainimport ("fmt""github.com/MichaelS11/go-hx711")funcmain() {err:=hx711.HostInit()iferr!=nil {fmt.Println("HostInit error:",err)return}hx711,err:=hx711.NewHx711("GPIO6","GPIO5")iferr!=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)varweight1float64varweight2float64weight1=100weight2=200hx711.GetAdjustValues(weight1,weight2)}

or

go build -v -o getAdjustValues github.com/MichaelS11/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 mainimport ("fmt""time""github.com/MichaelS11/go-hx711")funcmain() {err:=hx711.HostInit()iferr!=nil {fmt.Println("HostInit error:",err)return}hx711,err:=hx711.NewHx711("GPIO6","GPIO5")iferr!=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 abovehx711.AdjustZero=-123hx711.AdjustScale=456vardatafloat64fori:=0;i<10000;i++ {time.Sleep(200*time.Microsecond)data,err=hx711.ReadDataMedian(11)iferr!=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)iferr!=nil {fmt.Println("ReadDataMedianThenMovingAvgs error:",err)}// moving averagefmt.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

varmovingAvgfloat64stop:=falsestopped=make(chanstruct{})gohx711.BackgroundReadMovingAvgs(11,8,&movingAvg,&stop,stopped)// wait for datatime.sleep(time.Second)// moving averagefmt.Println(movingAvg)// when done set stop to true to quit BackgroundReadMovingAvgsstop=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 ( somebenchmarks ) 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 ).

About

Golang HX711 interface using periph.io driver

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp