011 - MakeCode MicroBit: Brickcell RTC DS3231
Table of Contents
The DS3231 is a popular real-time-clock (RTC) integrated circuit (IC) manufactured by Maxim Integrated. An RTC is a device that keeps track of time even when the main power source to a system, such as a microcontroller or a computer, is turned off or unavailable. The DS3231 is commonly used in a variety of electronic applications to provide accurate timekeeping and timestamp functionality.
High Accuracy
The DS3231 is known for its excellent timekeeping accuracy, often within a few seconds per month, making it suitable for applications where precise timekeeping is required.
Temperature Compensation
The DS3231 incorporates a temperature-compensated crystal oscillator (TCXO) that helps maintain accurate timekeeping over a wide temperature range.
Battery Backup
It has a built-in backup power supply circuit that allows it to continue running on a small coin cell battery (typically CR2032) when the primary power source is disconnected. This ensures that the RTC continues to keep time even during power outages.
I2C Interface
The DS3231 communicates with a host microcontroller or other devices through the I2C (Inter-Integrated Circuit) interface, making it easy to connect with various microcontrollers.
Alarm Functions
The DS3231 can be configured to trigger alarms at specific times or dates, making it useful for applications that require scheduled events.
32 KHz Output
It provides a 32 kHz square wave output that can be used as a clock signal for other devices.
Programmable Square-Wave Output
It can generate square wave signals with selectable frequencies (1Hz, 1.024kHz, 4.096kHz, or 8.192kHz) for various timing applications.
Low Power Consumption
It is designed to consume very low power, making it suitable for battery-powered devices.
Hardware Instruction
Let's explore how to connect the DS3231 RTC to a micro:bit microcontroller and utilize MakeCode for programming.
- Connect the DS3231 RTC GND pin to micro:bit GND pin.
- Connect the DS3231 RTC VCC pin to micro:bit VCC pin.
- Connect the DS3231 RTC serial data pin (SDA) to micro:bit pin 20.
- Connect the DS3231 RTC serial clock pin (SCL) to micro:bit pin 19.
Software Instruction
Now, let's start programming the micro:bit to utilize the DS3231 real-time-clock module.
- Login to https://makecode.microbit.org/ using your Microsoft account.
- Create a new project by clicking the "New Project" button. You may name it anything you want, for example, "rtc-ds3231-test".
- Click the "Extensions" block just under the "Math" block.
- Type https://github.com/gbantique/brickcell-rtc-ds3231/ in the search bar.
- Select the "brickcell-rtc-ds3231" from the search results. The "rtc ds3231" block should appear under the "Brickcell" block.
- Copy the code provided below.
serial.setBaudRate(BaudRate.BaudRate115200) Brickcell.setDate(2, 10, 10, 2023) Brickcell.setTime(8, 6, 0) basic.forever(function () { serial.writeLine(Brickcell.getDate()) serial.writeLine(Brickcell.getTime()) serial.writeLine("") basic.pause(2000) }) - Open a Serial Monitor such as the Termite terminal app. Set the baud rate to 115200 bps.
Or you can make a copy of my created project in your MakeCode workspace:
https://makecode.microbit.org/S07528-47249-67053-50399/
Expected Result
If you carefully follow the provided instructions above, you should be able to view the date and time on the serial terminal every 2000 milliseconds.