PyPCF8523/examples/calibration_example.py
2025-11-10 18:05:25 +01:00

102 lines
3.2 KiB
Python

#!/usr/bin/env python3
"""Calibration example for PyPCF8523 RTC driver.
This example demonstrates how to calibrate the RTC for better accuracy:
- Reading calibration settings
- Adjusting calibration offset
- Setting calibration schedule
The PCF8523 can drift up to 2 seconds per day. Calibration helps
compensate for this drift.
Calibration offset range: -64 to +63
- Positive values speed up the clock
- Negative values slow it down
Calibration schedule:
- Per minute: 1 LSB = 4.069 ppm
- Per 2 hours: 1 LSB = 4.340 ppm
"""
import time
from pypcf8523 import PCF8523
def main():
print("PCF8523 Calibration Example")
print("=" * 50)
# Initialize the RTC
rtc = PCF8523(i2c_bus=1)
# Read current calibration settings
current_offset = rtc.calibration
per_minute = rtc.calibration_schedule_per_minute
print(f"Current calibration offset: {current_offset}")
print(f"Calibration schedule: {'Per minute' if per_minute else 'Per 2 hours'}")
# Calculate ppm (parts per million) offset
ppm_per_lsb = 4.069 if per_minute else 4.340
ppm_offset = current_offset * ppm_per_lsb
print(f"Approximate offset: {ppm_offset:.2f} ppm")
# Example: Set calibration
print("\n" + "-" * 50)
print("Example calibration adjustment:")
print("-" * 50)
# If your RTC is running fast (gaining time), use negative offset
# If your RTC is running slow (losing time), use positive offset
# Example: Clock gains 2 seconds per day
# 2 seconds / 86400 seconds = 23.15 ppm
# Offset needed: 23.15 / 4.069 ≈ -6 (per minute mode)
new_offset = 0 # Change this based on your measurements
print(f"\nTo set calibration offset to {new_offset}:")
print(f" rtc.calibration = {new_offset}")
if new_offset != 0:
print("\nUncomment the following lines to apply:")
print(" # rtc.calibration_schedule_per_minute = True")
print(f" # rtc.calibration = {new_offset}")
print(f" # This would give approximately {new_offset * 4.069:.2f} ppm offset")
# Uncomment to actually apply calibration:
# rtc.calibration_schedule_per_minute = True
# rtc.calibration = new_offset
# How to measure drift:
print("\n" + "=" * 50)
print("How to measure and calibrate your RTC:")
print("=" * 50)
print("1. Set the RTC to accurate time (sync with NTP)")
print("2. Wait 24-48 hours")
print("3. Compare RTC time with accurate time")
print("4. Calculate drift in seconds per day")
print("5. Convert to ppm: (drift_seconds / 86400) * 1,000,000")
print("6. Calculate offset: ppm / 4.069 (per minute mode)")
print("7. Apply opposite sign: if fast use negative, if slow use positive")
print("8. Set the calibration offset")
print("\nExample:")
print(" If RTC gains 2 seconds/day:")
print(" 2 / 86400 * 1000000 = 23.15 ppm")
print(" 23.15 / 4.069 = 5.69 ≈ 6")
print(" Use offset = -6 (negative because it's fast)")
# Check battery status
print("\n" + "-" * 50)
if rtc.battery_low:
print("⚠️ WARNING: Backup battery is low!")
else:
print("✓ Backup battery is OK")
# Clean up
rtc.close()
print("\nRTC connection closed")
if __name__ == "__main__":
main()