Page 1
Welcome! Thank you for purchasing our AZ-Delivery DS3231 Real Time Clock Module. On the following pages, you will be introduced to how to use and set up this handy device. Have fun!
Page 2
Areas of application Education and teaching: Use in schools, universities and training institutions to teach the basics of electronics, programming and embedded systems. Research and development: Use in research and development projects to create prototypes and experiments in the fields of electronics and computer science. Prototype development: Use in the development and testing of new electronic circuits and devices.
Page 3
consult a doctor. Caution: Keep the product out of the reach of children and pets to avoid accidental contact and swallowing of small parts. Note: Store the product in a safe, closed container when not in use. Attention: Avoid contact of the product with food and drinks.
Table of Contents Introduction....................3 Specifications....................5 The pinout.....................6 How to set-up Arduino IDE................7 How to set-up the Raspberry Pi and Python..........11 Connecting the module with Uno..............12 Sketch example..................14 Connecting the module with Raspberry Pi..........20 Enabling the I2C interface...............21 Libraries and tools for Python..............23 Python script....................24 - 2 -...
Introduction The DS3231 Real Time Clock module is used as time synchronization device in applications where precise timings are essential. The module is used in digital clocks, computer motherboards, digital cameras, embedded systems etc. It is a real time clock with an integrated temperature-compensated crystal oscillator.
Page 6
The internal clock can provide seconds, minutes, hours, day, date, month, and year information. The date at the end of the month is automatically adjusted for months which have less than 31 days. It also includes corrections for leap years. The module has an I2C interface with I2C serial address, and it can be connected alongside with other devices on the same I2C lines.
Dimensions 34 x 23 x 18mm [1.3 x 09 x 07in] The module consists of a DS3231 RTC Clock chip and Atmel AT24C32 EEPROM chip. The AT24C32 has memory storage capacity of 32kB and uses the I2C bus interface with 0x57 address which can be modified. It has a capability of setting the time and date, checking and clearing alarms and logging data with a timestamp.
The pinout The DS3231 RTC module has a six pins on one side and additional four, for power supply and I2C interface lines on the other side. The pinout is shown on the following image: The DS3231 RTC module safely operates in 3.3V Voltage.
How to set-up Arduino IDE If the Arduino IDE is not installed, follow the link and download the installation file for the operating system of choice. For Windows users, double click on the downloaded .exe file and follow the instructions in the installation window. - 7 -...
Page 10
For Linux users, download a file with the extension .tar.xz, which has to be extracted. When it is extracted, go to the extracted directory and open the terminal in that directory. Two .sh scripts have to be executed, the first called arduino-linux-setup.sh and the second called install.sh.
Page 11
Almost all operating systems come with a text editor preinstalled (for example, Windows comes with Notepad, Linux Ubuntu comes with Gedit, Linux Raspbian comes with Leafpad, etc.). All of these text editors are perfectly fine for the purpose of the eBook. Next thing is to check if your PC can detect an Arduino board.
Page 12
If the Arduino IDE is used on Windows, port names are as follows: For Linux users, for example port name is /dev/ttyUSBx, where x represents integer number between 0 and 9. - 10 -...
How to set-up the Raspberry Pi and Python For the Raspberry Pi, first the operating system has to be installed, then everything has to be set-up so that it can be used in the Headless mode. The Headless mode enables remote connection to the Raspberry Pi, without the need for a PC screen Monitor, mouse or keyboard.
Connecting the module with Microcontroller Compatible with Arduino Connect the DS3231 RTC module with the Microcontroller compatible with Arduino as shown on the following connection diagram: Module pin MC pin Wire color Green wire Blue wire 3.3V Red wire Black wire...
Page 15
RTClib made by Adafruit, as shown in the following image: With the library comes several sketch examples, to open one, go to: File > Examples > RTClib > ds3231 With this sketch example the module can be tested. The sketch in this eBook is modified version of this sketch, to get more user-friendly output.
Sketch example #include <Wire.h> #include "RTClib.h" RTC_DS3231 rtc; char daysOfTheWeek[7][12] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" void setup () { Serial.begin(9600); delay(2000); rtc.begin(); rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); /* To manualy set date and time, remove the coment // signs and enter new values in the followingline in this sequence: year, day, month, hour, minute and second.*/ //rtc.adjust(DateTime(2020, 2, 24, 10, 00, 0));...
Page 19
Upload the sketch to the Uno and open Serial Monitor: (Tools > Serial Monitor). The result should look like the output on the following image: - 17 -...
Page 20
At the beginning of the sketch two libraries called Wire and RTClib are imported. These libraries are used to import functions that can be used for communication between the module and Uno. Next, an object called RTC_DS3231 is created with the following line of code: RTC_DS3231 rtc;...
Page 21
When a specific time and date has to be set, it can be done with adjust() and DateTime() functions. For example, if date is to be set to: 09th March 2020. And time to 10:00:00, following line of code can be used: rtc.adjust(DateTime(2020, 3, 09, 10, 00, 0));...
Connecting the module with Raspberry Pi Connect the DS3231 RTC module with the Raspberry Pi as shown on the following connection diagram: Microcontroller pin RTC pin Physical pin Wire color Red wire Blue wire GPIO2 Black wire Green wire GPIO3...
Enabling the I2C interface In order to use the module with Raspberry Pi, I2C interface of the Raspberry Pi has to be enabled. Open following menu: Application Menu > Preferences > Raspberry Pi Configuration In the new window, under the tab Interfaces, enable the I2C radio button, as on the following image: - 21 -...
Page 24
To detect I2C address of the module, i2ctools should be installed. If It is not installed, open the terminal and execute the following command: sudo apt-get install i2ctools -y Checking the RTC module I2C address is done by executing the following command in the terminal: i2cdetect -y 1 The output should look like on the following image:...
To do so, run the following commands in the terminal: sudo apt-get update sudo apt-get install -y python-smbus git External library script can be downloaded with following command: git clone https://github.com/Slaveche90/az-delivery-ds3231.git After downloading the library, the script rtc_lib.py can be found in the following directory: /home/pi/az-delivery-ds3231...
0x68) ds3231.write_now() # saves the current date and time of R. Pi # ds3231.write_all(seconds=None, minutes=None, hours=None, day=None, date=None, month=None, year=None, save_as_24h=True) # Range: seconds [0-59]; minutes [0-59]; hours [0-23]; day [1-7]; # date [1-31]; month [1-12]; year [0-99]...
Page 27
CTRL + C to end the script!]') try: while True: print('\nSystem time: {}'.format( time.strftime('%Y-%m-%d %H:%M:%S'))) data ds3231.read_datetime() # return tuple print('RTC date: {}.{}.{}'.format(data[0], data[1], check(data[2]), check(data[3]))) print('RTC time: {}:{}:{}'.format(check(data[4]), check(data[5]), check(data[6]))) # return string print('RTC date_time: {}'.format(ds3231.read_str())) print('Temperature: {:.1f}C'.format(ds3231.getTemp(), degree_sign)) time.sleep(1)
Page 28
Save the script by the name rtc.py in the same directory where the rtc_lib.py script is saved. To run the script, open the terminal in the directory where the script is saved and run the following command: python3 rtc.py The result should look like as on the following image: To stop the script press 'CTRL + C' on the keyboard.
Page 29
Next, the variable called degree_sign is created. The value of this variable represents the UTF8 symbol for degree sign. Then, the object called ds3231 is created with the following line of code: ds3231 = rtc_lib.SDL-DS3231(1, 0x68) Where the number 0x68 represents the I2C address of the RTC module.
Page 30
The function called write_all() has eight arguments and returns no value. The arguments represent part of data for date and time. The values for all arguments are integer numbers in the ranges: Seconds: 0 - 59, Minutes: 0 - 59, Hours: 0 - 23 Day: 1 - 7 (day in week), Date: 1 - 31, Month: 1 12, Year: 0 - 99 Save_as_24h: True/False (tested with only as True) Next, a new function is created, called check().
Page 31
The function read_datetime() has two arguments and returns a tuple. The first argument represents the century, an integer value, for example: for 21st century, use century=21. The second argument represents time zone info, used for datetime objects (it is not covered in this eBook). The return value is a tuple which has seven elements.
Page 32
Internet. If you are looking for the high quality products for Arduino and Raspberry Pi, AZ-Delivery Vertriebs GmbH is the right company to get them from. You will be provided with numerous application examples, full installation guides, eBooks, libraries and assistance from our technical experts.
Need help?
Do you have a question about the DS3231 and is the answer not in the manual?
Questions and answers