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