ESP 8266/NodeMCU interfacing with DS1307 RTC Module
NodeMCU interfacing with DS1307 RTC module
In this tutorial, we will know about Real Time Clock (RTC) module (DS1307 variant) and try to set and get time from the module. RTC modules are used to maintain clock on a device. It has a slot for battery which keep the clock ticking even if external power source is off. In the experiment i have placed CR2032 (3V) Lithium battery to keep clock running.
DS1307 module has several pins of these we will use Serial Clock Line (SCL) and Serial Data Line(SDA) for i2c communication. Vcc and GND are for powering the module.
Hardware requirements
1. NodeMCU
2. DS1307 Module
3. 3V CR2032 Lithium Battery
4. 4 Jumper wires
NodeMCU | DS1307 |
---|---|
3V | Vcc |
GND | GND |
D1 | SCL |
D2 | SDA |
Install RTCLib library. Goto Sketch -> Include Library -> Manage Library. Search for RTClib
Sketch
#include <ESP8266WiFi.h> #include <ESP8266WebServer.h> #include <Wire.h> #include "RTClib.h" #include "time.h" const char* ssid = "Sensorism"; // Enter Wifi network name const char* password = "ESP01RTCRead"; //Enter password for network IPAddress local_ip(192, 168, 1, 5); // IP to access device IPAddress gateway(192, 168, 1, 1); IPAddress subnet(255, 255, 255, 0); ESP8266WebServer server(80); RTC_DS1307 RTC; void setup() { WiFi.softAP(ssid, password); WiFi.softAPConfig(local_ip, gateway, subnet); delay(2000); server.on("/", handleOnConnect); server.on("/setTime", setTime); server.begin(); Wire.begin(); RTC.begin(); } void loop() { server.handleClient(); } void handleOnConnect() { DateTime now = RTC.now(); char timeBuffer[20]; sprintf(timeBuffer, "%02d:%02d:%02d %02d/%02d/%02d", now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year()); server.send(200, "text/plain", timeBuffer); } void setTime() { RTC.adjust(DateTime(F(__DATE__), F(__TIME__))); // Setting time to compile time. delay(1000); handleOnConnect(); }We will create a webserver at 192.168.1.5 IP and have 2 routes to get time and to setTime.
Time will be set to compile time of this code. You can make changes to fetch the time from NTP server and have time set correctly based on your usecase.
Compile and upload the code to NodeMCU.
Now take a device and connect to sensorism SSID
On browser go to http://192.168.1.5/setTime
you will be able to see time being set on clock
now got o http://192.168.1.5 to see clock ticking. Reload this page multiple times to see clock running.
Now turn the device off and turn it on again. Connect to SSID sensorism , wait for few sec for clock to tick and go to http://192.168.1.5 on browser. You will be able to see the clock running despite not setting the time second time.
1. USB port not visible on Aurdino IDE after connecting to NodeMCU. This happens when you use charging cable instead of data cable. I had to spend 4 hrs trying multiple cables all of which are charging only cables. Please check if the cable you are using is charge only or data cable. You can check by connecting your mobile using hte cable to laptop/raspberrypi and see if mount is successful. I had a time when all 4 cables i tested are charge only cables.
2. DS1307 getting reset on power off. Check the battery is kept correctly and battery voltage is functional range.
3. Battery over heating. You may encounter that battery is over heating. Its because DS1307 uses input power to charge battery as well. But CR2032 doesnt support discharge only battery. Use LIR2032 or disable charging of battery on DS1307 module.
Comments
Post a Comment