Skip to content

Commit 6098b2e

Browse files
committed
add hx711 driver
1 parent 228e57c commit 6098b2e

File tree

6 files changed

+1139
-2
lines changed

6 files changed

+1139
-2
lines changed

examples/hx711/main.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package main
2+
3+
import (
4+
"machine"
5+
"time"
6+
7+
"tinygo.org/x/drivers/hx711"
8+
)
9+
10+
const (
11+
clockOutPin = machine.D3
12+
dataInPin = machine.D2
13+
gainAndChannel = hx711.A128 // only the first channel A is used
14+
tickSleep = 1 * time.Microsecond // set it to zero for slow MCU's
15+
calibrationWait = 10 * time.Second
16+
cycleTime = 1 * time.Second
17+
)
18+
19+
// please adjust to your load used for calibration
20+
const (
21+
setLoad = 100 // used unit will equal the measured unit
22+
unit = "gram"
23+
)
24+
25+
func main() {
26+
time.Sleep(5 * time.Second) // wait for monitor connection
27+
28+
cfg := hx711.DefaultConfig
29+
cfg.TickSleep = tickSleep
30+
31+
sensor := hx711.New(outputPin{pin: clockOutPin}, inputPin{pin: dataInPin}, gainAndChannel)
32+
if err := sensor.Configure(&cfg); err != nil {
33+
println("Configure failed")
34+
panic(err)
35+
}
36+
37+
println("Please remove the mass completely for zeroing within", calibrationWait.String())
38+
time.Sleep(calibrationWait)
39+
println("Zero starts")
40+
if err := sensor.Zero(false); err != nil {
41+
println("Zeroing failed")
42+
panic(err)
43+
}
44+
45+
println("Please apply the load (", setLoad, unit+" ) for calibration within", calibrationWait.String())
46+
time.Sleep(calibrationWait)
47+
println("Calibration starts")
48+
if err := sensor.Calibrate(setLoad, false); err != nil {
49+
println("Calibration failed")
50+
panic(err)
51+
}
52+
53+
offs, factor := sensor.OffsetAndCalibrationFactor(false)
54+
println("Calibration done completely, offset:", offs, "factor:", factor)
55+
56+
println("Measurement starts")
57+
for {
58+
if err := sensor.Update(0); err != nil {
59+
println("Sensor update failed", err.Error())
60+
}
61+
62+
v1, _ := sensor.Values()
63+
64+
println("Mass:", v1, unit)
65+
66+
time.Sleep(cycleTime)
67+
}
68+
}
69+
70+
type outputPin struct {
71+
pin machine.Pin
72+
}
73+
74+
type inputPin struct {
75+
pin machine.Pin
76+
}
77+
78+
func (outp outputPin) Set(v bool) error {
79+
outp.pin.Set(v)
80+
return nil
81+
}
82+
83+
func (outp outputPin) Configure() error {
84+
outp.pin.Configure(machine.PinConfig{Mode: machine.PinOutput})
85+
return nil
86+
}
87+
88+
func (inp inputPin) Get() (bool, error) {
89+
return inp.pin.Get(), nil
90+
}
91+
92+
func (inp inputPin) Configure() error {
93+
inp.pin.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
94+
return nil
95+
}

go.mod

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
module tinygo.org/x/drivers
22

3-
43
go 1.22.1
54

65
toolchain go1.23.1
76

8-
97
require (
108
github.com/eclipse/paho.mqtt.golang v1.2.0
119
github.com/frankban/quicktest v1.10.2
@@ -19,7 +17,11 @@ require (
1917
)
2018

2119
require (
20+
github.com/davecgh/go-spew v1.1.1 // indirect
2221
github.com/google/go-cmp v0.6.0 // indirect
2322
github.com/kr/pretty v0.2.1 // indirect
2423
github.com/kr/text v0.1.0 // indirect
24+
github.com/pmezard/go-difflib v1.0.0 // indirect
25+
github.com/stretchr/testify v1.11.1 // indirect
26+
gopkg.in/yaml.v3 v3.0.1 // indirect
2527
)

go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
github.com/bgould/http v0.0.0-20190627042742-d268792bdee7/go.mod h1:BTqvVegvwifopl4KTEDth6Zezs9eR+lCWhvGKvkxJHE=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
24
github.com/eclipse/paho.mqtt.golang v1.2.0 h1:1F8mhG9+aO5/xpdtFkW4SxOJB67ukuDC3t2y2qayIX0=
35
github.com/eclipse/paho.mqtt.golang v1.2.0/go.mod h1:H9keYFcgq3Qr5OUJm/JZI/i6U7joQ8SYLhZwfeOo6Ts=
46
github.com/frankban/quicktest v1.10.2 h1:19ARM85nVi4xH7xPXuc5eM/udya5ieh7b/Sv+d844Tk=
@@ -15,14 +17,21 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
1517
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
1618
github.com/orsinium-labs/tinymath v1.1.0 h1:KomdsyLHB7vE3f1nRAJF2dyf1m/gnM2HxfTeV1vS5UA=
1719
github.com/orsinium-labs/tinymath v1.1.0/go.mod h1:WPXX6ei3KSXG7JfA03a+ekCYaY9SWN4I+JRl2p6ck+A=
20+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
21+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1822
github.com/soypat/natiu-mqtt v0.5.1 h1:rwaDmlvjzD2+3MCOjMZc4QEkDkNwDzbct2TJbpz+TPc=
1923
github.com/soypat/natiu-mqtt v0.5.1/go.mod h1:xEta+cwop9izVCW7xOx2W+ct9PRMqr0gNVkvBPnQTc4=
24+
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
25+
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
2026
github.com/valyala/fastjson v1.6.3/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
2127
golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d h1:0olWaB5pg3+oychR51GUVCEsGkeCU/2JxjBgIo4f3M0=
2228
golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d/go.mod h1:qj5a5QZpwLU2NLQudwIN5koi3beDhSAlJwa67PuM98c=
2329
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
2430
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
2531
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
32+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
33+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
34+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2635
tinygo.org/x/drivers v0.14.0/go.mod h1:uT2svMq3EpBZpKkGO+NQHjxjGf1f42ra4OnMMwQL2aI=
2736
tinygo.org/x/drivers v0.15.1/go.mod h1:uT2svMq3EpBZpKkGO+NQHjxjGf1f42ra4OnMMwQL2aI=
2837
tinygo.org/x/tinyfont v0.2.1/go.mod h1:eLqnYSrFRjt5STxWaMeOWJTzrKhXqpWw7nU3bPfKOAM=

gpio.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package drivers
2+
3+
// OutputPinner represents a pin used for digital output.
4+
type OutputPinner interface {
5+
//Configure sets the pin as output.
6+
Configure() error
7+
8+
// Set sets the output to the given state.
9+
Set(state bool) error
10+
}
11+
12+
// InputPinner represents a pin used for digital input.
13+
type InputPinner interface {
14+
// Configure sets the pin as input. Additionally mode can be set, such as an internal pull up or pull down resistor.
15+
Configure() error
16+
17+
// Get gets the state of the input.
18+
Get() (bool, error)
19+
}

0 commit comments

Comments
 (0)