I wanted to make a data logger for a project with an Arduino and I also needed the real time. So I used DS3231RTC (real time clock). I wanted to synchronise this RTC with my PC. I searched for some codes to send the PC time to Arduino and I found something and I want to share it as my first instructable.

Step 1: Make Arduino Ready

First you have to upload this program to your arduino. This program just receives the time from PC with special format and write it on the RTC.

To see the current time of the RTC you need to write "B," on the serial. (without quotation mark)

For more information visit this website.


Step 2: Send the Time From PC to Arduino

To send the time from PC, You just need to run this batch file. before that right click on it and Edit it. You need to change your arduino COM port. Mine was COM3. So change the COM number in the batch file. Do not forget, your PC's serial should be free. Maybe your Serial communication of your arduino IDE is open. Please close it first and then run the batch file.

This batch file is not at all perfect. It was the second time I wrote a batch file. So sometimes you have to run it twice. If you see the above picture as a result, including the time and date with some prefixes, that means the batch file is run completely.

Step 3: Results

Now if you write "B," on the Arduino's Serial. You will see the same time as your PC.

<p>In the previous picture you can see a Raspberry Pi header attached, this is because I also use the same setup for a Pi too!</p>
<p>I have it running some software I wrote that used an interrupt (every second) and it captures the temperature from a sensor and logs the high, low and current and displays them (as well as logging to SD card) it also displays the current time and date as well as the Zule (GMT) time</p>
<p>I added the com port as an argument and also added 1 second to the seconds to more accurately sync. The adding of a numerical value to a variable is a bit tricky..</p><p>set com=%1<br>mode %com% BAUD=9600 PARITY=n DATA=8<br>@echo off<br>for /F &quot;usebackq tokens=1,2 delims==&quot; %%i in (`wmic os get LocalDateTime /VALUE 2^&gt;NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j<br>set hours=%ldt:~8,2%<br>set minutes=%ldt:~10,2%<br>set /A seconds=%ldt:~12,2%+6<br>set miliSeconds=%ldt:~15,3%<br>:loop<br>if %TIME% LSS %hours%:%minutes%:%seconds%.00 goto loop<br>for /F &quot;usebackq tokens=1,2 delims==&quot; %%i in (`wmic os get LocalDateTime /VALUE 2^&gt;NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j</p><p>rem next line adds 1 second, CREATES NEW VARIABLE<br>set /a &quot;newsec=%ldt:~12,2% +1&quot;<br><br>echo S%newsec%, &gt;%com%<br>echo D%ldt:~10,2%, &gt;%com%<br>echo H%ldt:~8,2%, &gt;%com%<br><br>echo %newsec%<br>echo S%ldt:~12,2%, <br>echo D%ldt:~10,2%,<br>echo H%ldt:~8,2%,<br>set /A seconds=%ldt:~12,2%+6<br>:loop2<br>if %TIME% LSS %hours%:%minutes%:%seconds%.00 goto loop2<br><br>echo T%ldt:~6,2%, &gt;%com%<br>echo M%ldt:~4,2%, &gt;%com%<br>echo J%ldt:~0,4%, &gt;%com%<br><br>echo T%ldt:~6,2%,<br>echo M%ldt:~4,2%,<br>echo J%ldt:~0,4%,</p>
<p>First of all thanks for your work, now my RTC is synced!!</p><p>I had some problems running the bat file, somehow it gave an error when the if statement was executed. I managed to fix it. For the people who have the same error this is my bat file. Note you need to replace COM11 with your own COM port number.<br><br>mode COM11 BAUD=9600 PARITY=n DATA=8</p><p>@echo off</p><p>for /F &quot;usebackq tokens=1,2 delims==&quot; %%i in (`wmic os get LocalDateTime /VALUE 2^&gt;NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j</p><p>set hours=%ldt:~8,2%</p><p>set minutes=%ldt:~10,2%</p><p>set /A seconds=%ldt:~12,2%+2</p><p>set miliSeconds=%ldt:~15,3%</p><p>:loop</p><p>if &quot;%TIME%&quot; LSS &quot;%hours%:%minutes%:%seconds%,00&quot; goto loop</p><p>for /F &quot;usebackq tokens=1,2 delims==&quot; %%i in (`wmic os get LocalDateTime /VALUE 2^&gt;NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j</p><p>set /p x=&quot;S%ldt:~12,2%,D%ldt:~10,2%,H%ldt:~8,2%,&quot; &lt;nul &gt;\\.\COM11</p><p>echo %TIME%</p><p>echo S%ldt:~12,2%, </p><p>echo D%ldt:~10,2%,</p><p>echo H%ldt:~8,2%,</p><p>set /A seconds=%ldt:~12,2%+4</p><p>:loop2</p><p>if &quot;%TIME%&quot; LSS &quot;%hours%:%minutes%:%seconds%,00&quot; goto loop2</p><p>set /p x=&quot;T%ldt:~6,2%,M%ldt:~4,2%,J%ldt:~0,4%,&quot; &lt;nul &gt;\\.\COM11</p><p>echo %TIME%</p><p>echo T%ldt:~6,2%,</p><p>echo M%ldt:~4,2%,</p><p>echo J%ldt:~0,4%,</p><p>pause</p>
<p>Thanks :)</p>
<p>It says the DTR circuit is off</p>
<p>Worked well. Thanks for share.</p>
<p>It works, except that I seem to get the wrong day...</p>
<p>the day should be implemented manually in the code.</p>
<p>It works, except that I seem to get the wrong day...</p>
<p>I get the wrong time and wrong day of the week showing in serial monitor. the day/month/year shows correctly. What could cause this? I used the two files here as they were.</p>
I really do not know. Run it more than once. Try to debug.<br>
<p>Excelent program works perfect Thanks from Guatemala!!!</p>
<p>Thanks alot. I am happy to see it can help!</p>

About This Instructable




More by yosoufe:Play Youtube Playlist as ScreenSaver (Windows 7) Synchronise DS3231 RTC with PC via Arduino 
Add instructable to: