tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/ds18b20/main.go (about) 1 package main 2 3 import ( 4 "encoding/hex" 5 "machine" 6 "time" 7 8 "tinygo.org/x/drivers/onewire" 9 10 "tinygo.org/x/drivers/ds18b20" 11 ) 12 13 func main() { 14 // Define pin for DS18B20 15 pin := machine.D2 16 17 ow := onewire.New(pin) 18 romIDs, err := ow.Search(onewire.SEARCH_ROM) 19 if err != nil { 20 println(err) 21 } 22 sensor := ds18b20.New(ow) 23 24 for { 25 time.Sleep(3 * time.Second) 26 27 println() 28 println("Device:", machine.Device) 29 30 println() 31 println("Request Temperature.") 32 for _, romid := range romIDs { 33 println("Sensor RomID: ", hex.EncodeToString(romid)) 34 sensor.RequestTemperature(romid) 35 } 36 37 // wait 750ms or more for DS18B20 convert T 38 time.Sleep(1 * time.Second) 39 40 println() 41 println("Read Temperature") 42 for _, romid := range romIDs { 43 raw, err := sensor.ReadTemperatureRaw(romid) 44 if err != nil { 45 println(err) 46 } 47 println() 48 println("Sensor RomID: ", hex.EncodeToString(romid)) 49 println("Temperature Raw value: ", hex.EncodeToString(raw)) 50 51 t, err := sensor.ReadTemperature(romid) 52 if err != nil { 53 println(err) 54 } 55 println("Temperature in celsius milli degrees (°C/1000): ", t) 56 } 57 } 58 }