Synchronise DS3231 RTC With PC Via Arduino
Intro: Synchronise DS3231 RTC With PC Via Arduino
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.
16 Comments
brodaengraver 2 years ago
Can you tell me how to "run" this batch file. DonĀ“t know where to start it.
Best Regards
Broda
ana.karabelj 3 years ago
is it perhaps possible in some way to include the configuration of the day of the week?
MarvinAndroid 3 years ago
vanadiumoxide 7 years ago
It says the DTR circuit is off
dancopy 4 years ago
dancopy 4 years ago
CaesarO 5 years ago
I ran it as instructed, everything is synced except for the first two digits of the year. Instead of 2018, it said 1918.
TimN33 7 years ago
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!
TimN33 7 years ago
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..
set com=%1
mode %com% BAUD=9600 PARITY=n DATA=8
@echo off
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set hours=%ldt:~8,2%
set minutes=%ldt:~10,2%
set /A seconds=%ldt:~12,2%+6
set miliSeconds=%ldt:~15,3%
:loop
if %TIME% LSS %hours%:%minutes%:%seconds%.00 goto loop
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
rem next line adds 1 second, CREATES NEW VARIABLE
set /a "newsec=%ldt:~12,2% +1"
echo S%newsec%, >%com%
echo D%ldt:~10,2%, >%com%
echo H%ldt:~8,2%, >%com%
echo %newsec%
echo S%ldt:~12,2%,
echo D%ldt:~10,2%,
echo H%ldt:~8,2%,
set /A seconds=%ldt:~12,2%+6
:loop2
if %TIME% LSS %hours%:%minutes%:%seconds%.00 goto loop2
echo T%ldt:~6,2%, >%com%
echo M%ldt:~4,2%, >%com%
echo J%ldt:~0,4%, >%com%
echo T%ldt:~6,2%,
echo M%ldt:~4,2%,
echo J%ldt:~0,4%,
mennotjer 7 years ago
First of all thanks for your work, now my RTC is synced!!
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.
mode COM11 BAUD=9600 PARITY=n DATA=8
@echo off
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set hours=%ldt:~8,2%
set minutes=%ldt:~10,2%
set /A seconds=%ldt:~12,2%+2
set miliSeconds=%ldt:~15,3%
:loop
if "%TIME%" LSS "%hours%:%minutes%:%seconds%,00" goto loop
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set /p x="S%ldt:~12,2%,D%ldt:~10,2%,H%ldt:~8,2%," <nul >\\.\COM11
echo %TIME%
echo S%ldt:~12,2%,
echo D%ldt:~10,2%,
echo H%ldt:~8,2%,
set /A seconds=%ldt:~12,2%+4
:loop2
if "%TIME%" LSS "%hours%:%minutes%:%seconds%,00" goto loop2
set /p x="T%ldt:~6,2%,M%ldt:~4,2%,J%ldt:~0,4%," <nul >\\.\COM11
echo %TIME%
echo T%ldt:~6,2%,
echo M%ldt:~4,2%,
echo J%ldt:~0,4%,
pause
yosoufe 7 years ago
Thanks :)
nebloid 7 years ago
Worked well. Thanks for share.
BfromSLO 8 years ago
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.
yosoufe 8 years ago
HectorG18 8 years ago
Excelent program works perfect Thanks from Guatemala!!!
yosoufe 8 years ago
Thanks alot. I am happy to see it can help!