Skip to main content

MicroPython driver for DS1307 RTC

Project description

MicroPython DS1307

Downloads Release MicroPython License: MIT codecov CI

MicroPython driver for DS1307 RTC


General

MicroPython driver for DS1307 RTC

📚 The latest documentation is available at MicroPython DS1307 ReadTheDocs 📚

Installation

Install required tools

Python3 must be installed on your system. Check the current Python version with the following command

python --version
python3 --version

Depending on which command Python 3.x.y (with x.y as some numbers) is returned, use that command to proceed.

python3 -m venv .venv
source .venv/bin/activate

pip install -r requirements.txt

Setup

Install package with upip

Connect the MicroPython device to a network (if possible)

import network
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect('SSID', 'PASSWORD')
station.isconnected()

General

Install the latest package version of this lib on the MicroPython device

import mip
mip.install("github:brainelectronics/micropython-ds1307")

For MicroPython versions below 1.19.1 use the upip package instead of mip

import upip
upip.install('micropython-ds1307')

Specific version

Install a specific, fixed package version of this lib on the MicroPython device

import mip
# install a verions of a specific branch
mip.install("github:brainelectronics/micropython-ds1307", version="feature/initial-implementation")
# install a tag version
mip.install("github:brainelectronics/micropython-ds1307", version="0.1.0")

For MicroPython versions below 1.19.1 use the upip package instead of mip

import upip
upip.install('micropython-ds1307')

Test version

Install a specific release candidate version uploaded to Test Python Package Index on every PR on the MicroPython device. If no specific version is set, the latest stable version will be used.

import mip
mip.install("github:brainelectronics/micropython-ds1307", version="0.1.0-rc1.dev1")

For MicroPython versions below 1.19.1 use the upip package instead of mip

import upip
# overwrite index_urls to only take artifacts from test.pypi.org
upip.index_urls = ['https://test.pypi.org/pypi']
upip.install('micropython-ds1307')

See also brainelectronics Test PyPi Server in Docker for a test PyPi server running on Docker.

Manually

Upload files to board

Copy the module to the MicroPython board and import them as shown below using Remote MicroPython shell

Open the remote shell with the following command. Additionally use -b 115200 in case no CP210x is used but a CH34x.

rshell --port /dev/tty.SLAB_USBtoUART --editor nano

Perform the following command inside the rshell to copy all files and folders to the device

mkdir /pyboard/lib
mkdir /pyboard/lib/ds1307

cp ds1307/* /pyboard/lib/ds1307

cp examples/main.py /pyboard
cp examples/boot.py /pyboard

Usage

from ds1307 import DS1307
from machine import I2C, Pin
from time import gmtime, time

# DS1307 on 0x68
I2C_ADDR = 0x68     # DEC 104, HEX 0x68

# define custom I2C interface, default is 'I2C(0)'
# check the docs of your device for further details and pin infos
# this are the pins for the Raspberry Pi Pico adapter board
i2c = I2C(0, scl=Pin(13), sda=Pin(12), freq=800000)
ds1307 = DS1307(addr=I2C_ADDR, i2c=i2c)

# get the current RTC time
print("Current RTC time: {}".format(ds1307.datetime))

# set the RTC time to the current system time
now = gmtime(time())
ds1307.datetime = now

print("Current RTC time: {}".format(ds1307.datetime))

# Print the date and time in ISO8601 format: 2023-04-18T21:14:22
print("Today is {:04d}-{:02d}-{:02d}T{:02d}:{:02d}:{:02d}".format(
    ds1307.year, ds1307.month, ds1307.day,
    ds1307.hour, ds1307.minute, ds1307.second))

Contributing

Unittests

Run the unittests locally with the following command after installing this package in a virtual environment

# run all tests
nose2 --config tests/unittest.cfg

# run only one specific tests
nose2 tests.test_ds1307.TestDS1307.test_addr

Generate the coverage files with

python create_report_dirs.py
coverage html

The coverage report is placed at reports/coverage/html/index.html

Credits

Based on Mike Causer's MicroPython TinyRTC I2C module and the PyPa sample project

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

micropython-ds1307-0.1.1.tar.gz (6.1 kB view details)

Uploaded Source

File details

Details for the file micropython-ds1307-0.1.1.tar.gz.

File metadata

  • Download URL: micropython-ds1307-0.1.1.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.17

File hashes

Hashes for micropython-ds1307-0.1.1.tar.gz
Algorithm Hash digest
SHA256 48fdf846a2efa5a4c489f9d10f652a9517e5bf15b11a38f913d967634ffde5af
MD5 70146dd19c14d77e5555011d4ed5cfad
BLAKE2b-256 34ebe13c72ad925d1bde6839854ab088a7e3e948e0dbe15ba7dec15003192e53

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page