/ README.rst
README.rst
1 2 Introduction to Adafruit's PCF8523 Real Time Clock (RTC) Library 3 ================================================================ 4 5 .. image:: https://readthedocs.org/projects/adafruit-circuitpython-pcf8523/badge/?version=latest 6 :target: https://circuitpython.readthedocs.io/projects/pcf8523/en/latest/ 7 :alt: Documentation Status 8 9 .. image :: https://img.shields.io/discord/327254708534116352.svg 10 :target: https://adafru.it/discord 11 :alt: Discord 12 13 .. image:: https://github.com/adafruit/Adafruit_CircuitPython_PCF8523/workflows/Build%20CI/badge.svg 14 :target: https://github.com/adafruit/Adafruit_CircuitPython_PCF8523/actions/ 15 :alt: Build Status 16 17 This is a great battery-backed real time clock (RTC) that allows your 18 microcontroller project to keep track of time even if it is reprogrammed, 19 or if the power is lost. Perfect for datalogging, clock-building, time 20 stamping, timers and alarms, etc. Equipped with PCF8523 RTC - it can 21 run from 3.3V or 5V power & logic! 22 23 The PCF8523 is simple and inexpensive but not a high precision device. 24 It may lose or gain up to two seconds a day. For a high-precision, 25 temperature compensated alternative, please check out the 26 `DS3231 precision RTC. <https://www.adafruit.com/products/3013>`_ 27 If you need a DS1307 for compatibility reasons, check out our 28 `DS1307 RTC breakout <https://www.adafruit.com/products/3296>`_. 29 30 .. image:: _static/3295-00.jpg 31 :alt: PCF8523 Breakout Board 32 33 Dependencies 34 ============= 35 36 This driver depends on the `Register <https://github.com/adafruit/Adafruit_CircuitPython_Register>`_ 37 and `Bus Device <https://github.com/adafruit/Adafruit_CircuitPython_BusDevice>`_ 38 libraries. Please ensure they are also available on the CircuitPython filesystem. 39 This is easily achieved by downloading 40 `a library and driver bundle <https://github.com/adafruit/Adafruit_CircuitPython_Bundle>`_. 41 42 Installing from PyPI 43 ==================== 44 On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally `from 45 PyPI <https://pypi.org/project/adafruit-circuitpython-pcf8523/>`_. To install for current user: 46 47 .. code-block:: shell 48 49 pip3 install adafruit-circuitpython-pcf8523 50 51 To install system-wide (this may be required in some cases): 52 53 .. code-block:: shell 54 55 sudo pip3 install adafruit-circuitpython-pcf8523 56 57 To install in a virtual environment in your current project: 58 59 .. code-block:: shell 60 61 mkdir project-name && cd project-name 62 python3 -m venv .env 63 source .env/bin/activate 64 pip3 install adafruit-circuitpython-pcf8523 65 66 67 Usage Notes 68 =========== 69 70 Basics 71 ------ 72 73 Of course, you must import the library to use it: 74 75 .. code:: python 76 77 import busio 78 import adafruit_pcf8523 79 import time 80 81 All the Adafruit RTC libraries take an instantiated and active I2C object 82 (from the `busio` library) as an argument to their constructor. The way to 83 create an I2C object depends on the board you are using. For boards with labeled 84 SCL and SDA pins, you can: 85 86 .. code:: python 87 88 from board import * 89 90 You can also use pins defined by the onboard `microcontroller` through the 91 `microcontroller.Pin` module. 92 93 Now, to initialize the I2C bus: 94 95 .. code:: python 96 97 i2c_bus = busio.I2C(SCL, SDA) 98 99 Once you have created the I2C interface object, you can use it to instantiate 100 the RTC object: 101 102 .. code:: python 103 104 rtc = adafruit_pcf8523.PCF8523(i2c_bus) 105 106 Date and time 107 ------------- 108 109 To set the time, you need to set datetime` to a `time.struct_time` object: 110 111 .. code:: python 112 113 rtc.datetime = time.struct_time((2017,1,9,15,6,0,0,9,-1)) 114 115 After the RTC is set, you retrieve the time by reading the `datetime` 116 attribute and access the standard attributes of a struct_time such as ``tm_year``, 117 ``tm_hour`` and ``tm_min``. 118 119 .. code:: python 120 121 t = rtc.datetime 122 print(t) 123 print(t.tm_hour, t.tm_min) 124 125 Alarm 126 ----- 127 128 To set the time, you need to set `alarm` to a tuple with a `time.struct_time` 129 object and string representing the frequency such as "hourly": 130 131 .. code:: python 132 133 rtc.alarm = (time.struct_time((2017,1,9,15,6,0,0,9,-1)), "daily") 134 135 After the RTC is set, you retrieve the alarm status by reading the 136 `alarm_status` attribute. Once True, set it back to False to reset. 137 138 .. code:: python 139 140 if rtc.alarm_status: 141 print("wake up!") 142 rtc.alarm_status = False 143 144 Contributing 145 ============ 146 147 Contributions are welcome! Please read our `Code of Conduct 148 <https://github.com/adafruit/Adafruit_CircuitPython_PCF8523/blob/master/CODE_OF_CONDUCT.md>`_ 149 before contributing to help this project stay welcoming. 150 151 Documentation 152 ============= 153 154 For information on building library documentation, please check out `this guide <https://learn.adafruit.com/creating-and-sharing-a-circuitpython-library/sharing-our-docs-on-readthedocs#sphinx-5-1>`_.