DS3231 RTC library help???

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
Algester
Posts: 4
Joined: Thu Dec 09, 2021 4:06 pm

DS3231 RTC library help???

Post by Algester » Sun Dec 12, 2021 5:26 am

I initially posted this in the Raspberry pico board of this forum but I may perhaps get better results here

I have a library for the DS3231
https://github.com/Algester/Pico-Clock- ... _DS3231.py

right now I have slapped in the Month array or so I think should be the month array
with this
mon = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]

and then implemented

under def DS3231_ReadTime
month = self.mon[self.bcd2bin(buffer[5])]
which I think should be able to convert the numbers into text based on the array

but somehow changing the readtime mode to 2 (where the logic should be implemented is spitting out
Error: DS3231 is not connected? on the LCD screen

while not related I also need to know how do I "convert" the date time from 24 hours to 12 hours I havent seen an example that makes it "work" every sample I see in the internet only spits out 24 hours I know it should be sent to the main.py but I just have no clear clue on how to make it work (I know I may have to do some basic math??)

and finally my issue with my am3231 driver seems to spit out float values that the LCD api doesnt seem to like... so I'm not sure if doing
lcd.putstr(int(sensor.temperature()))
is even remotely correct but seems to be the most simplest thing to do

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: DS3231 RTC library help???

Post by pythoncoder » Sun Dec 12, 2021 11:05 am

There is a tested, cross-platform MicroPython library for DS3231 here.
Peter Hinch
Index to my micropython libraries.

Algester
Posts: 4
Joined: Thu Dec 09, 2021 4:06 pm

Re: DS3231 RTC library help???

Post by Algester » Sun Dec 12, 2021 2:18 pm

I'll try this out... maybe I could get results the way I want t too cause I have been stumped on why changing str(month) to month makes the library go bonkers when weekday works on my initial library

Algester
Posts: 4
Joined: Thu Dec 09, 2021 4:06 pm

Re: DS3231 RTC library help???

Post by Algester » Mon Dec 13, 2021 7:14 am

Code: Select all

#from lib import RTC_DS3231
from lib import ds3231_port
from lib import am2320
import time

import utime

import machine
from ds3231_port import DS3231
from machine import I2C
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd

I2C_ADDR     = 0x27
I2C_NUM_ROWS = 2
I2C_NUM_COLS = 16

i2c = I2C(0, sda=machine.Pin(0), scl=machine.Pin(1), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
ds3231 = DS3231(i2c)
sensor = am2320.AM2320(i2c)

#rtc.datetime((MM, DD, YY, wday, hh, mm, ss, 0))
#from this line down before pin is reference to the old RTC library
#if possible attempt to use GPIO 20-22 buttons to configure time rather than set them on the PC first
#Weekday is start at Saturday with x01
#                     sec\min\hou\wee\day\mon\yea
#rtc.DS3231_SetTime(b'\x20\x55\x22\x04\x08\x12\x21')
#remove comment to set time. Do this only once otherwise time will be set everytime the code is executed.

pin = machine.Pin(20, machine.Pin.IN)
#temperature and humidity needs to be converted from float to non float value (unknown formula yet)
temp = sensor.temperature()
humi = sensor.humidity()

#24 hour-12 hour conversion??
#display.hour = rtc.hour % 12
#if display.hour == 0:
#  display.hour = 12
#display.pm = rtc.hour >= 12

while True:
    #Display RTC_ReadTime in mode 2 change as needed see lib/RTC_DS3231
    t = ds3231.get_time()
    #t = rtc.DS3231_ReadTime(2)
    lcd.move_to(0,0)
    lcd.putstr(t)
    time.sleep(1)
    #attempt to use maker pi's built in buttons
    if pin.value() == 0:
        lcd.clear()
        sensor.measure()
        lcd.move_to(0,0)
        lcd.putstr(int(sensor.temperature()))
        lcd.move_to(1,0)
        lcd.putstr(int(sensor.humidity()))
        time.sleep_ms(1000)
I'm not sure if this is what I think should be in the main.py as I'm still trying to think if it will work

Post Reply