All of you certainly had to configure an RTC chip with the current date and time… today’s project can help to perform that task!
RTCSetup is composed by two elements:
- a GUI, written in C# and running on the PC
- a sketch running on Arduino, that is connected to the RTC
GUI and sketch communicate through a serial connection, with a simple protocol. The source for both of them are available in my Github’s repository.
Arduino and RTC
I chose to use the Adafruit’s RTClib library to talk with the DS1307 chip, that is for sure one of the most used RTC in the hobbistic world. The connection between the IC and Arduino is established using the I2C bus:
For a detailed description of the connection and the use of the library, please refer to the official tutorial.
The Arduino’s sketch receives the commands from the PC, executes them and sends back a response.
As I wrote, the communication between the GUI and the Arduino uses a simple protocol made by only 4 commands:
The first command sent after the connection is established, used by the PC to verify that a “compatible” device is connected to the serial port. Arduino must answer with !!
This command is used to obtain the version of the sketch. Arduino answers with the string defined as a constant at the top of the sketch:
#define VERSION "1.0"
This command is used to get the actual date and time, read from the RTC. Arduino answers with a string in the format dd/MM/yyyy hh:mm:ss.
This command is used to set the RTC time. Arduino answers with OK.
The GUI, developed in C#, talks with Arduino using the protocol above and implements three functionalities:
- sets the current date and time
- sets a custom date and time defined by the user
- gets and displays the actual date and time of the RTC
First, you must choose the serial port Arduino is connected to and press CONNECT. If the connection is established (commands ## and ?V) the sketch’s version is displayed in the status bar.
The first panel shows the actual time of the PC. With a click on the right button (red arrow) you can configure the RTC with that time:
With the green arrow button you can instead get the actual time of the RTC:
Finally you can, using the date-time picker on the center, choose custom date and time values and send those to the RTC:
I used the method described in this tutorial to embed a custom font in the application.
I measured the time (150ms) the sketch takes to set the time when the command is sent by the GUI: this is the reason why in the code you can see that the !T command is sent when the actual milliseconds are 850 and the time passed is the actual one + 1 second.
while (DateTime.Now.Millisecond != 850) Thread.Sleep(1); string command = "!T" + time.AddSeconds(1).ToString("ddMMyyyyHHmmss"); serialPort.WriteLine(command);