gobot.io/x/gobot/v2@v2.1.0/examples/raspi_ads1015.go (about)

     1  //go:build example
     2  // +build example
     3  
     4  //
     5  // Do not build by default.
     6  
     7  package main
     8  
     9  import (
    10  	"fmt"
    11  	"time"
    12  
    13  	"gobot.io/x/gobot/v2"
    14  	"gobot.io/x/gobot/v2/drivers/i2c"
    15  	"gobot.io/x/gobot/v2/platforms/raspi"
    16  )
    17  
    18  func main() {
    19  	a := raspi.NewAdaptor()
    20  	// Use the gain to be able to read values of at least 5V (for all channels)
    21  	ads1015 := i2c.NewADS1015Driver(a, i2c.WithADS1x15BestGainForVoltage(5.0))
    22  
    23  	work := func() {
    24  		gobot.Every(100*time.Millisecond, func() {
    25  			v, _ := ads1015.ReadWithDefaults(0)
    26  			fmt.Println("A0", v)
    27  		})
    28  	}
    29  
    30  	robot := gobot.NewRobot("ads1015bot",
    31  		[]gobot.Connection{a},
    32  		[]gobot.Device{ads1015},
    33  		work,
    34  	)
    35  
    36  	robot.Start()
    37  }