Skip to content

Example 17: DS1302 RTC Clock ​

Experiment Overview ​

DS1302 is a low-power real-time clock chip that can maintain timekeeping after power-off via a coin cell battery. FastBee has a built-in DS1302 dedicated peripheral type (type: 50), supporting time read/set, trickle charging and 31 bytes of user RAM.

Hardware Wiring ​

Board LabelGPIO PinConnected Device
CLKGPIO14DS1302 clock line
DATGPIO27DS1302 data line
RSTGPIO26DS1302 reset/enable

DS1302 uses a three-wire interface (CE/IO/SCLK), requiring 3 GPIO pins.

JSON Configuration Example ​

json
{
  "peripherals": [
    {
      "id": "rtc_clk",
      "name": "DS1302-CLK",
      "type": 12,
      "enabled": false,
      "pins": [14],
      "params": { "initialState": 0 }
    },
    {
      "id": "rtc_dat",
      "name": "DS1302-DAT",
      "type": 12,
      "enabled": false,
      "pins": [27],
      "params": { "initialState": 0 }
    },
    {
      "id": "rtc_rst",
      "name": "DS1302-RST",
      "type": 12,
      "enabled": false,
      "pins": [26],
      "params": { "initialState": 0 }
    }
  ]
}

Peripheral Execution Linkage (Plan A: NTP Approach) ​

Scenario: Periodic Time Display (Timer Trigger) ​

Function: Update seven-segment display with NTP-synced system time every second

Web Interface Configuration Steps ​

Step 1: Ensure TM1637 seven-segment display is configured

  • Peripheral ID: tm1637_01
  • Type: Seven-segment display

Step 2: Create Rule

  1. Click Peripheral Execution in the left menu
  2. Click New Rule button
  3. Fill in basic configuration:
    • Rule Name: Periodic Time Display
    • Report Data: ✅ Enable
    • Enabled: ✅ Enable

Step 3: Configure Trigger

  1. Click Add Trigger button

  2. Fill in trigger configuration:

    FieldValueDescription
    Trigger TypeSelect Timer TriggerPeriodic update
    Timer ModeSelect Fixed IntervalBy millisecond interval
    Interval Time10001 second

Step 4: Configure Action

  1. Click Add Action button

  2. Fill in:

    • Action Type: Select Display Time
    • Target Peripheral: Select tm1637_01
  3. Click Save button

Approach Comparison ​

ApproachProsConsUse Case
NTPHigh precision, no hardware neededRequires networkNetworked devices
DS1302Retains on power-off, works offlineExtra hardware needed, lower precisionOffline devices

Notes ​

  1. Battery: DS1302 needs a CR2032 coin cell to maintain time during power-off
  2. NTP Priority: In networked environments, recommend NTP; DS1302 is suited for offline scenarios
  3. Driver Support: FastBee has built-in DS1302 driver (FASTBEE_ENABLE_DS1302=1)
  4. Precision: DS1302 precision is ~±2ppm (~5 seconds per month)
  5. Communication Protocol: DS1302 uses 3-wire SPI (non-standard SPI), timing requires software emulation

FastBee | fastbee.cn