Time if times

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
deatz
Posts: 10
Joined: Mon Oct 04, 2021 8:57 pm

Re: Time if times

Post by deatz » Mon Oct 11, 2021 6:23 pm

Shew.... at least I am not going crazy....

for now this will have to do :)

if 0 <= wd <= 3 or wd == 6

hippy
Posts: 130
Joined: Sat Feb 20, 2021 2:46 pm
Location: UK

Re: Time if times

Post by hippy » Wed Oct 13, 2021 6:06 pm

pythoncoder wrote:
Sat Oct 09, 2021 9:48 am
You seem to have found a bug there. On a Pyboard, on the Unix build and on CPython the day of the week is 5 (Saturday, with 0 being Monday). On the Pico it is 4.

I have raised this issue.
If this is any help the fixes I have had working for a while now might help -

viewtopic.php?f=21&t=11108#p61066

deatz
Posts: 10
Joined: Mon Oct 04, 2021 8:57 pm

Re: Time if times

Post by deatz » Sat Oct 30, 2021 3:36 pm

Good day again,
So I have made it to the point have everything working the way I want... so long as power is not interrupted after initialization. I was going to use battery power to run the Pico but with it continually looping, the batteries only last a few days (make me think I might should have picked a simple small chip for this project), at any rate, now it will need to be plugged in, and in such I will need to install an RTC so it will restart with a current time if the power fails.

I have searched and searched and I cannot seem to find a good tutorial on the installation/setup of a DS3231 unit with the Pico. I have found a good one for a regular Pi but, in my mind at least, it isn't translating. The only decent one for a Pico I've found so far has such poor English I cannot follow it and there is no documentation.

Any help or direction would be much appreciated.

Thank you,
DeatZ

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Time if times

Post by scruss » Sun Oct 31, 2021 2:17 am

deatz wrote:
Sat Oct 30, 2021 3:36 pm
I have searched and searched and I cannot seem to find a good tutorial on the installation/setup of a DS3231 unit with the Pico. I have found a good one for a regular Pi but, in my mind at least, it isn't translating. The only decent one for a Pico I've found so far has such poor English I cannot follow it and there is no documentation.
So what have you tried? What DS3231 module have you got?

With your DS3231 wired with SCL going to GP17 and SDA going to GP16, can you at least get this in the REPL?

Code: Select all

MicroPython v1.17 on 2021-09-02; Raspberry Pi Pico with RP2040
Type "help()" for more information.
>>> from machine import Pin, I2C
>>> i2c=I2C(0, scl=Pin(17), sda=Pin(16))
>>> i2c.scan()
[104]
If you get the [104], then you've found a DS3231.

Peter Hinch's ds3231_port.py can set and read a DS3231 from the system clock.

deatz
Posts: 10
Joined: Mon Oct 04, 2021 8:57 pm

Re: Time if times

Post by deatz » Sun Nov 14, 2021 9:55 pm

I get [87, 104]

I loaded up the ds3231_port.py code and it seems to run through, doesn't throw any errors but also doesn't remember the time when I unplug it and then plug it in to non-pc power.

I can't seem to work my way through the code in ds3231_port.py to know what I should be getting. For instance when I try to call get_time it says it's not set, etc. I think I am just too far behind the curve. I've tried to get some basics of i2c and see how they apply to Micro Python but mist of what I find is either on pic chips or Raspberry pi, not the Pico.

I'm working on two super simple projects one just Python and one with the Pico that are frustrating me to no end. I need to find a foundational course or something, I've YouTubed myself to death and they are either too simple, too complex, or unrelated...

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Time if times

Post by scruss » Mon Nov 15, 2021 8:04 pm

MicroPython can be a bit confusing, yes.

This is what I use to set up/test DS3231 clock boards:

Code: Select all

# ds3231_port_test
# Test/demo of portable driver for DS3231 precision RTC chip
# pro micro rp2040, ds3231 on qwiic port
# cut down a bunch - scruss, 2021,11

# (original) Author: Peter Hinch
# Copyright Peter Hinch 2018 Released under the MIT license

from machine import Pin, I2C
import utime
from ds3231_port import DS3231
i2c=I2C(0, scl=Pin(17), sda=Pin(16))
ds3231 = DS3231(i2c)

print('Initial values')
print('DS3231 time:', ds3231.get_time())
print('RTC time:   ', utime.localtime())

print('Setting DS3231 from RTC')
ds3231.save_time()  # Set DS3231 from RTC
print('DS3231 time:', ds3231.get_time())
print('RTC time:   ', utime.localtime())
Looks like there may be an I2C FRAM chip on your board, too, maybe.

deatz
Posts: 10
Joined: Mon Oct 04, 2021 8:57 pm

Re: Time if times

Post by deatz » Tue Nov 16, 2021 1:31 am

I'll push this through tomorrow and see what I come up with.

This will set the time on the pico and when it is plugged back in, it will use the set time when utime.local() is called?

Thanks again for your patience and all your help!
DeatZ

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Time if times

Post by scruss » Tue Nov 16, 2021 1:53 am

It sets the time from the Pico's clock, which if you're using Thonny and perhaps rshell, will be preset to the right time

deatz
Posts: 10
Joined: Mon Oct 04, 2021 8:57 pm

Re: Time if times

Post by deatz » Tue Nov 16, 2021 3:01 am

I am using Thonny, though it's become a bit quirky with the last update. At any rate. just run this once and then after it is unplugged and replugged with another source it'll have it's own correct time?

Everything works great as long as it stays plugged into a PC, but once I pull it and give it just a power source it forgets what the time is.

I'll report back.

Thanks,
DeatZ

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: Time if times

Post by karfas » Tue Nov 16, 2021 7:22 am

1) Are you sure the DS3231 stays powered on when you switch to battery ?
My DS3231 modules come with a coin-cell battery holder...
2) Show us your code.
After startup, you need to call ds3231.get_time(true) or something similar to set your system time.
scruss' code above sets the DS3231 to system time, not the other way around.
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

Post Reply