tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/ds3231/main.go (about) 1 // Connects to an DS3231 I2C Real Time Clock (RTC). 2 package main 3 4 import ( 5 "machine" 6 "time" 7 8 "fmt" 9 10 "tinygo.org/x/drivers/ds3231" 11 ) 12 13 func main() { 14 machine.I2C0.Configure(machine.I2CConfig{}) 15 16 rtc := ds3231.New(machine.I2C0) 17 rtc.Configure() 18 19 valid := rtc.IsTimeValid() 20 if !valid { 21 date := time.Date(2019, 12, 05, 20, 34, 12, 0, time.UTC) 22 rtc.SetTime(date) 23 } 24 25 running := rtc.IsRunning() 26 if !running { 27 err := rtc.SetRunning(true) 28 if err != nil { 29 fmt.Println("Error configuring RTC") 30 } 31 } 32 33 for { 34 dt, err := rtc.ReadTime() 35 if err != nil { 36 fmt.Println("Error reading date:", err) 37 } else { 38 fmt.Printf("Date: %d/%s/%02d %02d:%02d:%02d \r\n", dt.Year(), dt.Month(), dt.Day(), dt.Hour(), dt.Minute(), dt.Second()) 39 } 40 temp, _ := rtc.ReadTemperature() 41 fmt.Printf("Temperature: %.2f °C \r\n", float32(temp)/1000) 42 43 time.Sleep(time.Second * 1) 44 } 45 }