├── DS3231_example.py
├── LICENSE
├── README.md
└── my_lib
└── RTC_DS3231.py
/DS3231_example.py:
--------------------------------------------------------------------------------
1 | ##########################################
2 | ### This Code is for Raspberry Pi Pico ###
3 | ### copyright 2021 balance19 ###
4 | ##########################################
5 |
6 | from my_lib import RTC_DS3231
7 | import time
8 |
9 | # Initialisation of RTC object. Several settings are possible but everything is optional.
10 | # If you meet the standards (see /my_lib/RTC_DS3231.py) no parameters are needed.
11 | rtc = RTC_DS3231.RTC()
12 |
13 | # It is encoded like this: sec min hour week day month year.
14 | # Uncomment the line below to set time. Do this only once otherwise time will be set everytime the code is executed.
15 | # rtc.DS3231_SetTime(b'\x00\x14\x18\x28\x14\x07\x21')
16 |
17 | while True:
18 | t = rtc.DS3231_ReadTime(1) # Read RTC and receive data in Mode 1 (see /my_lib/RTC_DS3231.py)
19 | print(t)
20 | time.sleep(1)
21 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 balance19
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # micropython_DS3231
2 |
3 |
Motivation:
4 | I wrote this code mostly to learn new things in coding. I have a lot of experience with Atmel µControllers of course even Arduino's. Normally i code in Python and found out that there is micropython out there. That leads me to buy a "Raspberry Pi Pico"
5 |
6 | Why did i build this project:
7 | Honestly i didn't search the internet, wether there is already a solution out there or not. As i already mentioned i want to learn. ;-)
8 |
9 | What problem does this project solve:
10 | There isn't really a problem but makes the use of this board pretty easy. In addition the "Raspberry Pi Pico" is now capable to tell the correct time with high precision and more or less two lines of code.
11 |
12 | What did I learn:
13 | I learned the following things in this project
14 |
15 | - how to get started with the Raspberry Pi Pico and use VS Code to develope
16 | - how to work with classes
17 | - writing a library (never did this before)
18 |
19 |
20 | What Features will follow in this project:
21 | As my DS3231 Board has a EEPROM (24C32 32kbit) on board, i will include the functionalaty in this project. The Board i use, was originally for Arduino. Sadly i don't know the name of my board anymore.
22 |
23 | How to use this library:
24 |
25 | - copy the folder my_lib in your project
26 | - add "from my_lib import RTC_DS3231" on top of your main file.
27 | - create the RTC object with "rtc = RTC_DS3231.RTC()"
28 | - use "rtc.DS3231_SetTime(b'\x00\x14\x18\x28\x14\x07\x21')" (modify the time and date) to set the Time. Do this only once or if you want to correct the Time.
29 | - use "t = rtc.DS3231_ReadTime(1)" to get the current Time.
30 | - use "print(t)" to send the time over uart to the PC
31 |
32 |
--------------------------------------------------------------------------------
/my_lib/RTC_DS3231.py:
--------------------------------------------------------------------------------
1 | ##########################################
2 | ### This Code is for Raspberry Pi Pico ###
3 | ### copyright 2021 balance19 ###
4 | ##########################################
5 |
6 | import machine
7 |
8 | # Class for getting Realtime from the DS3231 in different modes.
9 | class RTC:
10 | w = ["FRI", "SAT", "SUN", "MON", "TUE", "WED", "THU"]
11 | # If you want different names for Weekdays, feel free to add. Couple examples below:
12 | # w = ["FR", "SA", "SU", "MO", "TU", "WE", "TH"]
13 | # w = ["Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"]
14 | # w = ["Freitag", "Samstag", "Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag"]
15 | # w = ["viernes", "sabado", "domingo", "lunes", "martes", "miercoles", "jueves"]
16 |
17 | # Initialisation of RTC object. Several settings are possible but everything is optional.
18 | # If you meet these standards no parameters are required.
19 | def __init__(self, sda_pin=16, scl_pin=17, port=0, speed=100000, address=0x68, register=0x00):
20 | self.rtc_address = address # for using different i2c address
21 | self.rtc_register = register # for using different register on device. DON'T change for DS3231
22 | sda = machine.Pin(sda_pin) # configure the sda pin
23 | scl = machine.Pin(scl_pin) # configure the scl pin
24 | self.i2c = machine.I2C(port, sda=sda, scl=scl, freq=speed) # configure the i2c interface with given parameters
25 |
26 | # Method for setting the Time
27 | def DS3231_SetTime(self, NowTime=b"\x00\x23\x12\x28\x14\x07\x21"):
28 | # NowTime has to be in format like b'\x00\x23\x12\x28\x14\x07\x21'
29 | # It is encoded like this sec min hour week day month year
30 | # Then it's written to the DS3231
31 | self.i2c.writeto_mem(int(self.rtc_address), int(self.rtc_register), NowTime)
32 |
33 | # DS3231 gives data in bcd format. This has to be converted to a binary format.
34 | def bcd2bin(self, value):
35 | return (value or 0) - 6 * ((value or 0) >> 4)
36 |
37 | # Add a 0 in front of numbers smaller than 10
38 | def pre_zero(self, value):
39 | pre_zero = True # Change to False if you don't want a "0" in front of numbers smaller than 10
40 | if pre_zero:
41 | if value < 10:
42 | value = f"0{value}" # From now on the value is a string!
43 | return value
44 |
45 | # Read the Realtime from the DS3231 with errorhandling. Currently two output modes can be used.
46 | def DS3231_ReadTime(self, mode=0):
47 | try:
48 | # Read RT from DS3231 and write to the buffer variable. It's a list with 7 entries.
49 | # Every entry needs to be converted from bcd to bin.
50 | buffer = self.i2c.readfrom_mem(self.rtc_address, self.rtc_register, 7)
51 | # The year consists of 2 digits. Here 2000 years are added to get format like "2021"
52 | year = self.bcd2bin(buffer[6]) + 2000
53 | month = self.bcd2bin(buffer[5]) # Just put the month value in the month variable and convert it.
54 | day = self.bcd2bin(buffer[4]) # Same for the day value
55 | # Weekday will be converted in the weekdays name or shortform like "Sunday" or "SUN"
56 | weekday = self.w[self.bcd2bin(buffer[3])]
57 | # Uncomment the line below if you want a number for the weekday and comment the line before.
58 | # weekday = self.bcd2bin(buffer[3])
59 | hour = self.pre_zero(self.bcd2bin(buffer[2])) # Convert bcd to bin and add a "0" if necessary
60 | minute = self.pre_zero(self.bcd2bin(buffer[1])) # Convert bcd to bin and add a "0" if necessary
61 | second = self.pre_zero(self.bcd2bin(buffer[0])) # Convert bcd to bin and add a "0" if necessary
62 | if mode == 0: # Mode 0 returns a list of second, minute, ...
63 | return second, minute, hour, weekday, day, month, year
64 | if mode == 1: # Mode 1 returns a formated string with time, weekday and date
65 | time_string = f"{hour}:{minute}:{second} {weekday} {day}.{month}.{year}"
66 | return time_string
67 | # If you need different format, feel free to add
68 |
69 | except Exception as e:
70 | return (
71 | "Error: is the DS3231 not connected or some other problem (%s)" % e
72 | ) # exception occurs in any case of error.
73 |
--------------------------------------------------------------------------------