Giter VIP home page Giter VIP logo

adafruit_circuitpython_pcf8523's Introduction

Introduction to Adafruit's PCF8523 Real Time Clock (RTC) Library

Documentation Status

Discord

Build Status

Code Style: Black

This is a great battery-backed real time clock (RTC) that allows your microcontroller project to keep track of time even if it is reprogrammed, or if the power is lost. Perfect for datalogging, clock-building, time stamping, timers and alarms, etc. Equipped with PCF8523 RTC - it can run from 3.3V or 5V power & logic!

The PCF8523 is simple and inexpensive but not a high precision device. It may lose or gain up to two seconds a day. For a high-precision, temperature compensated alternative, please check out the DS3231 precision RTC. If you need a DS1307 for compatibility reasons, check out our DS1307 RTC breakout.

PCF8523 Breakout Board

Dependencies

This driver depends on the Register and Bus Device libraries. Please ensure they are also available on the CircuitPython filesystem. This is easily achieved by downloading a library and driver bundle.

Installing from PyPI

On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally from PyPI. To install for current user:

pip3 install adafruit-circuitpython-pcf8523

To install system-wide (this may be required in some cases):

sudo pip3 install adafruit-circuitpython-pcf8523

To install in a virtual environment in your current project:

mkdir project-name && cd project-name
python3 -m venv .venv
source .venv/bin/activate
pip3 install adafruit-circuitpython-pcf8523

Usage Notes

Basics

Of course, you must import the library to use it:

import time
from adafruit_pcf8523.pcf8523 import PCF8523

All the Adafruit RTC libraries take an instantiated and active I2C object (from the board library) as an argument to their constructor. The way to create an I2C object depends on the board you are using. For boards with labeled SCL and SDA pins, you can:

import board

Now, to initialize the I2C bus:

i2c = board.I2C()

Once you have created the I2C interface object, you can use it to instantiate the RTC object:

rtc = PCF8523(i2c)

Date and time

