gobot.io/x/gobot/v2@v2.1.0/examples/tinkerboard_pcf8591.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 "log" 12 "time" 13 14 "gobot.io/x/gobot/v2" 15 "gobot.io/x/gobot/v2/drivers/aio" 16 "gobot.io/x/gobot/v2/drivers/i2c" 17 "gobot.io/x/gobot/v2/platforms/tinkerboard" 18 ) 19 20 func main() { 21 // This driver was tested with Tinkerboard and this board with temperature & brightness sensor: 22 // https://www.makershop.de/download/YL_40_PCF8591.pdf 23 // 24 // Wiring 25 // PWR Tinkerboard: 1 (+3.3V, VCC), 6, 9, 14, 20 (GND) 26 // I2C1 Tinkerboard: 3 (SDA), 5 (SCL) 27 // PCF8591 plate: wire AOUT --> AIN2 for this example 28 board := tinkerboard.NewAdaptor() 29 pcf := i2c.NewPCF8591Driver(board, i2c.WithBus(1)) 30 aout := aio.NewAnalogActuatorDriver(pcf, "AOUT") 31 aout.SetScaler(aio.AnalogActuatorLinearScaler(0, 3.3, 0, 255)) 32 33 var val int 34 var err error 35 36 // brightness sensor, high brightness - low raw value 37 descLight := "s.0" 38 // temperature sensor, high temperature - low raw value 39 // sometimes buggy, because not properly grounded 40 descTemp := "s.1" 41 // wired to AOUT 42 descAIN2 := "s.2" 43 // adjustable resistor, turn clockwise will lower the raw value 44 descResi := "s.3" 45 // the LED light is visible above ~1.7V, this means ~127 (half of 3.3V) 46 writeVal := 1.7 47 48 work := func() { 49 gobot.Every(1000*time.Millisecond, func() { 50 if err := aout.Write(writeVal); err != nil { 51 fmt.Println(err) 52 } else { 53 log.Printf("Write AOUT: %.1f V [0..3.3]", writeVal) 54 writeVal = writeVal + 0.1 55 if writeVal > 3.3 { 56 writeVal = 0 57 } 58 } 59 60 if val, err = pcf.AnalogRead(descLight); err != nil { 61 fmt.Println(err) 62 } else { 63 log.Printf("Brightness (%s): %d [255..0]", descLight, val) 64 } 65 66 if val, err = pcf.AnalogRead(descTemp); err != nil { 67 fmt.Println(err) 68 } else { 69 log.Printf("Temperature (%s): %d [255..0]", descTemp, val) 70 } 71 72 if val, err = pcf.AnalogRead(descAIN2); err != nil { 73 fmt.Println(err) 74 } else { 75 log.Printf("Read AOUT (%s): %d [0..255]", descAIN2, val) 76 } 77 78 if val, err = pcf.AnalogRead(descResi); err != nil { 79 fmt.Println(err) 80 } else { 81 log.Printf("Resistor (%s): %d [255..0]", descResi, val) 82 } 83 }) 84 } 85 86 robot := gobot.NewRobot("pcfBot", 87 []gobot.Connection{board}, 88 []gobot.Device{pcf}, 89 work, 90 ) 91 92 robot.Start() 93 }