gobot.io/x/gobot/v2@v2.1.0/examples/tinkerboard_yl40.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/i2c" 16 "gobot.io/x/gobot/v2/platforms/tinkerboard" 17 ) 18 19 func main() { 20 // Wiring 21 // PWR Tinkerboard: 1 (+3.3V, VCC), 6, 9, 14, 20 (GND) 22 // I2C1 Tinkerboard: 3 (SDA), 5 (SCL) 23 // YL-40 module: wire AOUT --> AIN2 for this example 24 // 25 // Note: temperature measurement is often buggy, because sensor is not properly grounded 26 // fix it by soldering a small bridge to the adjacent ground pin of brightness sensor 27 board := tinkerboard.NewAdaptor() 28 yl := i2c.NewYL40Driver(board, i2c.WithBus(1)) 29 30 work := func() { 31 // the LED light is visible above ~1.7V 32 writeVal, _ := yl.AOUT() 33 34 gobot.Every(1000*time.Millisecond, func() { 35 if err := yl.Write(writeVal); err != nil { 36 fmt.Println(err) 37 } else { 38 log.Printf(" %.1f V written", writeVal) 39 writeVal = writeVal + 0.1 40 if writeVal > 3.3 { 41 writeVal = 0 42 } 43 } 44 45 if brightness, err := yl.ReadBrightness(); err != nil { 46 fmt.Println(err) 47 } else { 48 log.Printf("Brightness: %.0f [0..1000]", brightness) 49 } 50 51 if temperature, err := yl.ReadTemperature(); err != nil { 52 fmt.Println(err) 53 } else { 54 log.Printf("Temperature: %.1f °C", temperature) 55 } 56 57 if ain2, err := yl.ReadAIN2(); err != nil { 58 fmt.Println(err) 59 } else { 60 log.Printf("Read back AOUT: %.1f [0..3.3]", ain2) 61 } 62 63 if potiState, err := yl.ReadPotentiometer(); err != nil { 64 fmt.Println(err) 65 } else { 66 log.Printf("Resistor: %.0f %% [-100..+100]", potiState) 67 } 68 }) 69 } 70 71 robot := gobot.NewRobot("yl40Bot", 72 []gobot.Connection{board}, 73 []gobot.Device{yl}, 74 work, 75 ) 76 77 robot.Start() 78 }