Help with Waveshare Precision RTC Module for Raspberry Pi Pico?

Hi Support,

I just bought two “Waveshare Precision RTC Module for Raspberry Pi Pico” from you and unfortunately can’t get either to work.

I suspect that I am doing something wrong, but have two questions that if answered may help me to sort this out.

*1* The units I received are slightly different from the ones picture on the Core Electronics website and also WaveShare’s website. If you hold the unit with the battery visible and below the RTC chip, there are 3 soldering points above the RTC chip in what you have pictured. The difference on the units I received is that there are an additional 2 soldering points above the 3 mentioned that appear to be for selecting which PINS to use on the Pico. The 2 soldering points appear to be bridged for I2C0, GP20 and GP21. Does this mean that I should be setting my SDA/SCL pins to 27 and 26 ?

*2* On the Core Electronics product page for this, it mentions under the Features section " * Comes with development resources and manual (Raspberry Pi Pico C/C++ and MicroPython examples)". But I can’t find any reference on that same page to where the MicroPython examples are. Have I failed to see them or are they not linked on that page ?

Other information

  • Both my Pico units are running the latest version of MicroPython (v1.18)
  • I’ve tested while no other devices are connected.
  • I’ve confirmed that it doesn’t work on two different Pico units that definitely work with several other PiicoDev units I purchased at the same time.
  • I’ve done several Google searches for help and so far can’t find anything that has led me in the right direction.

Thank You in advanced for any help you can provide.

If I figure this out myself I will post back here with the details of my solution.

3 Likes

Hi Gavin,

Welcome!!

Hmm, I havent had much experience with Waveshare parts but recall a couple previous posts of something similar happening. Electrically they should be identical.

The demos are linked on that page but the Direction to them from Waveshares site is a bit round-about.
Head to the Wiki Link on the Core web page (Also linked here) then the ‘user guides’ tab for the instructions and ‘Resources’ to download the library.

