mirror of
https://github.com/kidoman/embd
synced 2025-07-04 20:37:46 +02:00
Add basic support for INA219 current sensor
This commit is contained in:
parent
bfcd1345fe
commit
69655b6c03
4 changed files with 182 additions and 0 deletions
49
samples/ina219.go
Normal file
49
samples/ina219.go
Normal file
|
@ -0,0 +1,49 @@
|
|||
// +build ignore
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
"github.com/kidoman/embd/sensor/ina219"
|
||||
|
||||
_ "github.com/kidoman/embd/host/all"
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
if err := embd.InitI2C(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer embd.CloseI2C()
|
||||
|
||||
bus := embd.NewI2CBus(1)
|
||||
|
||||
ina := ina219.New(bus, 0x40, 0.1)
|
||||
defer ina.Close()
|
||||
|
||||
for {
|
||||
sv, err := ina.ShuntVoltage()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
v, err := ina.Voltage()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
c, err := ina.Current()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Printf("Shunt Voltage=%v Voltage=%v Current=%v\n", sv, v, c)
|
||||
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue