Arduino Real Time Clock - Using the Tiny RTC

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today in the workshop we'll learn how to use a real-time clock with the Arduino we'll see how the DS 13:07 real-time clock works and how to use it with interrupts we'll also build a clock with a temperature and humidity display so get ready to have a great time and welcome to the workshop [Music] hello and welcome to the workshop today we are going to talk about a very timely subject than that being real time clocks now what exactly is a real time clock is there such a thing as a fake time clock or an unreal time clock well no a real time clock just allows you to answer one of the most basic questions that I'm sure you asked once or twice every day what time is it right now now the Arduino um Arduino are wonderful devices we use them for a number of different projects they're very capable they've got digital i/o pins they've got analog the digital converters they can communicate with i2c SPI serial communications but one thing they cannot do is answer that question what time is it the Arduino does have some time related functions there is the delay function there's also the Millis function that'll tell you the number of milliseconds that have elapsed since the Arduino was last rebooted or powered up but there is no function to actually give you the time of day and for that you need to add an additional component a real-time clock now I'm going to be using a real-time clock that's based on the DS 13:07 real-time clock chip and it's a very popular module it's called the tiny RTC module so what we're going to do today is hook a tiny RTC module up to our Arduino it hooks via the i2c bus or run some sketches to set the time on the module and to read the time in the module and then we'll look at a more advanced use of the module using interrupts so let's get started and learn a little bit about the tiny RTC module and the DS 13:07 real time clock chip let's take a look at the DS 13:07 real time clock chip the DS 13:07 counts time date day of week month and year with an accuracy down to one second it can be used in a twelve or a 24-hour format the device has leap year compensation to the year 2100 which incidentally is not a leap year the DS 1300 °c two-wire serial interface in addition to its clock functions it also has a programmable square wave output the device has battery backup with automatic power failure detect and switchover in battery backup mode the device consumes less than 500 nano amperes for very long battery life now here are the pin outs of the tiny RTC one of the most popular DS 1307 based modules he will note that the pin notes are repeated on both sides of the device first we have the device select which allows you to enable or disable the device next is the i2c serial clock followed by the i2c serial data the VCC connection is for the power supply and of course there is a ground connection there is also a square wave output and an output from the backup battery so now let's start using two DS 1307 so here's the tiny RTC real-time clock module as you can see I've soldered some header pins to one side of the module and there's a connector on this side as well if you're making the i2c connections it doesn't matter what side you connect to they actually provide two sets of connections which is pretty common for i2c modules because sometimes you want the daisy chain these things now if you flip the module over you'll notice that there's a coin cell and a holder over here if you order your module from an overseas source there's a good chance it will arrive without the battery in it and you'll have to install it itself and that's just because of regulations involving shipping lithium batteries but otherwise it's a very simple little module and as you're about to see it's quite an easy module to use so now that we've looked at the tiny RTC module it's time to put it to use the module uses the i2c bus so hook up to the Arduino is very simple once we hook it up we'll download a couple of libraries run a few tests sketches and see how we can set the time and read the time on the module so let's get going and hook it up to our Arduino now here's how we're going to hook up our real-time clock module you will need an Arduino Uno and a tiny RTC real-time clock module we'll begin by connecting VCC on the module to there we nose 5 volt output next we'll connect the ground on the module to the Arduino ground the SDA connection on the module will connect the Arduino analog pin a for some Arduino is also have an SD a connection and you can use that instead finally the SCL connection on the module will connect to arduino analog pin a5 or the SEL connection if your Arduino has one and this completes the hookup of the tiny RTC module there are a number of different libraries you could use with the real time clock today I'm going to be using two libraries that were contributed to the community by Paul storage in who has contributed a lot of code to the Arduino community I'm using his DS 13:07 RTC library and it is also dependent upon the time library you can find details for downloading and installing those libraries into your arduino ide on the article accompanying this video on the durham bought work.com website once you have the library is installed you can go and take a look at a couple of the example sketches as you'll go into examples and scroll down to DF 13:07 RTC where you'll find read test and set time now the set time sketch will set the time of your real time clock to match the time on the computer running your arduino ide so you'll want to ensure that your computer has the correct time since most computers synchronize to a network time protocol server that probably isn't a problem the sketch we'll start off by including the wire library which is the built in arduino library for dealing with eye to sea will also include the two libraries we just downloaded now the sketch has a number of different elements in it some of which you may not be familiar with one of the ones over here is tml TM elements is a data structure and allows convenient access to the elements of time that we are all familiar with like minutes seconds hours days etc without using a data structure at the time you are going to get back is going to be in the number of seconds that have elapsed since the beginning of unix time which is january 1st 1970 it is far easier to use a data structure for that we define ours as being TM now most of the activity in this sketch occurs down at the bottom here in these two functions the gets time and get date functions and what these functions do is they take the time and date that are in your arduino compiler so in other words the time and date on your system and these are returned and so get time and date are called in the setup and these are written to the real-time clock using this function here and then after that we open the serial monitor and read back that time and that date now this code actually has no function whatsoever in the loop and so this code will now program your real-time clock to match the time on your computer so by running this and opening the serial monitor we should see that the time correlates as long as it's capable of writing the Dia's 1307 chip so let's take a look at that right now now writing the set time sketch really couldn't be simpler observe the time on your computer and then upload the sketch to your Arduino once it's uploaded simply open your serial monitor and you should observe that the time that the real-time clock has now correlates to the time you have on your computer and that's all there is to it now the other example sketch that's included with the DS 13:07 RTC library is the read test this shows you how you can read the time from a clock module that has already been set and as you'll see it's actually pretty simple now we will start off by including the same three libraries the wire library for i2c the time librarian DS 1307 RTC library then we go into the setup when we setup our serial monitor at 9600 baud and just print DS 1307 RTC read test now before we go into the loop let's take a look at a function that we have down at the bottom because this function is used within the loop the function is called print to digits and all this functions purposes is to format the time correctly because when the digit is below 10 such as nine seconds eight seconds or nine minutes or eight minutes we'd like to proceed it with a zero and that's what this function does it sees if the number is between zero and ten including zero and if it is it writes an additional zero to it so it formats it nicely in the loop we start off again with the TM elements the data structure that we spoke of earlier when we define that as TM so that we can get our basic unit of hours minutes and seconds and then it is just a matter of reading the real-time clock and printing out those values of TM hours and then a colon the minutes a colon a second and then we'll do a print that says day month here equals and then print a day with a slash a month with a slash and a year notice how we use the print two digit phone shion's on the hours and the minutes the format those correctly so that's really all that there is to it to read at des 1307 real-time clock once it has been programmed with the time so let's run this and see what it looks like running the read test sketch is just as simple as a set time sketch once you've uploaded the sketch to your Arduino just open up your serial monitor and if everything is working you can see the clock that matches the clock on your computer giving the correct time and date and again that's really all there is to it we've now proven that RDS 13:07 real time clock is functioning correctly and as the correct time and date so now we've seen the fundamentals of using the tiny RTC clock with the arduino and if all we wanted to do was display the current time in our arduino in other words build a clock we really don't need much more we could use a liquid crystal or an OLED or an LED display instead of the serial monitor and build ourselves a clock although chances are that isn't what you want to do since digital clocks aren't exactly expensive and they're pretty readily available now another thing you can do with the tiny RTC module is generate a square wave you may have noticed that there's a square wave output on one of the connectors on one side of the module now that square wave can be generated at a number of preset frequencies and I will show you how you can do that now now the preset frequency I'm going to be using is one Hertz so in other words you'll get one pulse every second now you could do a number of different things with this you could eliminate the Arduino entirely and use that one Hertz pulse to pulse a stepper motor and make yourself an analog clock if you wished but another thing you could do with that pulse is use it the cause and interrupt now we've discussed interrupts previously interrupts essentially are exactly what they sound like an interrupt comes along and the processor stops everything it's doing and services the interrupt and then goes back to doing what it was doing before the interrupt came there are a couple of pins on the Arduino that are set for hardware interrupts and those pins differ from the model of Arduino that you have so on different Arduino you'll have different pins doing interrupts the Arduino Uno pins 2 & 3 can be used for interrupts and those are interrupt 0 and interrupt 1 respectively so I'm going to show you some code right now that can make use of one of those interrupts we're going to connect the square wave output from the real-time clock module to one of those interrupt pins and run a bit of code will initially just flash an LED with it but then after that I will show you something practical you can do using interrupts with the real-time clock so let's get going and take a look at that now to perform our experiment with the square wave interrupts we'll start with the same wiring that we already have you can add an LED and a dropping resistor if you wish although these are optional you can also just elect to use the onboard led on the Arduino if you do decide to use these I use two 220 ohm resistor but any value from 150 to 470 ohms with suffice we need to make one additional connection from the real-time clock module to the Arduino so connect the SQ or square wave output to pin 2 of the Arduino digital i/o if you're using the external LED connect one side of the resistor to pin 13 and the other side to the anode of the LED connect the cathode of the LED to the ground on the Arduino and this completes the wiring now here's the arduino DS 1307 set square wave interrupt sketch provided by Robert Albrecht and it's a really nice sketch what I like about it is that it illustrates how to use the i2c library very well and it doesn't require any additional libraries so you will start by including the wire library which is the library for communicating with the i2c bus that's included in your Arduino IDE next we'll make a couple of definitions ds1308 designer pin that we are connecting an LED to now we could use the number 13 over here from nard we know uno but it's actually better to use this statement LED built-in because that will work on every Arduino board next we have a function called set sqw and this actually sets up the frequency of this quarter wave and so we're going to pass it a value that you'll see a little later and we start off on the wire library we begin our transmission so we transmit over to the control idea over here so we know we're talking to the DS 13:07 then we'll write a value of seven which is called a preamble and then after that we write the actual value that we've passed over here which will set up the square wave frequency and then after that we end the transmission so this is an excellent illustration of how you communicate with devices on the i2c bus next we have handle int this is our interrupt handler because what we are doing is we are creating an interrupt so every time the interrupt has been detected we'll call this interrupt handler and this interrupt handler simply flashes the LED now if we go into setup we'll see how we set everything up and it's quite simple really we'll set up the stereo monitor and we'll print a few things over to the serial monitor we define the LED pin as being an output next we attach our interrupt now we're going for interrupts ro which is data pin 2 on the Arduino Uno and then we will call our interrupt handler which we called handle int that was this up over here and we do this on the falling edge of the interrupt so every time we see this interrupts on data pin 2 which is interrupts 0 will call handle int which would flash the LED and over here finally we set the actual square wave frequency on the DS 13:07 by using the set s qw function we discussed earlier and we pass it this value which will set the frequency to 1 Hertz this sketch has nothing in the loop because everything is accomplished here in the setup so let's take a look at this working now now here's our square wave interrupts sketch it's a very simple demonstration as you can see on the screen we can see that it has set the square wave to 1 Hertz and it's attached itself to the interrupts on pin d2 which is this blue wire over here coming from my real-time clock and I've got an external LED that is blinking on and off and you can also see if you look carefully that the onboard led is doing the same visually it looks pretty well just like a fancy version of the blink sketch except of course this one is working by using an interrupt every time it sees a pulse on pin d2 so it's a very simple but effective demonstration on how to use that square wave signal as an interrupt for the Arduino so our interrupt-driven sketch seems to work pretty well it flashes an LED every time it receives an interrupt but as far as being a practical sketch it really isn't I mean there are other ways of flashing an LED and if you really wanted to connect the LED directly to the real time clock moduel square wave output and just flashed that way and eliminate the Arduino altogether so let's take a look at a more practical use for using interrupts now what I'm going to do is use a temperature and humidity sensor I'm going to use an AM 2320 which is an i2c temperature and humidity sensor but the same thing would apply to a dht22 or dht11 which are more common temperature and humidity sensors one of the things with these sensors is they need about two seconds between readings to stabilize and so the way that we ran our older sketch is where we displayed the time isn't going to work in those sketches we go out to the clock module read the time displayed on the serial monitor and then we delayed for one second and then went back and did the whole thing over again of course the time would have incremented by one second when we read it next but if we need to wait at least two seconds the stabilizer module that isn't going to work by the time we go back and read the clock two or maybe even three seconds will have elapsed and the clock display is going to be a bit erratic and so interrupts are a way of resolving this issue now what I've done with this sketch is I'm going to read and interrupt every second and I'm going to increment the counter and when the counter is incremented I'm going to go out and read the time and display it so every second I'll display the time and I'll also display the humidity reading and the temperature reading which I have stored in a variable I'm only going to update the temperature and humidity sensor every 10 seconds however because I figure that in normal situations the temperature and humidity in your room or your environment isn't going to fluctuate that much within 10 seconds now if you want to you can rewrite the sketch just change the number and set it down to two or three seconds if you want to read it more regularly but I thought this was an easy and nice compromise so let's take a look at that sketch right now now we're going to start off with the same circuit we did with the real-time clock and the square wave driving the interrupt on pin 2 I've removed the LED and the resistor from the circuit we're going to add the a.m. to 3 to 0 temperature and humidity sensor into our circuit the pin on the far left side of the a.m. to 3 to 0 is the VCC pin and this will be connected to the 5 volt line the second from the right pin on the a.m. to 3 to 0 is the ground pin and this of course will be connected to the Arduino ground the second pin on the left on the AM 2320 is the SDA pin so connect this to the analog a 4 line or the SDA input if your Arduino has one and finally the remaining pin is the clock line the SCL pin so now that you've hooked this up let's take a look at the sketch that we'll get the AM 2320 working with our real-time clock now here's the sketch we're going to use to add the AM 2320 temperature and humidity sensor to our real-time clock circuit now if you've been paying attention you'll probably notice that this sketch is essentially an amalgamation of the sketches that we looked at earlier so we're going to start off by including the required library so we have the wire library the two libraries for the real-time clock and an additional library the Adafruit AM 2320 library now if you have not installed this library before you can find the AM 2320 library in your library manager however you'll also need to install the Adafruit unified sensor library as this is used with many Adafruit sensors it's not called from the sketch but the Adafruit AM 2320 library is dependent upon it next we'll start off by doing the same thing we did before we'll define the address for the i2c sensor because we're going to need to address it and set our square-wave to 1 Hertz as we did earlier we'll also define an object for the am 23 20 in here that I'm calling ticks and the old tick value this is basically a value that's going to increment every time the square wave causes an interrupt in the Arduino and we're going to use this as a counter I've also got a couple of variables for the humidity and the temperature over here now we go into the setup will set up our serial monitor and as before we're going to attach the interrupt to the pin d2 which is interrupts are over here so every time we get a falling pulse over here it'll call the handle int function which we'll take a look at in the moment once again we're going to set the square wave at 1 Hertz so we call a set sqw function that you saw earlier and passed this value to set the square wave to 1 Hertz we'll initialize a temperature and humidity sensor then delay for two seconds and read the temperature and humidity sensor remember we need to do the delay in order to allow the sensor to stabilize before we take a reading now let's go on into the loop there's not that much in the loop because it's done mostly in the functions first of all we wanted to read the temperature and humidity sensor every 10 seconds now if you wish you could read it more frequently although I wouldn't go below 3 seconds but at any rate we count the number of ticks and when the ticks are reached 10 will read the humidity and the temperature and assign it to the two variables and then reset the tick value to 0 now this is where we update the serial monitor now if the ticks are not equal to the Alltech value it means the tick value has incremented remember this could be executed several times within a second and we don't want to repeat the reading of the seconds so we wait until the tick is incremented to a value higher than its previous one if the value has incremented then we'll reset the tick value to all tick value so that we can run this next time and then we call a function I'll show you in a second called print current time and pass it to humidity in the temperature now here's our interrupt handler the interrupt handler simply does one thing it increments the values of the ticks by one and that's it it's always important to do as little as you can within an interrupt handler because you're stopping processing of everything else okay the set sqw we've seen before this de sets the frequency of the square wave and we looked at that earlier we've also seen this before the print two digits function which simply takes one digit number and places a zero in front of them in order to make them two digit numbers and now the print current time function would prints to the serial monitor as we saw earlier we're going to pass it both the temperature and humidity value we start off a game with our data structure and then we go through when we did what we did before we're going to print the hours minutes and seconds i've formatted this to just have the hours minutes and seconds followed by the date in universal date format and then after that we'll print the humidity and the temperature we'll do a new line and that's it for the function so once again this is really an amalgamation of things that we have seen before so now let's load this up to our Arduino and take a look at it and see how it works so here's our real-time clock with our temperature and humidity module and I've got the temperature and humidity module right here in a breadboard and the real-time clock is over here and of course here's my Arduino now if you take a look at the screen you can see that we are indeed reading the time and the temperature remember however that we update the temperature on every tenth tip or every 10 seconds so what I'm gonna do right now is I'm going to just raise the temperature a bit I'm going to throw a heat gun [Music] [Applause] now notice the temperature has not yet gone up it's done every 10 seconds but we should eventually see that rise and there we go now we're at twenty eight point five degrees Celsius and we'll wait another ten seconds look at that now we're at thirty six degrees Celsius so as you can see the temperature and humidity module is only being read every 10 seconds now in normal circumstances of course the temperature shouldn't rise that drastically within a ten second period but if this is a problem for you you could also reduce that time down to about three seconds or maybe even two seconds to have a more responsive meter but otherwise our circuit seems to work just perfectly all right well that about wraps it up for today I hope you enjoyed the video and that you're now thinking of making time aware Arduino projects now as always you will find a detailed article accompanying this video on the drone bot workshop comm website you will find the link to that right below the video while you are on the website please consider joining my newsletter this is not a sales letter it's my way of keeping you informed about what is going on in the workshop and also of soliciting your opinions so that I can find out what other subjects you would like me to cover in these videos and articles also if you have not subscribed to the YouTube channel please do that there's a lot going on in the workshop will be having videos every Saturday and robot videos every Thursday so if you haven't subscribed already please do that until the next time we meet please take care of yourselves and I hope to see you very soon again here in the workshop good bye for now [Music]
Info
Channel: DroneBot Workshop
Views: 123,134
Rating: 4.9267225 out of 5
Keywords: real time clock, arduino tutorial, arduino clock, Tiny RTC, ds1307 real time clock
Id: lyvoOEO-Ncg
Channel Id: undefined
Length: 31min 28sec (1888 seconds)
Published: Sat Mar 09 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.