RTC-Nano

From Wiki
Jump to: navigation, search

Introduction

MG 3067-1.jpg
This mini-size Nano module is integrated with the PCF8563 RTC and ds18b20 temperature sensor. Being compatible with Raspberry Pi and Arduino, and you can just plug the 5-pin female header into the Raspberry Pi to use. Besides, it has low power consumption, and provides battery backup for the continuous timing.

How to Use RTC-Nano on Raspberry Pi

Connect the RTC-nano module and Raspberry Pi:
Rtc raspberry.png
Note: The pin marked with white triangle beside is GND; pay attention when plugging the module into the Raspberry Pi. DO NOT connect them reversely, or the module will be damaged.

Configuration

To use RTC-nano module on Raspberry Pi, you need to complete some configuration. Here two ways are provided: auto-configuration by script and manual configuration.
Auto-configuration:
1) Log into the Raspberry Pi, open a terminal.
2) Download the package by github:

git clone https://github.com/sunfounder/SunFounder_RTC_Nano.git 

3) Get into the code directory to install:

cd SunFounder_RTC_Nano 
sudo ./install 	

SEEM.png
Prompts in installation:
You'll be prompted about whether to sync the time on the Raspberry Pi to the RTC or not; if you type in no, you can set it manually.
In manual setting, type in the date and then time, and the setting value will be saved as the time on the RTC module.

When the setting is done, you will see a reboot prompt, type in yes.
Manual Configuration:
1) Modify the /boot/config.txt to increase the device tree

sudo nano /boot/config.txt 

Get to the end of the file with the PageDown (PgDn) key, and type in these:

dtparam=i2c_arm=on
dtoverlay=i2c-rtc,pcf8563
dtoverlay=w1-gpio

Press Ctrl + x to exit, and y to save.
2) Remove the fake-hwclock and use the ‘real’ hwclock instead.

sudo apt-get -y remove fake-hwclock 
sudo update-rc.d -f fake-hwclock remove 

3) Modify /etc/rc.local to add the time sync at booting.

sudo nano /etc/rc.local 

Add the following contents to the line above exit 0:

sudo hwclock -s

Then the configuration is done.
4) Install an RTC module-supporting Python library.

sudo python setup.py install 

After the library installation is done, we can import this in Python programming. For the library’s API and documentation, please refer to the Appendix.

How to Use

Check whether the device is ready
1. Check whether the ds18B20 temperature device is recognized:

ls /sys/bus/w1/devices/ 

LS-sys.png
If you see a device name starting with 28-, that’s the ds18B20. If a prompt of an error and no file or folder found appears, it indicates that the w1-bus may be disabled. If no error is shown but still no "28-"-named device is found, the sensor on ds18b20 may be broken.
2. Check whether the i2c-RTC time device is recognized:

i2cdetect -y 1 

I2CDETECT.png
If you can see an i2c address, the connection to the i2c-RTC works fine.

Check the device’s data
Check the temperature data of the ds18b20 device:

sudo cat /sys/bus/w1/devices/note: here type in your device name/w1_slave 

Sudo-cat.png
The first line is data check, and the second, t = 30000 is the temperature value in integer: divide it by 1000, and you will get 30.000 in Celsius degree - the present temperature data detected by the ds18B20.

Check the time saved on the RTC device:

sudo hwclock -r 

Hwclock.png
Set the time on the RTC:

sudo hwclock --set –date="yyyy/MM/dd HH:mm:ss"

Hwclosk-set.png
Sync the time from the Raspberry Pi to the RTC device:

sudo hwclock -s 

Hwclock-s.png

How to Use RTC-Nano on Arduino

The wiring of the RTC-Nano module and Arduino:
Artc-nano-arduino.jpg
Note: The pin marked with white triangle is GND. To connect the female headers on the module, you need to use some male-to-male Dupont wires. Here please notice the anode and cathode headers’ connection. DO NOT connect them reversely, or the module will be damaged.

Library Installation