To double check that the units work/can be recognised run the following commands (this assumes that the pinout is correct

from machine import I2C
i2c = I2C(0)
i2c.scan()

The address for the DS3231 is 0x68
Lemme know how you go with the commands or if you hit any other snags.

I’ve got a Makerverse RTC that works seamlessly.

2 Likes

Hi Liam, thanks for responding and your suggestions.

Unfortunately I had already tried the example code from the WaveShare demo files in the Pico-rtc-ds3231_code.zip I downloaded from their website.

Although your suggestion to scan the I2C bus has provided me with information to suggest that both WaveShare RTC units are dead. All I get is “[]” when either is connected. When I connect devices I know are working I get numbers corresponding to the devices I have connected.

Also I happen to have another I2C RTC clock I bought from CoreElectronics several years ago for a Raspberry Pi bedside clock I built, so temporarily connected it to the Pico and it shows up when I scan the I2C bus.

I guess I will try to return them and pick up the Makerverse RTC unit instead.

Have also attached a picture of the board so anyone who reads this can see the differences I mentioned in my first post.

Thanks Again.

2 Likes

Hi Gavin,

Before jumping into messaging for a return try this code:

from machine import I2C,Pin
i2c = machine.I2C(0, scl=machine.Pin(21), sda=machine.Pin(20))
i2c.scan()
3 Likes

Hi Liam, in short I have got the RTC working. But for the benefit of all I will explain below.

First, in response to your last comment. The scan returns the number 104.
20220212-224037

So this confirmed for me that I was doing something wrong and that both WaveShare RTC units were working. Thanks again for helping me with that.

Next I search for more example code using the words “micropython ds3231 code” , this lead me to the video 030 - MicroPython TechNotes: DS3231 RTC - YouTube

In this video at 2:27, it refers to a library which can be downloaded from micropython-samples/DS3231 at master · peterhinch/micropython-samples · GitHub

Watch the next minute or so of the video as the presenter explains how to use it and also that they added an additional function to the library in order to make it easier to set the time of the RTC. Code below if you don’t feel like typing the additional code yourself :slight_smile:

# Add manually setting time.
    def set_time(self, YY, MM, DD, hh, mm, ss, wday, yday):
        self.ds3231.writeto_mem(DS3231_I2C_ADDR, 0, tobytes(dec2bcd(ss)))
        self.ds3231.writeto_mem(DS3231_I2C_ADDR, 1, tobytes(dec2bcd(mm)))
        self.ds3231.writeto_mem(DS3231_I2C_ADDR, 2, tobytes(dec2bcd(hh)))
        self.ds3231.writeto_mem(DS3231_I2C_ADDR, 3, tobytes(dec2bcd(wday +1)))
        self.ds3231.writeto_mem(DS3231_I2C_ADDR, 4, tobytes(dec2bcd(DD)))
        self.ds3231.writeto_mem(DS3231_I2C_ADDR, 5, tobytes(dec2bcd(MM)))
        self.ds3231.writeto_mem(DS3231_I2C_ADDR, 6, tobytes(dec2bcd(YY-1900)))

And to test you can use the following code in your main.py

from machine import Pin
from machine import SoftI2C
from ds3231_port import DS3231

i2c = SoftI2C(scl=Pin(21),sda=Pin(20))
ds = DS3231(i2c)

# Uncomment the next line to set the date and time, format can be found in the library.
# Do forget to comment it out again otherwise you will keep setting the time to this on every run.
#ds.set_time(2022, 02, 13, 00, 34, 00, 7, 00)

mytime = ds.get_time()       # Get Time and assign to variable
print (str(mytime))          # Print time to shell.
temp = ds.get_temperature()  # Get Temp from RTC.
print (str(temp))            # Print Temp.

Example output below
20220213-164742

Hopefully this helps anyone else who comes across this post, and please feel free to correct anything I have stated as I am a noob to MicroPython and the Pico.

Now to figure out how to make this work along side with the Pico Dev sensors :slight_smile:

3 Likes

Have you checked the batteries are good (or you’ve removed any plastic that insulates the batteries)?

Also, just checking, have you got it mounted the same way they show in the photos?

Yours matches the photos on Waveshare’s site:

Also, have you double checked you’re using the latest version of their demo code?
https://www.waveshare.com/w/upload/2/26/Pico-rtc-ds3231_code.zip

2 Likes

Question 1: Yes. See Pico pin outs.
Pico-R3-A4-Pinout.pdf (377.0 KB)

Question 2: Below is the Python code downloaded from the Waveshare Wiki web page.

#!/usr/bin/python
# -*- coding: utf-8 -*-
from machine import Pin, I2C
import time
import binascii

#    the first version use i2c1
#I2C_PORT = 1
#I2C_SDA = 6
#I2C_SCL = 7

#    the new version use i2c0,if it dont work,try to uncomment the line 14 and comment line 17
#    it should solder the R3 with 0R resistor if want to use alarm function,please refer to the Sch file on waveshare Pico-RTC-DS3231 wiki
#    https://www.waveshare.net/w/upload/0/08/Pico-RTC-DS3231_Sch.pdf
I2C_PORT = 0
I2C_SDA = 20
I2C_SCL = 21

ALARM_PIN = 3


class ds3231(object):
#            13:45:00 Mon 24 May 2021
#  the register value is the binary-coded decimal (BCD) format
#               sec min hour week day month year
    NowTime = b'\x00\x45\x13\x02\x24\x05\x21'
    w  = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
    address = 0x68
    start_reg = 0x00
    alarm1_reg = 0x07
    control_reg = 0x0e
    status_reg = 0x0f
    
    def __init__(self,i2c_port,i2c_scl,i2c_sda):
        self.bus = I2C(i2c_port,scl=Pin(i2c_scl),sda=Pin(i2c_sda))

    def set_time(self,new_time):
        hour = new_time[0] + new_time[1]
        minute = new_time[3] + new_time[4]
        second = new_time[6] + new_time[7]
        week = "0" + str(self.w.index(new_time.split(",",2)[1])+1)
        year = new_time.split(",",2)[2][2] + new_time.split(",",2)[2][3]
        month = new_time.split(",",2)[2][5] + new_time.split(",",2)[2][6]
        day = new_time.split(",",2)[2][8] + new_time.split(",",2)[2][9]
        now_time = binascii.unhexlify((second + " " + minute + " " + hour + " " + week + " " + day + " " + month + " " + year).replace(' ',''))
        #print(binascii.unhexlify((second + " " + minute + " " + hour + " " + week + " " + day + " " + month + " " + year).replace(' ','')))
        #print(self.NowTime)
        self.bus.writeto_mem(int(self.address),int(self.start_reg),now_time)
    
    def read_time(self):
        t = self.bus.readfrom_mem(int(self.address),int(self.start_reg),7)
        a = t[0]&0x7F  #second
        b = t[1]&0x7F  #minute
        c = t[2]&0x3F  #hour
        d = t[3]&0x07  #week
        e = t[4]&0x3F  #day
        f = t[5]&0x1F  #month
        print("20%x/%02x/%02x %02x:%02x:%02x %s" %(t[6],t[5],t[4],t[2],t[1],t[0],self.w[t[3]-1]))

    def set_alarm_time(self,alarm_time):
        #    init the alarm pin
        self.alarm_pin = Pin(ALARM_PIN,Pin.IN,Pin.PULL_UP)
        #    set alarm irq
        self.alarm_pin.irq(lambda pin: print("alarm1 time is up"), Pin.IRQ_FALLING)
        #    enable the alarm1 reg
        self.bus.writeto_mem(int(self.address),int(self.control_reg),b'\x05')
        #    convert to the BCD format
        hour = alarm_time[0] + alarm_time[1]
        minute = alarm_time[3] + alarm_time[4]
        second = alarm_time[6] + alarm_time[7]
        date = alarm_time.split(",",2)[2][8] + alarm_time.split(",",2)[2][9]
        now_time = binascii.unhexlify((second + " " + minute + " " + hour +  " " + date).replace(' ',''))
        #    write alarm time to alarm1 reg
        self.bus.writeto_mem(int(self.address),int(self.alarm1_reg),now_time)

if __name__ == '__main__':
    rtc = ds3231(I2C_PORT,I2C_SCL,I2C_SDA)
    rtc.set_time('13:45:50,Monday,2021-05-24')
    rtc.read_time()
    rtc.set_alarm_time('13:45:55,Monday,2021-05-24')

https://www.waveshare.com/wiki/Pico-RTC-DS3231
Select Resources tab, Click Demo Codes to download.

I would use a multimeter to confirm the connections on the board go to the pins they are supposed to and check the battery voltage and connections as @Oliver33 has stated. If thats all ok and the python code does not work then maybe the boards are faulty.

Schematic of board from the Waveshare Web site.
Pico-RTC-DS3231_Sch.pdf (180.2 KB)

Regards
Jim

2 Likes

Hi Oliver, thanks for the response.

I have indeed checked that the battery was good and that the orientation of the boards is corrected. Code was also the latest from WaveShare.

In short I have actually sorted the issue out, but my reply to Liam was hidden and is waiting for review.

I think it’s because I posted a YouTube and a GIT Hub link which helped to explain how I got it to work.

If my post doesn’t get approved I will re-post without the links.

4 Likes

Hi James, thanks for your reply.

I’ve actually managed to get the WaveShare RTC working and also have the PiicoDev sensors pulling data so I can display it all on the OLED screen.

I’m waiting for a previous post to clear before typing up another one showing exactly how I had to wire everything up and code it with MicroPython.

4 Likes

Good to hear.
The Waveshare product has potential as a RTC for the Pico. Using the same footprint makes it pretty easy to install and use. Will keep this product in mind for future Pico projects.
Cheers
Jim

3 Likes

Hi Gavin,

Glad to hear you got it working and excellent guide/troubleshooting!

We’d love to see what you’ve made, do you have any photos handy that you could send through on the forum?
Liam.

1 Like

Hi Liam,

I’m still in testing and learning mode so don’t have a final setup yet.

Once I do have a final setup I will post in the Projects or Tutorials section.

Gavin.

2 Likes