To set the time, you need to set datetime` to a time.struct_time object:

rtc.datetime = time.struct_time((2017,1,9,15,6,0,0,9,-1))

After the RTC is set, you retrieve the time by reading the datetime attribute and access the standard attributes of a struct_time such as tm_year, tm_hour and tm_min.

t = rtc.datetime
print(t)
print(t.tm_hour, t.tm_min)

Alarm

To set the time, you need to set alarm to a tuple with a time.struct_time object and string representing the frequency such as "hourly":

rtc.alarm = (time.struct_time((2017,1,9,15,6,0,0,9,-1)), "daily")

After the RTC is set, you retrieve the alarm status by reading the alarm_status attribute. Once True, set it back to False to reset.

if rtc.alarm_status:
    print("wake up!")
    rtc.alarm_status = False

Documentation

API documentation for this library can be found on Read the Docs.

For information on building library documentation, please check out this guide.

Contributing

Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.

adafruit_circuitpython_pcf8523's People

Contributors

bablokb avatar brennen avatar demophoon avatar dhalbert avatar evaherrada avatar foamyguy avatar hukuzatuna avatar jepler avatar jposada202020 avatar kattni avatar ladyada avatar mrmcwethy avatar nosferatujr avatar sommersoft avatar tannewt avatar tekktrik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

adafruit_circuitpython_pcf8523's Issues

Missing Type Annotations

There are missing type annotations for some functions in this library.

The typing module does not exist on CircuitPython devices so the import needs to be wrapped in try/except to catch the error for missing import. There is an example of how that is done here:

try:
    from typing import List, Tuple
except ImportError:
    pass

Once imported the typing annotations for the argument type(s), and return type(s) can be added to the function signature. Here is an example of a function that has had this done already:

def wrap_text_to_pixels(
    string: str, max_width: int, font=None, indent0: str = "", indent1: str = ""
) -> List[str]:

If you are new to Git or Github we have a guide about contributing to our projects here: https://learn.adafruit.com/contribute-to-circuitpython-with-git-and-github

There is also a guide that covers our CI utilities and how to run them locally to ensure they will pass in Github Actions here: https://learn.adafruit.com/creating-and-sharing-a-circuitpython-library/check-your-code In particular the pages: Sharing docs on ReadTheDocs and Check your code with pre-commit contain the tools to install and commands to run locally to run the checks.

If you are attempting to resolve this issue and need help, you can post a comment on this issue and tag both @FoamyGuy and @kattni or reach out to us on Discord: https://adafru.it/discord in the #circuitpython-dev channel.

The following locations are reported by mypy to be missing type annotations:

  • adafruit_pcf8523.py:139
  • adafruit_pcf8523.py:160

Error setting time

I tried accessing the PCF8523 on an Adalogger Featherwing following the example but get this error when setting the time.


>>> import busio
>>> import adafruit_pcf8523
>>> import time
>>> from board import *
>>> myI2C = busio.I2C(SCL, SDA)
>>> rtc = adafruit_pcf8523.PCF8523(myI2C)
>>> rtc.datetime = time.struct_time((2017,1,9,15,6,0,0,9,-1))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "libraries/drivers/pcf8523/adafruit_pcf8523.py", line 121, in datetime
  File "libraries/helpers/register/adafruit_register/i2c_bcd_datetime.py", line 81, in __set__
AttributeError: 'struct_time' object has no attribute 'second'

RTC loses time after shutdown in dtoverlay state.

Hi -

1st method:

I have added dtoverlay=i2c_rtc,pcf8523 in the boot/config.txt
i2cdetect -y 1 shows UU instead 0x68.

I soldered the crystal oscillator and rtc chip started keeping track of time after setting the time, let's say 1pm Although, when I shutdown the system for 10 mins approx., rtc chip should give me 1:10pm, but its giving me 1:01pm time. There is a battery coin cell as well. It seems like rtc loses 10mins time after complete shutdown of the system and when I turn on the system(via main power supply) the time would start from where it was left before like 1:01pm.

I dont know how to fix this issue. Please suggest.

2nd method:
When I comment out dtoverlay and it obviousl shows me 0x68. In this case, I am not losing time even after I turn on the system after 10 mins. Seems like library is writing 0b000 to enable battery switch over. That's exactly what we want. Although, how to get Time Zone?
I do not know how to write to control register3 while device is in reserved address mode (0xUU). Any suggestions would be appreciated.

Thanks,
Akshay

Originally posted by @TinManAkshay in #27 (comment)

Alarm not working for 'minutely' frequency

Using the M0 Express and the Adalogger FeatherWing - RTC + SD Add-on. Alarm function can be called with "minutely" frequency without error, but return value from alarm is not correct and alarm_status never becomes True.

Works as expected when frequency is set to "hourly".

Code:

import time
import busio
from board import *
import sys
import adafruit_pcf8523

myI2C = busio.I2C(SCL, SDA)

rtc = adafruit_pcf8523.PCF8523(myI2C)

rtc.datetime = time.struct_time((2017,10,15,16,59,0,0,9,-1))
rtc.alarm_interrupt = True
rtc.alarm = (time.struct_time((2017,10,15,17,00,0,0,9,-1)), "minutely")

t = rtc.datetime
a = rtc.alarm
print(rtc.alarm_interrupt)
print(t)
print(a)
print(t.tm_hour, t.tm_min)

while True:
        time.sleep(10)
        t = rtc.datetime
        print(t.tm_hour, t.tm_min)

        if rtc.alarm_status:
		print("wake up!")
		rtc.alarm_status = False

Output:

code.py output:
struct_time(tm_year=2017, tm_mon=10, tm_mday=15, tm_hour=16, tm_min=15, tm_sec=0, tm_wday=0, tm_yday=-1, tm_isdst=-1)
(struct_time(tm_year=2017, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=1, tm_isdst=-1), None)
16 15
16 15
16 15
16 15
16 15
16 15
16 16
16 16
16 16
16 16
16 16
16 16
16 17
16 17
16 17

Finishing Touches For PyPI

This needs a couple finishing touches for PyPI:

  • README needs PyPI install directions (and other missing standard items)
  • Add Blinka to setup.py->install_requires

RTC datetime doesn't change

This is my code:

image

Output:

image

Once I have set the time and then try to print the time, time doesn't change. It will just print the fixed value. Its not iterating through the time. I'd appreciate if anybody could suggest the fix or point out some issues with the system.

Thanks,
Akshay

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.