2023-05-20 20:25:21 +08:00
|
|
|
//go:build example
|
2022-03-25 01:40:26 +08:00
|
|
|
// +build example
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2022-03-25 01:40:26 +08:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/drivers/i2c"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/tinkerboard"
|
2022-03-25 01:40:26 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// Wiring
|
|
|
|
// PWR Tinkerboard: 1 (+3.3V, VCC), 6, 9, 14, 20 (GND)
|
|
|
|
// I2C1 Tinkerboard: 3 (SDA), 5 (SCL)
|
|
|
|
// YL-40 module: wire AOUT --> AIN2 for this example
|
|
|
|
//
|
|
|
|
// Note: temperature measurement is often buggy, because sensor is not properly grounded
|
|
|
|
// fix it by soldering a small bridge to the adjacent ground pin of brightness sensor
|
|
|
|
board := tinkerboard.NewAdaptor()
|
|
|
|
yl := i2c.NewYL40Driver(board, i2c.WithBus(1))
|
|
|
|
|
|
|
|
work := func() {
|
|
|
|
// the LED light is visible above ~1.7V
|
|
|
|
writeVal, _ := yl.AOUT()
|
|
|
|
|
|
|
|
gobot.Every(1000*time.Millisecond, func() {
|
|
|
|
if err := yl.Write(writeVal); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
} else {
|
|
|
|
log.Printf(" %.1f V written", writeVal)
|
|
|
|
writeVal = writeVal + 0.1
|
|
|
|
if writeVal > 3.3 {
|
|
|
|
writeVal = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if brightness, err := yl.ReadBrightness(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
} else {
|
|
|
|
log.Printf("Brightness: %.0f [0..1000]", brightness)
|
|
|
|
}
|
|
|
|
|
|
|
|
if temperature, err := yl.ReadTemperature(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
} else {
|
|
|
|
log.Printf("Temperature: %.1f °C", temperature)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ain2, err := yl.ReadAIN2(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
} else {
|
|
|
|
log.Printf("Read back AOUT: %.1f [0..3.3]", ain2)
|
|
|
|
}
|
|
|
|
|
|
|
|
if potiState, err := yl.ReadPotentiometer(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
} else {
|
|
|
|
log.Printf("Resistor: %.0f %% [-100..+100]", potiState)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("yl40Bot",
|
|
|
|
[]gobot.Connection{board},
|
|
|
|
[]gobot.Device{yl},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
|
|
|
robot.Start()
|
|
|
|
}
|