Download the RTC-Nano_Arduino_Code.zip.
Open the Arduino IDE (go to the Arduino.cc to download if you hasn't done yet), select Sketch < Include Library < Add .ZIP Library
Sketch.png
Select Arduino-Temperature-Control.zip, then click to Open.
Pcf8563.png
“Library added to your libraries. Check ”Include library” menu” will appear.
Add another two libraries from the libraries folder in the same way.
Of course, you can install the three libraries from Arduino IDE: select Sketch -> Include Library-> Manage Libraries, search them out and Install them.

Using the Sample Files

Here we provide two sample files: one is to set the RTC’s date and time, and the other is to print the date and time stored in the module, and the detected temperature by the RTC. 1213ss.png
Sample 1-readDate_Time_temperature

The code is to: print the date and time stored in RTC-Nano module, as well as the detected temperature.
Take these steps:
Connect the Arduino board to the computer.
Open the file readDate_Time_temperature.ino.
Select the corresponding Board and Port (check through Device Manager->Ports), and click Upload to burn the test code to your Arduino board.
Then open Serial Monitor, and select baud rate 9600.
Rtcddf.png
Then the data will be printed on the window.
The data printed is the date and time stored in module, and the temperature detected by ds18b20 in Celsius degree.
The string in hexadecimal format at the first line is the address of the device ds18b20.
Read Date and Time:
In the code, the void printDate() function is to print the date and time.

void printDate(){
 Serial.print(rtc.formatDate());
 Serial.print("   ");
 Serial.print(rtc.formatTime());
 Serial.println("   ");
}

We need to call the library and initialize the RTC before applying this function to acquire the data of time and date:

 #include <Wire.h>
 #include <Rtc_Pcf8563.h>
 //init the real time clock
 Rtc_Pcf8563 rtc;

Open the serial port In setup():

void setup()
{
  Serial.begin(9600);
}
void loop(){
  printDate();
  delay(500);
}

Read Temperature: The function void printTemperature (DeviceAddress deviceAddress) is to print the temperature information.

void printTemperature(DeviceAddress deviceAddress){
  // Send the command to get temperatures
  sensors.requestTemperatures();

  float tempC = sensors.getTempC(deviceAddress);
  Serial.print("Temperature: ");
  Serial.print(tempC);
  Serial.print(" ");
  Serial.println('C');
  // print Fahrenheit
  //Serial.print(" Temp F: ");
  //Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
}

We need to complete some initialization before applying this function to print temperature information:

#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// arrays to hold device address
DeviceAddress insideThermometer;

Open the serial port and start up the library in setup():

void setup()
{
  Serial.begin(9600);
  // Start up the library
  sensors.begin();
  // deviceInfo();
  sensors.getAddress(insideThermometer, 0);
}
void loop(){
  printTemperature(insideThermometer);
  delay(500);
}

Sample 2-setDate_Time

The code is to: set date and time on the RTC module.
Take these steps:
Connect the Arduino board to the computer.
Open the file setDate_Time.ino.
Select the corresponding Board and Port, and click Upload to burn the test code to your Arduino board.
Then open Serial Monitor, and select baud rate 9600.
Setdate.png
Note: Set No line ending beside the baud rate, meaning no line ending in the sent data.
Then it will remind you to set the date and time. Enter the values in the input boxes, then press Enter to save or click the Send button.
Only when you type in a valid number, it will jump to the next setting. Or it will prompt you that the input number is invalid and please try again. When all the settings are done, the values will be written to the RTC-Nano module.
Then the time and date information will be printed on the Serial Monitor once per second.
Set the RTC’s date and time:
This module applies the function void setDate() to set date, and void setTime() to set time. The while() loop controls moving to the next setting only after this part is done. The self-defined functions like setYear(), setMonth(), and setDay() etc., will return 1 if the setting is successful; and they'll return -1 if fails.

void setDate(){
  while(setYear()<0);
  while(setMonth()<0);
  while(setDay()<0);
  while(setWeekday()<0);
  rtc.setDate(rtcDate[0],rtcDate[1],rtcDate[2],rtcDate[3],rtcDate[4]);
}

void setTime(){
  while(setHour()<0);
  while(setMinute()<0);
  while(setSecond()<0);
  rtc.setTime(rtcTime[0],rtcTime[1],rtcTime[2]);
}

The RTC need to be initialized before the functions write data to the RTC:

#include <Wire.h>
#include <Rtc_Pcf8563.h>

//init the real time clock
Rtc_Pcf8563 rtc;
//set a time to start with.
//day, weekday, month, century(1=1900, 0=2000), year(0-99)
int rtcDate[5] = {1, 6, 4, 0, 17};
//hr, min, sec
int rtcTime[3] = {10,10,10};

Open the serial port in setup(), initialize the date and time information saved on the RTC:

void setup() {
  Serial.begin(9600);
  //clear out the registers
  rtc.initClock();

  setDate();
  setTime();
  Serial.println("End");
}

Here the self-defined functions like setYear(), setMonth(), and setDay() are to receive the serial input and check whether the setting values fit the common sense. For example, if the month input is larger than 12, it should prompt an error and ask you to input again; the value in setDay() should comply with the month set in setMonth(), and when the month is February, the days should be 28 in non-leap years while 29 in leap yearsS.

int setMonth(){
  Serial.print("input month(1~12): ");
  int inInt = readInput();

  if (inInt<1 or inInt>12){
    Serial.print("  [error] input month not appropriate,input again(1~12), Error value: ");
    Serial.println(inInt);
    return -1;
  }
  else{
    rtcDate[2] = inInt;
    //Serial.println(rtcDate[2]);
    //Serial.println("    Set month success");
    return 1;
  }

int setDay(){
  int month1[7] = {1,3,5,7,8,10,12};
  int month2[5] = {2,4,6,9,11};

  Serial.print("input day(1~31): ");
  int inInt = readInput();

  if (inInt>31){
    Serial.println("  [Error] input day not appropriate,input again(1~31)");
    return -1;
  }
  else{
    rtcDate[0] = inInt;
    if(rtcDate[0]>30){
      // when the input days>30, judge whether the input month is leap month.
      for(int i=0;i<5;i++){
        if(rtcDate[2]=month2[i])
          {rtcDate[0]=30;}
      }
      // if the input month is 2, check it’s in leap year.
      if(rtcDate[2]==2){
        if(rtcDate[0]>28){
          if(rtcDate[4]%4==0)
            {rtcDate[0]=29;}
          else
            {rtcDate[0]=28;}
        }
      }
    }
    //Serial.println(rtcDate[0]);
    //Serial.println("    Set day success");
    return 1;
  }
}

Resource

RTC-Nano_Arduino_CodeZIP.jpg
DS18B20 datasheetPDF.jpg
PCF8563 datasheetPDF.jpg