Getting error about date in Sensor Kit v2 - in ds1302.py

I am getting this error when try to run lesson 33:

Traceback (most recent call last):
  File "/home/piboy/SunFounder_SensorKit_for_RPi2/Python/33_ds1302.py", line 58, in <module>
    setup()
  File "/home/piboy/SunFounder_SensorKit_for_RPi2/Python/33_ds1302.py", line 31, in setup
    print (rtc.get_datetime())
  File "/home/piboy/SunFounder_SensorKit_for_RPi2/Python/ds1302.py", line 229, in get_datetime
    return datetime(year, month, day, hour, minute, second)
ValueError: hour must be in 0..23

If I put a print in there, the hour ends up as 89?

the hour value seems to be computed this way - is this code doing the right thing or is the module not wired up properly perhaps?

    216         Byte = self.read_byte()
    217         hour = (Byte % 16) + int(Byte / 16) * 10

This issue will take time to test and reproduce, will update you with more information.

1 Like

If you are running a Python script.
When prompted to set the date and time, follow the format:
Set the year, month, and day to YYYY MM DD

Set the hours, minutes, and seconds to HH MM SS (24-hour system)

Input to be space. For example, if I want to input March 12, 2024, the correct input is: 2024 03 12
For example, if I want to enter March 12, 2024, the correct entry would be: 2024 03 12, and the correct entry would be: 18 hours, 32 minutes and 22 seconds.

If you are running a C code script, you do not need to enter the value with spaces, just enter it continuously.

Sorry, to be clear - I never get to the point where it prompts for a date. It fails prior to that. Line 31 can be seen here - calling rtc.get_datetime().

Iā€™m very sorry, we went and did a test and the error you are experiencing did not occur.
What version of Raspberry Pi are you currently using. Please let us know so we can reproduce the problem.
Did you get any errors during the installation of the product source code. If so, please provide us with a screenshot of the error message.
According to the error message, the problem occurs when calling the rtc.get_datetime() method. Specifically, the error states that the hour value must be between 0 and 23. If you enter a value outside this range, an error is reported.

  1. Ensure that the power supply to the DS1302 module is properly connected, providing a stable 5V voltage.
  2. Check that the pin connections between the DS1302 module and the Raspberry Pi are correct. Make sure the SCL pin is connected to GPIO23, the I/O pin is connected to GPIO24, and the RST pin is connected to GPIO25.