|
| 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 | +} |
0 commit comments