Tiny RTC DS1307 clock module complete code (Arduino) and some points needing attention

Posted by FillePille on Thu, 17 Feb 2022 11:01:54 +0100

In fact, this thing is OK, but there is no technical support. There will be holes in many details. It took me more than ten days (during which I also spent a week learning welding) to fully succeed. During this period, I was very angry. I thought I bought a piece of garbage and even wanted to throw away this 2 yuan thing. So I bought another one, and the results were the same. Then I calmed down to see what was going on. Now share the details of the experience with later novices, and experts can skip it.

I did it when I wrote this blog today. Children and old people are not deceived

 

 

I bought one in Taobao and pinduoduo, named Tiny RTC, for 2 yuan

 

The reverse side is like this. It needs manual welding. It's not very friendly

 

After welding, I worked hard for many days and finally mastered a new skill.

Let's begin to share my experience. The following points deserve attention:

1. Manual welding is required, which cannot be avoided. At least four of the five holes can be welded honestly, and the DS can not be used. However, since all four can be welded, I don't care about one more. I welded both ends (five at one end and seven at one end) at one time;

2. After welding, there will be no pit. No one will connect the four wires wrong;

3. The next step is to need a test program. The place bought for 2 yuan may not provide it. Customer service is generally robot. At this time, if there is no test program in good condition, the novice will be very confused. Whether this thing is useful or not, and I don't know where to start next. Don't panic now. Here's a complete program. I've tested it more than 100 times. In terms of hardware, I use Arduino UNO R3. Since there are only two positive and negative power lines and four SCL and SDA2 lines in total, there is no need to consider any hardware inconsistency in hardware connection. In addition, I can also provide some technical support, just leave a message. This is half done, and the next half is the code;

4. The first problem with the code may be that the statement brought by your store is wrong, and RTC should be used_ DS1307 RTC to define RTC variables instead of DS1307 RTC; (RTClib and other libraries are needed, which I will not repeat here)

5. The current time setting uses this sentence: RTC adjust(DateTime(2021, 5, 9, 15, 43, 01));  

6. The second is definitely wrong, but don't panic. The code at the end of the article also includes some of my own code to fine tune the second, but don't worry about such a small matter now, and comment / /#define hack later_ Adjust this sentence, let's adjust the exact second;

7. After the program is swiped in, the first possible problem is that the time moves, but each restart is still the original time. It's as like as two peas. It's easy to find out that the code we just brushed into is a dead time. Is it the same as when we first set up the time? It's the same as going back in time, right: P

8. Therefore, once the read-out time is successfully written, there is no need to set the time. Comment it out immediately and burn it into the program again;   //RTC.adjust(DateTime(2021, 5, 9, 15, 43, 01)); // Set initial time

9. Now there is no place in the program to modify your time. You can't modify it if you want to. Hehe, isn't there a problem that the second hand is not right at this time_ Adjust opens the function of the code I wrote and modifies the number 1 in this sentence to the appropriate value: Wire write(bin2bcd(sec + 1)); / / second level fine adjustment - + 1 can be changed to an appropriate adjustment value. Do not add too much to avoid carry, or subtraction is more reliable. This code is extracted and modified from the Wire library and RTClib library;

10. It seems to be all finished now. What else should I pay attention to? Let me think about it. Yes, be careful not to touch the 3V lithium battery (CR2032) when pulling out the cable, otherwise the time will become 2000-01-01. However, what's the panic? Just do it again. How would I know if I didn't meet you? O(∩∩) o

#include <Streaming.h>
#include <Wire. h> / / IIC Library
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>

//#define HACK_ADJUST
#ifdef HACK_ADJUST
int adjusted = 0;
static uint8_t bin2bcd(uint8_t val) {
  return val + 6 * (val / 10);
}
#endif

LiquidCrystal_I2C lcd(0x27, 16, 2); //Set LCD1602 device address 0x27
RTC_DS1307 RTC;

void setup()
{
  Wire.begin(); // Turn on the bus, which is used for I2C
  RTC.begin();  // Initialization clock
  //RTC.adjust(DateTime(2021, 5, 9, 15, 43, 01)); // Set initial time
  lcd.init();                  // Initialize LCD
  lcd.backlight();             //Set LCD backlight on
  Serial.begin(115200);
}

void loop()
{
  DateTime dateTime = RTC.now(); // Get the current time
  Serial << dateTime.year() << "-" << dateTime.month() << "-" << dateTime.day() << endl;
  Serial << dateTime.hour() << ":" << dateTime.minute() << ":" << dateTime.second() << endl;

  //Display year
  lcd.setCursor(3, 0);
  lcd.print(dateTime.year());

  //Show month
  lcd.print('-');
  if (dateTime.month() > 9)
  {
    lcd.print(dateTime.month());
  }
  else
  {
    lcd.print('0');
    lcd.print(dateTime.month());
  }

  //Display day
  lcd.print('-');
  if (dateTime.day() > 9)
  {
    lcd.print(dateTime.day());
  }
  else
  {
    lcd.print('0');
    lcd.print(dateTime.day());
  }

  //Show hours
  lcd.setCursor(4, 1);
  if (dateTime.hour() > 9)
  {
    lcd.print(dateTime.hour());
  } else
  {
    lcd.print('0');
    lcd.print(dateTime.hour());
  }

  //Show minutes
  lcd.print(':');
  if (dateTime.minute() > 9)
  {
    lcd.print(dateTime.minute());
  }
  else
  {
    lcd.print('0');
    lcd.print(dateTime.minute());
  }

  //Display seconds
  lcd.print(':');
  if (dateTime.second() > 9)
  {    
#ifdef HACK_ADJUST    //A hacking adjust by Safirst C. Ke
    int sec = dateTime.second();
    if (!adjusted)
    {
      Wire.beginTransmission(DS1307_ADDRESS);
      Wire.write((byte)0);
      Wire.write(bin2bcd(sec + 1));  //Second level fine-tuning - + 1 can be changed to an appropriate adjustment value. Do not add too much to avoid carry, or subtraction is more reliable.
      Wire.endTransmission();
      adjusted = 1;
    }  
#endif

    lcd.print(dateTime.second());
  }
  else
  {
    lcd.print('0');
    lcd.print(dateTime.second());
  }
  delay(990);
}

 

Topics: C++ Programming Linux arduino rtc