Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to use DS3231/SD8931 Real-Time Clock by ESPHome
#1
Code:
esphome:
  name: a16v3-ds3231-oled
  min_version: 2026.11.0
  on_boot:
    priority: -100
    then:
      - ds3231.read_time:
          id: rtc_time

esp32:
  board: esp32-s3-devkitc-1
  framework:
    type: esp-idf

logger:

i2c:
  sda: GPIO9
  scl: GPIO10
  frequency: 100kHz

# Connect Ethernet to sync the clock via SNTP.
ethernet:
  type: W5500
  clk_pin: GPIO42
  mosi_pin: GPIO43
  miso_pin: GPIO44
  cs_pin: GPIO15
  interrupt_pin: GPIO2
  reset_pin: GPIO1

ds3231:
  address: 0x68

time:
  - platform: ds3231
    id: rtc_time
    timezone: Asia/Shanghai
  - platform: sntp
    timezone: Asia/Shanghai
    servers:
      - ntp.aliyun.com
      - pool.ntp.org
    on_time_sync:
      then:
        - ds3231.write_time:
            id: rtc_time

pcf8574:
  - id: outputs_1_8
    address: 0x24

switch:
  - platform: gpio
    id: out1
    internal: true
    restore_mode: ALWAYS_OFF
    pin:
      pcf8574: outputs_1_8
      number: 0
      mode: OUTPUT
      inverted: true

font:
  - file: "gfonts://Roboto Mono"
    id: oled_font
    size: 16

display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    address: 0x3C
    update_interval: 1s
    lambda: |-
      // Use the same timestamp for OUT1 and the display.
      const auto now = id(rtc_time).now();
      const bool active = now.is_valid() && (now.second % 10 < 5);
      if (id(out1).state != active) {
        if (active) id(out1).turn_on();
        else id(out1).turn_off();
      }
      if (now.is_valid()) {
        it.strftime(0, 0, id(oled_font), "%Y-%m-%d", now);
        it.strftime(0, 21, id(oled_font), "%H:%M:%S", now);
      } else {
        it.print(0, 0, id(oled_font), "Waiting for");
        it.print(0, 21, id(oled_font), "network time");
      }
      it.printf(0, 42, id(oled_font), "OUT1: %s", active ? "ON" : "OFF");

KinCony KC868-A16v3 ESPHome Demo – Control OUT1 with the DS3231 Real-Time Clock
This ESPHome YAML demonstrates how the KC868-A16v3 can control an output using its onboard DS3231 real-time clock (RTC). The 128×64 SSD1306 OLED shows the date, local time, and OUT1 status. Watch the last digit of the seconds and compare it with the OUT1 indicator LED. Digits 0–4 turn OUT1 ON, and digits 5–9 turn it OFF. The display refreshes once per second, and you can run the demo without connecting an external load or a Home Assistant server.
How to try it
  1. Flash
    Code:
    a16v3-ds3231-oled.txt
    to the board using ESPHome 2026.11.0 or later. The default time zone is
    Code:
    Asia/Shanghai
    . To display your local time, change both
    Code:
    timezone
    entries before compiling.
  2. For the first test, connect the board’s Ethernet port to a network with internet access. When ESPHome receives the time through SNTP, it writes it to the DS3231. If the board starts without a valid time, the OLED shows
    Code:
    Waiting for network time
    and OUT1 stays OFF.
  3. To test the RTC backup, make sure a working RTC battery is installed. Once the OLED shows the correct time, unplug Ethernet, power the board off for one minute, and power it back on with Ethernet still disconnected. The board should read the time from the DS3231, show that the minute has elapsed, and resume switching OUT1 every five seconds.

download yaml:

.txt   a16v3-ds3231-oled-HA.txt (Size: 1.77 KB / Downloads: 2)
Reply


Forum Jump:


Users browsing this thread:
1 Guest(s)