All Projects → MichaelS11 → go-hx711

MichaelS11 / go-hx711

Licence: MIT license
Golang HX711 interface using periph.io driver

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to go-hx711

CaravanPi
System for measuring and displaying various values in caravans and motor homes, including climate values, filling levels and levelling data. MagicMirror (https://magicmirror.builders/) is used for presentation. A circuit board design is available now
Stars: ✭ 14 (-6.67%)
Mutual labels:  scale, hx711
hx711
HX711 full function driver for general MCU and Linux.
Stars: ✭ 67 (+346.67%)
Mutual labels:  gpio, hx711
Drivers
TinyGo drivers for sensors and other devices that use I2C, SPI, GPIO, ADC, and UART interfaces.
Stars: ✭ 250 (+1566.67%)
Mutual labels:  gpio
hedgedhttp
Hedged HTTP client which helps to reduce tail latency at scale.
Stars: ✭ 103 (+586.67%)
Mutual labels:  scale
BetterDummy
Unlock your displays on your Mac! Smooth scaling, HiDPI unlock, XDR/HDR extra brightness upscale, DDC, brightness and dimming, dummy displays, PIP and lots more!
Stars: ✭ 9,601 (+63906.67%)
Mutual labels:  scale
scalem
A jQuery plugin to make any element scalable (responsive).
Stars: ✭ 33 (+120%)
Mutual labels:  scale
spicedb
Open Source, Google Zanzibar-inspired fine-grained permissions database
Stars: ✭ 3,358 (+22286.67%)
Mutual labels:  scale
Rpi Rgb Led Matrix
Controlling up to three chains of 64x64, 32x32, 16x32 or similar RGB LED displays using Raspberry Pi GPIO
Stars: ✭ 2,544 (+16860%)
Mutual labels:  gpio
jean-pierre
A Raspberry Pi robot that helps people make their grocery list.
Stars: ✭ 41 (+173.33%)
Mutual labels:  gpio
pirrigo
Full-featured Raspberry Pi based irrigation controller and web application for scheduling.
Stars: ✭ 18 (+20%)
Mutual labels:  gpio
ASV
[CVPR16] Accumulated Stability Voting: A Robust Descriptor from Descriptors of Multiple Scales
Stars: ✭ 26 (+73.33%)
Mutual labels:  scale
pihut-xmas-asyncio
Demonstration driving The Pi Hut Raspberry Pi 3D Xmas tree using Python Asyncio
Stars: ✭ 15 (+0%)
Mutual labels:  gpio
saklarku
Aplikasi mobile remote control untuk mengendalikan saklar/relay yang terhubung dengan port LED/GPIO di router berbasis OpenWRT
Stars: ✭ 20 (+33.33%)
Mutual labels:  gpio
scale
📦 Toolkit for mapping abstract data into visual representation.
Stars: ✭ 53 (+253.33%)
Mutual labels:  scale
go-bsbmp
Golang library to interact with Bosch Sensortec BMP180/BMP280/BME280/BMP388 temperature, pressure and humidity sensors via I2C-bus from Raspberry PI.
Stars: ✭ 41 (+173.33%)
Mutual labels:  gpio
Mqtt Io
Expose GPIO modules (Raspberry Pi, Beaglebone, PCF8754, PiFace2 etc.) and digital sensors (LM75 etc.) to an MQTT server for remote control and monitoring.
Stars: ✭ 234 (+1460%)
Mutual labels:  gpio
awesome-embedded-swift
⚡️🛠🧰 A curated list for Embedded and Low-Level development in the Swift programming language.
Stars: ✭ 57 (+280%)
Mutual labels:  gpio
nitroml
NitroML is a modular, portable, and scalable model-quality benchmarking framework for Machine Learning and Automated Machine Learning (AutoML) pipelines.
Stars: ✭ 40 (+166.67%)
Mutual labels:  scale
Arduino-GPIO
General Purpose Input/Output (GPIO) library for Arduino
Stars: ✭ 43 (+186.67%)
Mutual labels:  gpio
einet
Uncertainty and causal emergence in complex networks
Stars: ✭ 77 (+413.33%)
Mutual labels:  scale

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

Tags

It is possible to use sysfs or /dev/gpiomem GPIO access.

  • sysfs is implemented via Periph.
  • /dev/gpiomem is implemented via 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.

package main

import (
	"fmt"
	"time"

	"github.com/MichaelS11/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/MichaelS11/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/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 main

import (
	"fmt"
	"time"

	"github.com/MichaelS11/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

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 ) 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 ( #1 for some information / metrics ).

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].