#5 Arduino compatible Real Time Clock modules (RTC) - DS1307 & DS3231

แชร์
ฝัง
  • เผยแพร่เมื่อ 13 พ.ค. 2024
  • Adding a Real Time Clock to your Arduino project is EASY and I'll show you how.
    RTC modules cost very little and can be added to your project to add data-logging or anything where you need the date and time.
    Just four wires and you can connect an RTC to your Arduino or other microcontroller that uses the I2C bus. Easy.
    Sketch can be found here:
    github.com/RalphBacon/DS1307-...
    See further videos in this series in this playlist: • Beginners' Guide To Ar...
    And my channel is here:
    / ralphbacon
    How can I remember this? Memory tip: "See" Ralph Bacon, geddit?
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 118

  • @MrBobWareham
    @MrBobWareham 4 ปีที่แล้ว +2

    Thanks, Ralph very helpful and easy to follow and understand your a star thanks Bob

    • @RalphBacon
      @RalphBacon  4 ปีที่แล้ว

      Wow! That's an old video you dusted off there, Bob, glad you liked it despite the dodgy audio quality!

  • @RalphBacon
    @RalphBacon  8 ปีที่แล้ว +3

    The code I'm using can be found here: dl.dropboxusercontent.com/u/20622434/DS1307_DS3231_Reference_Code.ino
    I'll soon also add a (very) short video explaining what the BCD (Binary Coded Decimal) is all about - it's much simpler than you might think.

    • @tomgullan3565
      @tomgullan3565 3 ปีที่แล้ว

      Hello Ralph. I've just very recently subscribed to your channel and am in the process of following your Arduino tutorials. I'm getting an error code 404 when I try to access your code on Dropbox. Is the code still available?
      I like the content and your style, thank you.

  • @CRETEJOE
    @CRETEJOE 2 ปีที่แล้ว

    Hello Ralph, I want to build a rain sensor so that we do not have to keep checking when the washing is out. I got the idea from my grandson, who watches videos about model RC cars, to search TH-cam. Luckily I found you. I watched your rain sensor video and realised that I need to learn about Arduino. So at the age of 71 I'm going back to school watching your basic videos. Hopefully I will learn enough before I expire. Thanks for the videos and slow clear explanations John

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      I'll be doing a new *rain sensor* project "soon", John, as I discovered the old way (used in Benny's outdoor cat run) would detect rain but then continue to say that rain was present even though it had stopped quite a while ago (or it was just a few drops).
      The next time I do this project I will add a small heating element under the sensor so that rain is evaporated very quickly thus giving a true indication of the presence of rain. Once it has stopped raining the heating element will switch off.
      What you can do is still construct the rain sensor (remember to poll infrequently [eg every 15 seconds] to prevent oxidisation of the sensor) and when I've done the project with the heating element just add that bit. I'm sure that at 71 you still have many productive (Arduino) years ahead of you, and I welcome you on my journey!

    • @CRETEJOE
      @CRETEJOE 2 ปีที่แล้ว

      @@RalphBacon Thank you for your reply. I'll only put my sensor out with the washing and bring it back in so some of the features will not be needed by me. In the past I have programmed in BASIC as well as made web pages using just note pad and HTML so code is not alien to me. Watching some of your basic videos I now have a concept of the hardware however, finding and installing the compiler, inserting code and sending to a device is something I do not know. Do you have a video about installing and using the compiler from the start?

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      Just go to the Arduino.cc website, download the Windows version of the IDE (currently version 1.8.15 at time of writing) and install it like you would any other Windows/Linux program. If you can launch it and get a blank(-ish) code area then you are almost there.
      Look at some of my (really) old videos where it describes how to get the COM port of your Arduino that you just plugged in and use that in the settings under the Tools menu. It might take you a while to "figure it out" but everyone has to go through this - me included!
      If you use an Arduino UNO (or clone) you can get the Blink example program running by toggling pin 13 which has the built-in LED attached. That will be your Eureka moment.

    • @CRETEJOE
      @CRETEJOE 2 ปีที่แล้ว

      @@RalphBacon Thanks again Ralph. This is all so much fun and fantastic to have help.

  • @michaeltyborski4802
    @michaeltyborski4802 6 ปีที่แล้ว

    Thank you for showing us how to use I/O modules with the Arduino. Lots of vendors, products, and nuances make design choices interesting. To you and the viewers: Thank you for making the technical world understandable and more creative!

    • @RalphBacon
      @RalphBacon  6 ปีที่แล้ว

      Thanks for that, Michael Tyborski, nice to hear from you.

  • @luthodyiki5902
    @luthodyiki5902 5 หลายเดือนก่อน

    Thank you very much,great explanation. I’ve realised that after a day my seconds starts to be leading by 6 seconds from the real time how can I fix that.
    Thank you.

    • @RalphBacon
      @RalphBacon  5 หลายเดือนก่อน

      If you are using a DS3231 I would be very surprised at such a discrepancy.
      If you are using a DS1307 I would not be at all surprised and is why I don't use those devices.
      Which one are you using?

  • @LuberthDijkman
    @LuberthDijkman 3 ปีที่แล้ว

    Verry good, will change my thermostat tft project
    The to many rtc.libs .h files people can choose to include make arduino dificult

    • @RalphBacon
      @RalphBacon  3 ปีที่แล้ว

      Make sure you use the DS3231 not the awful DS1307. The former keeps time very accurately indeed, once it has been set.

  • @tlotlisangnkhase9475
    @tlotlisangnkhase9475 ปีที่แล้ว

    This is good Ralph, can you help if I want to display hours, minutes, second on a separate line rather than displaying everything in one line

    • @RalphBacon
      @RalphBacon  ปีที่แล้ว

      I was only outputting the information in the Serial Monitor so you could just use "Serial.println" rather than "Serial.print" to move to the next line once the current data has been printed.

  • @Jonas-qz2gb
    @Jonas-qz2gb 3 ปีที่แล้ว +2

    First of all thank you for the great video!
    I was getting frustrated with the DS3231 because there are so many poorly documented libraries and nothing was working, so I decided to configure it only with the datasheet and the Wire library like you did.
    I used part of your code from github for setting the time and added some functions to use the alarm 1 of the DS3231 which I need for a project and it is working great now!
    However, I was wondering as to why you were using pointers in the readDS3231time() function. Is it just to make the code work without any global variables?
    Is it that you could not pass the seconds, minutes, hours ... variables directly to the function because they are only declared in the displayTime() function and for the readDS3231time() function they are undeclared? So instead you reference them using pointers to be able to change their values even though the variable itself is undeclared in the readDS3231time() function?
    Sorry if the question might be dumb, I am not that comfortable using pointers yet, hence the confusion. I would be happy if you could help me out with my question!

    • @RalphBacon
      @RalphBacon  3 ปีที่แล้ว +1

      I wrote this over 4 years ago so I'm guessing that I didn't want global variables (I still don't like them) so using pointers is a good way round that and allows the subroutine to update them too, if required. If not, then passing by value is good enough.

    • @Jonas-qz2gb
      @Jonas-qz2gb 3 ปีที่แล้ว

      @@RalphBacon thank you a lot for your fast response! Now I get it.

  • @mikebarton3218
    @mikebarton3218 5 ปีที่แล้ว +2

    Hi Ralph, I used the code from video #5 and it works great. I am trying to insert a conditional statement into the code so that if hour = a value (e.g. 12) and minute = 00 and second = 00 {turns a pin high with digital write}. I have used parts of the blink sketch for this. However, I can't get it to work. I have also downloaded various ds3231 libraries and examples to no avail. I'm a total newbie at this (age 64) but don't want to go back to a hardware solution. Can you point me in the right direction? Many thanks, Mike - long time subscriber and avid viewer.

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      First off, thank you for being a subscriber and avid viewer - I need more like you, Mike! Now let's see if we can get you up and running. Your pathetic age excuse does not wash with me! How are you extracting the various elements of the time so that you can then query the value? If you go to my channel page, About tab, you will find my email address and you can send me what you have done so far; it will be far quicker that way. Sorry to make you hunt for that email, but if I publish it here I will have a gazillion emails! I'll await your sketch email...

  • @donaldnorris5659
    @donaldnorris5659 4 ปีที่แล้ว +1

    Very good. How about a time and temperature sketch since the ds3231 has that ability?

    • @RalphBacon
      @RalphBacon  4 ปีที่แล้ว +1

      The temperature sensor on the DS3231 is not that great to be honest, Donald. It would be better (in terms of accuracy) to use a dedicated temperature sensor such as the DHT22, DHT11 or the DS1820 all of which I've demoed in my videos. It can be very frustrating to read a temperature of, say, 20°C yet you know it's only 18°C from another source! You can tweak it in your code, of course, if the drift is stable and linear otherwise it gets too complicated and it's much easier to use a separate sensor.

    • @donaldnorris5659
      @donaldnorris5659 4 ปีที่แล้ว

      @@RalphBaconThanks for the reply. i'll look into that.

  • @SusanAmberBruce
    @SusanAmberBruce 5 ปีที่แล้ว

    Thanks, I got a DS3231 through the post today, watched your video and now it's telling me the time via the serial port on the Arduino.

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      Congratulations on getting this working, Susan. And thanks for letting me know my video was of some help. Onward and upwards in the only direction for you now! Once you have an Arduino Uno (or Nano, or even an ATTiny85) you will never use another 555 timer again. I know, it happened to me. Thanks for posting, nice to hear from you.

  • @malekiiaetius7036
    @malekiiaetius7036 2 ปีที่แล้ว

    In the setTime, how can we adjust the RTC automatically from computer time? Not using the hard coding setDS1302Time(18, 10, 10, 7, 18, 9, 2020);
    Thanks a lot

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      Unfortunately it's not possible. The standard way of doing this is to select a future date/time, wait for that time to arrive and set your program running so it picks up the correct time. Then be sure to comment out that bit of the code so your time is not reset again and again! Of course, if you write code in your Arduino to set the time on the DS3231 (like a bedside alarm clock) you can do it like that but it's a longer process.

  • @anukeshambatkar6255
    @anukeshambatkar6255 4 ปีที่แล้ว

    sir how to replace ds1302 rtc with ds3231
    what changes we have to do in program

    • @RalphBacon
      @RalphBacon  4 ปีที่แล้ว

      They both use I2C, Anukesh, to communicate with your µController so you should be able to swap one out and replace it with the other. Then change the library you're using so it corresponds to the correct module and it should "just work". I've found a written article on this:
      tronixstuff.com/2014/12/01/tutorial-using-ds1307-and-ds3231-real-time-clock-modules-with-arduino/
      I hope this helps.

  • @In20xx
    @In20xx 6 ปีที่แล้ว

    Your videos are great, thanks!

    • @RalphBacon
      @RalphBacon  6 ปีที่แล้ว

      Thanks for your post, Cy Porter, good to hear from you.

  • @fleckditchburn2494
    @fleckditchburn2494 4 ปีที่แล้ว

    At the end of the video #5 RTC you say you have a video on how to set the times.Can you please tell me what number this video is.
    Great tutorials you provide love watching them.
    Thanks very much.
    Fleck

    • @RalphBacon
      @RalphBacon  4 ปีที่แล้ว

      Hmm, I vaguely remember this video (from 4 years ago!) and I also remember writing a nice bit of code to set the date and take into account leap years, DST and probably a few other things. Unfortunately it never got made into the promised library. [Some time later...] However, I have found the code. Leave it with me for while, Fleck, as I'm recovering from eye surgery so I'm limited in my computer time but I hope to nail this one soon.

    • @fleckditchburn2494
      @fleckditchburn2494 4 ปีที่แล้ว

      @@RalphBacon
      Ralph
      Thanks for the quick response.
      I wish you well with your eye problem, there is always something trying to hold us back.
      I also have medical problems in 2009 at the age of 59 I was diagnosed with Motor Neuron Disease (MND), and given two years to live. I am totally paralyzed and operate my computer using my eyes. Next month (June) I will be 71 years old and still loving my computers.
      I find Arduino very frustrating especially the libraries. I have been working on a clock for a week or so now and always find problems when compiling mainly due to libraries.
      I decided to have a look inside the library code of the DS3231. Having looked in a few libraries with the same name I was surprised to see different code in each one. This of course is very miss leading when they all have the same name.
      On your video #83 I smiled when you said the following (Timeline 2.48)
      “… was it easy to do this hmm, sort of well no really it wasn’t it was a lot of faffing about with libraries….”
      It made me feel I am not the only one suffering.
      Thanks very much for having a look for your code.
      Fleck

    • @RalphBacon
      @RalphBacon  4 ปีที่แล้ว

      If you like I can just let you have my code and you can do with it what you want? Sorry to hear about your medical situation, as you say, there are always road bumps to slow us down! Let me know about the code, I can either email it to you or put it in DropBox (probably better that way). I agree about the library fiasco. Sometimes my code picks up the wrong library (because authors insist on using the same name, why?!?) and I wonder why my code does not run! Grrr!

    • @fleckditchburn2494
      @fleckditchburn2494 4 ปีที่แล้ว

      @@RalphBacon that would be fantastic, if you email it me at fleck7@gmail.com.
      I do enjoy life and my motto for MND is Must Never Despair.
      I'm going to 3D print a little enclosure for the clock I will send you a picture of my progress so far with the clock display, but not through TH-cam via email.
      Thanks very much, I will keep in touch with you.
      Fleck.

    • @RalphBacon
      @RalphBacon  4 ปีที่แล้ว

      Sent you the email!

  • @johnmellor6065
    @johnmellor6065 6 ปีที่แล้ว

    brilliant stuff your making me look like a genius

    • @RalphBacon
      @RalphBacon  6 ปีที่แล้ว

      Thanks for that, genius john mellor, nice to hear from you.

  • @davewreski6900
    @davewreski6900 4 ปีที่แล้ว

    Hello Mr. Bacon, Can you help me send commands to the RTC module. I have spent hours trying without success. One of them I would like to change it to 12 hour mode. I have tried to modify some of your code but ran into a brick wall. Please help.

    • @RalphBacon
      @RalphBacon  4 ปีที่แล้ว

      Are you talking about using the (Adafruit or similar) library, Dave? The obvious method would be to do something like:
      if (rtc.hour() > 12) {
      localHour = rtc.hour() - 12;
      localPM = true;
      }
      else
      {
      localHour = rtc.hour();
      localPM = false;
      }
      I'm making up the calls to the rtc object but it will be along those lines.
      Or were you asking a different question?

  • @arvidlinden5292
    @arvidlinden5292 5 ปีที่แล้ว

    Thank you! Worked great!

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      You are most welcome arvid linden, I'm glad you like the video. Nice to hear from you.

  • @zakdunn
    @zakdunn 8 ปีที่แล้ว +1

    Hey there Ralph, great informational video! You mention that the DS3231 is much better than the DS1307, any reasoning for this?
    Thanks!

    • @RalphBacon
      @RalphBacon  8 ปีที่แล้ว +3

      +Zak Dunn
      Indeed! The DS3231is more accurate and is temperature compensated - so after several months it is still pretty much spot on whereas the DS1307 will be a minute or two out. Also the DS3231 board (well, the one I have, pretty sure it's standard) has a built in temperature sensor just as a bonus!

  • @porlando12
    @porlando12 6 ปีที่แล้ว

    Is there a better way to set the time instead of hardcoding it one time in the setup loop? I would like to accommodate for 1-second RTC drift by synchronizing the arduino with the local time of my computer upon uploading the sketch.
    Thanks for the video!

    • @RalphBacon
      @RalphBacon  6 ปีที่แล้ว +2

      You can't programmatically set the date from your PC, Philip, whilst uploading the code (we all wish!). But there are several ways to set the date/time programmatically (I've even written a library that only needs two switches but have yet to publish it, because I want to use a Rotary Encode instead, more flexible). Or set it from a GPS module every hour/day (I've done a video on that too). Any good?

    • @porlando12
      @porlando12 6 ปีที่แล้ว +1

      Ralph S Bacon thanks for the info!

    • @MikeK8LH
      @MikeK8LH 5 ปีที่แล้ว

      Couldn't you use the compiler "__date__" and "__time__" parameters to set the RTC chip when uploading code to the Arduino? It seems David Johnson-Davies has done just that in one of his blog posts (www.technoblogy.com/show?1BGY).
      Cheerful regards...

  • @raymondheath7668
    @raymondheath7668 7 ปีที่แล้ว

    You said that the 1307 is not temp compensated as the 3231, but I assume that this would only be a problem in a widely varying temp environment. Also you had a physical II2C address of 68, but I did not see the address links connected when you showed the board. How does that work?

    • @RalphBacon
      @RalphBacon  7 ปีที่แล้ว +1

      In my experience, Raymond, the DS1307 loses time like I lose my hair. Wildly varying temperatures just make it (even) worse. Go for the DS3231 every time! To discover the address of any I2C device just use the I2C scanner sketch; I'm guessing this one default to address 68 despite no pads being linked which is good because too many devices default to 0x27 range. It certainly worked though!

    • @raymondheath7668
      @raymondheath7668 7 ปีที่แล้ว

      I will have to try that sketch. I am not used to things hanging on a serial line without a physical address. When we were developing Arinc 429 and custom chips back in the early 80's each box or board knew what packet to grab because it had its address on the leading bytes

    • @RalphBacon
      @RalphBacon  7 ปีที่แล้ว +1

      I2C certainly uses physical addressing, as you know. When the master pulls the SDA line low (indicating a START condition, when all devices on the bus start listening) the next thing the master transmits is the address of the device (eg 0x68) to which it wants to talk. Only that device then responds with an ACK by pulling the SDA line low (which the master is by then listening for). The spec sheet for the DS3231 says the address is fixed at 0x68 with no means to change it, pity but at least it's well out of the 0x27 range. Run the sketch and you'll see it all works!

  • @borisbogdan6705
    @borisbogdan6705 5 ปีที่แล้ว

    A great thumb up

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      Wow, thanks Boris, this is an old video indeed (and the sound quality leaves a little to be desired) but still good information. Thanks for posting.

  • @m0gga
    @m0gga 7 ปีที่แล้ว

    Another great video Ralph!
    I am still working on my aquarium controller project as per my last message regarding SDR's. So far I have successfully created a multi layer menu system which is displayed on a 20x4 I2C LCD and would like to incorporate a clock on one of the pages on the LCD menu. However I have a small problem, I have a button scanning routine which is constantly running in the loop section of the code so how can I display the clock at will and still monitor for button presses. I don't wish to display the clock all the time but would like to dismiss the clock and return to my menu system via a button press.
    Any pointers you can offer me would be greatly appreciated. Thanks.

    • @RalphBacon
      @RalphBacon  7 ปีที่แล้ว

      Hi Fred, good to hear from you again.
      Yes, you can do what you want with the clock. As you are constantly checking for key presses in the main loop (that's good) when you detect that the "ShowClearClock" button has been pressed then (from the loop) run the routine that shows the clock, set a (global) flag that remembers this and return to the loop.
      When the "ShowClearClock" button is pressed again, run the routine again except this time clear the clock (you know it's already being displayed by inspecting the relevant flag), set a different flag for the main loop to detect that the main menu should be shown, the loop then runs that bit of code via a function call and shows the menu.
      Alternatively, you could show the clock for, say, 5 seconds, then automatically return to showing the menu. This is also controlled from the loop by decrement the count every loop until 5 seconds has passed then automatically call the "ShowClearClock" function.
      I think you're already done it this way: the main loop checks "Machine State" and fires off the required routines. So when certain buttons are pressed the main loop receives back a value from the "CheckButtons" routine and calls the routines that, for example, show the clock, display the menu etc. Each function does its own job and ONLY its own job with the main loop coordinating what needs to be done by inspecting flags (variable values).
      In this way there is no blocking code (no delay() calls) as each function knows what it is waiting for and carries on once it has the required input.
      I hope this helps, feel free to ask further questions. I'm almost tempted to do a (simple) video on this!

    • @m0gga
      @m0gga 7 ปีที่แล้ว

      Thanks Ralph for the very helpful reply, actually I was 'as we speak' working on just such a method that you have suggested. I absolutely love coding, it really keeps your brain active which can only be a good thing.
      I first got hooked on programming back in the day of the Sinclair Z80 home computer with only 1Kb of usable ram on board, so I was programming it in low-level Z80 machine code, (none of that assembly language rubbish I might add !) writing instrucions directly to the microprocessor registers.
      I look forward to seeing your video on this topic, if you make one, keep up the good work.
      Once again, thanks.

    • @RalphBacon
      @RalphBacon  7 ปีที่แล้ว

      I once fixed a Spectrum ZX81 for my boss at the time. It even had a wobbly RAM expansion pack. Or am I thinking of a ZX Spectrum? Seems so long ago now. I'm pretty sure I have not coded in pure machine code, I've always used assembler, so kudos to you that you managed to remember all the hex values! Yes, I've added the video idea to my backlog, you never know...

    • @m0gga
      @m0gga 7 ปีที่แล้ว +1

      Yes, that was the one I had, a ZX81 which was the first home computer to come onto the market, the Spectrum came some time after the XZ81 and had a coloured membrane keyboard. I also had the dreaded wobbly 16Kb expansion pack, many were the days when I had been up most of the night working on a stock taking application I was creating to use in my job, and that damn RAM pack would glitch and I would lose the whole damn lot!!! I did eventually finish the application and it saved me many many hours that I would normally have spent using a calculator.
      By the way, I have sorted how to show the clock whilst still scanning for button presses simply by declaring a Boolean flag that when set to true would allow the real time clock portion of the code to run, which would then disable the menu navigation buttons and vice-versa each time the clock button was pressed. The button scanning routine is a function call that returns a value, and the clock routine reside inside the loop but is skipped over if the enable flag is false. Easy peezey!
      Thanks again for pointing me in the right direction.

    • @m0gga
      @m0gga 7 ปีที่แล้ว

      Hi Ralph, it's me again. I have created a routine to set the time on the DS331 but I am having difficulty in passing a variable of type String to the setTheTime function. I have included the main part o the code below. The command that I am trying to process is setTheTime(stringTime), but it fails. My knowledge of C++ is a but limited, can you help?
      CODE-------------------------------------------------------------------------------------------------------
      #include
      #define BUFF_MAX 128
      void setup()
      {
      Wire.begin();
      uint8_t time[8];
      char recv[BUFF_MAX];
      unsigned int recv_size = 0;
      setTheTime("303917402022017"); // ssmmhhWDDMMYYYY set time once in the given format
      DS3231_init(DS3231_INTCN);
      memset(recv, 0, BUFF_MAX);
      lcd.begin(20, 4); // initialize the lcd - 20 columns, 4 rows
      lcd.backlight(); // Switch on the backlight
      }
      void loop()
      {
      char tempF[6];
      float temperature;
      char buff[BUFF_MAX];
      unsigned long now = millis();
      struct ts t;
      // if enabled show time once in a while
      if (showClock)
      {
      if (now - prev > interval)
      {
      DS3231_get(&t); //Get time
      temperature = DS3231_get_treg(); //Get temperature
      dtostrf(temperature, 5, 1, tempF);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print(t.mday);
      printLongMonth(t.mon);
      lcd.print(t.year);
      lcd.setCursor(0, 1); //Go to second line of the LCD Screen
      if (t.hour == 0)
      {
      lcd.print("0");
      }
      lcd.print(t.hour);
      lcd.print(":");
      if (t.min < 10)
      {
      lcd.print("0");
      }
      lcd.print(t.min);
      lcd.print(":");
      if (t.sec < 10)
      {
      lcd.print("0");
      }
      lcd.print(t.sec);
      lcd.print(' ');
      lcd.print(tempF);
      lcd.print((char)223);
      lcd.print("C ");
      lcd.setCursor(0, 3);
      lcd.print("Press clock to exit");
      prev = now;
      }
      }
      }
      void setTheTime(char *cmd)
      {
      struct ts t;
      // ssmmhhWDDMMYYYY set time format
      t.sec = inp2toi(cmd, 0);
      t.min = inp2toi(cmd, 2);
      t.hour = inp2toi(cmd, 4);
      t.wday = inp2toi(cmd, 6);
      t.mday = inp2toi(cmd, 7);
      t.mon = inp2toi(cmd, 9);
      t.year = inp2toi(cmd, 11) * 100 + inp2toi(cmd, 13);
      DS3231_set(t);
      }

  • @garymetheringham4990
    @garymetheringham4990 6 ปีที่แล้ว

    Hi, I've Been playing around with this script learning about writing the on-board chip directly instead of using a library (Like You say saving memory but not relevant yet), But the Question is How Do you set say bit 7 high without changing any other bits (possible unknown value) do i have to read the whole byte then write back the value + 128 or is there an easier way? (Sorry for the dumb newbee Question but i've not done any coding since BASIC on a C64 back in 1985-8)

    • @RalphBacon
      @RalphBacon  6 ปีที่แล้ว

      The way to change individual bits (when you have to write back a whole byte) is to use binary arithmetic. You are correct inasmuch that you do (sort of) add 128 but you do it by a binary OR (represented by ||). So, to make bit 7 high you would do something like:
      char x = x || 0b10000000
      which means if any bit is high (either in the char x OR in that hard-coded binary value then the value is 1). You don't want to change other bits so by comparing to binary 0 they will retain their original value, but bit 7 will always be set to 1.
      Example:
      0 1 0 0 0 1 0 1 - char x
      1 0 0 0 0 0 0 0 - binary hard coded comparison as above
      ----------------------
      1 1 0 0 0 1 0 1 - the final result
      Does this make any sense? Good question, maybe I should explain this in a video segment in the future for others.

    • @garymetheringham4990
      @garymetheringham4990 6 ปีที่แล้ว

      sort of. think a programming tutorial dealing with reading, writing bits to bytes with 1, a set of 8 leds and then putting that into writing to some chip to show it working. (I've learnt so much in the last month from your videos.) wish i'd found arduino years ago. Ebay must be loving me at the mo😂😂

    • @RalphBacon
      @RalphBacon  6 ปีที่แล้ว

      I'll certainly consider some sort of video in the future Gary. Glad you're learning a lot, it should be fun (and from what you're saying, quite expensive). Don't forget to "thumbs up" any video that you like! Also consider the Far Eastern sites such as Banggood.com, Gearbest.com and Aliexpress.com as you can save quite a bit. Many of the sellers on eBay are also present in the Aliexpress marketplace too. Thanks for posting.

    • @garymetheringham4990
      @garymetheringham4990 6 ปีที่แล้ว +1

      yeh im keeping the Chinese economy afloat single handed, ordered loads of different sensors so i hope you have also. some i havn't yet seen on your video list are laser sensor, colour recognition sensor, motion position sensor, gps, uv, ir temp sensor. all should be in uk any time now. keep up the good work looking forward to learning about the above.

    • @RalphBacon
      @RalphBacon  6 ปีที่แล้ว

      You and me both are keeping the Chinese economy going! Keep tuned and if I have not covered a particular sensor do let me know.

  • @willcalltickets
    @willcalltickets 6 ปีที่แล้ว

    Where do you get the information regarding the DS3231_I2C_ADDRESS? Is this different for every device? Help me understand where this number comes from :)

    • @willcalltickets
      @willcalltickets 6 ปีที่แล้ว

      Found the answer here at about the 12m30s mark
      th-cam.com/video/7_gCUZg8nAo/w-d-xo.html

    • @RalphBacon
      @RalphBacon  6 ปีที่แล้ว

      Glad you found it, Rob. Just be aware that these device addresses _may_ change in new batches (manufacturers do funny things), so one you had 10 years ago compared to one today may be different, although unlikely.

    • @willcalltickets
      @willcalltickets 6 ปีที่แล้ว

      Any idea how to handle a situation where I may be running two of these devices? I realize it may make more sense to just get a larger display, but I have 3 or so of these lcd displays lying around...hoping there is a way to easily address 3 on the same bus?

    • @RalphBacon
      @RalphBacon  6 ปีที่แล้ว

      You can hang up to 128 devices on an I2C bus as long as they all have unique addresses. Which, unfortunately, the DS3231 do not have, they all seem to have 0X68, although I have to question _why_ you need more than one on the bus? What are you trying to do here, Rob?

    • @willcalltickets
      @willcalltickets 6 ปีที่แล้ว +1

      Ralph S Bacon i just thought i May possibly want to display more info at one time . But I’ll workaround it

  • @drvlog4285
    @drvlog4285 6 ปีที่แล้ว

    Hey Ralph I got an issue with the time. the RTC module runs faster than it should. Like after some time, the RTC's time is coupe of seconds ahead than the computer's clock. how to fix that
    Thank y ou

    • @RalphBacon
      @RalphBacon  6 ปีที่แล้ว

      What module are you using Damith? The DS3231 is temperature compensated and should be very accurate. The DS1307 is not and is less accurate anyway (which is why I don't recommend it). Which one are you using?

    • @drvlog4285
      @drvlog4285 6 ปีที่แล้ว

      I'm using a DS 1307z. temperature is with in the limits

    • @drvlog4285
      @drvlog4285 6 ปีที่แล้ว

      got this from the web. but have mo idea how to fix it.
      'Fast Clocks
      The following are the most common scenarios that cause a crystal-based RTC to run fast.
      1) Noise coupling into the crystal from adjacent signals. This problem has been extensively covered
      above. Noise coupling usually causes an RTC to be grossly inaccurate.
      2) Wrong crystal. An RTC typically runs fast if a crystal with a specified load capacitance (CL) greater
      than the RTC-specified load capacitance is used. The severity of the inaccuracy is dependent on the
      value of the CL. For example, using a crystal with a CL of 12pF on an RTC designed with a 6pF CL
      causes the RTC to be about 3 to 4 minutes per month fast.'

    • @RalphBacon
      @RalphBacon  6 ปีที่แล้ว

      That's undoubtedly your problem then. The quality of some DS1307 boards (the design) is not up to manufacturer's specs, such as the capacitance used for the crystal, or there is a ground-plane opposite the crystal which increases the capacitance and slows down the frequency. They also lose or gain time as the temperature of its location goes up and down. Frankly, there is nothing you can do, realistically, to fix it. Buy a DS3231 and replace yours. The wiring is the same IIRC, or at the very least very similar (details will be in the video). That's the best advice I can offer!

    • @drvlog4285
      @drvlog4285 6 ปีที่แล้ว

      yeah thanks Ralph !!!

  • @ambidaearllawrencep.9174
    @ambidaearllawrencep.9174 4 ปีที่แล้ว

    2165/165/165 (Monday) 165:165:85
    since midnight 1/1/1970 = 1563147985s = 18091d
    now + 7d + 30s: 2019/7/22 12:16:31
    how to fix this

    • @RalphBacon
      @RalphBacon  4 ปีที่แล้ว

      I'm not sure I understand the problem you're describing here. Have you set the RTC to the correct date/time initially? Remember this is a one-time operation. As long as you don't remove the battery (or other power) it will continue to keep the correct date/time. Can you explain a bit more exactly what the problem is and what you're seeing?

  • @youpattube1
    @youpattube1 7 ปีที่แล้ว

    did you ever sort out the problem of D.S.T. ?

    • @RalphBacon
      @RalphBacon  7 ปีที่แล้ว +1

      +youpattube1
      Indeed I did, by writing my own code/library which I keep meaning to turn into a true, Arduino library so others can use it. Just requires two buttons to set the time, date, and knows all about DST (for the UK, but easily changeable for other parts of the world). I'll take this as a timely reminder and I will move it up the list so others can benefit too. Perhaps that is something I can do over the Xmas break (but I'm not at home for most of December so it might be a challenge... we shall see). Thanks for the reminder.

  • @umarmukhtarramadhan272
    @umarmukhtarramadhan272 7 ปีที่แล้ว

    can you help me why i'm not add library to arduino?

    • @RalphBacon
      @RalphBacon  7 ปีที่แล้ว

      To add a library (a folder containing files ending in ".h" and ".cpp" as well as other files) simply copy that library (folder) to the "libraries" sub-folder which is found under your sketches folder. Yes, too many folder! Then you can write commands such as #include and the compiler will search for it in the libraries (sub-folder). I hope this helps, but let me know if you still have problems (and what the exact problem is).

  • @sanb3586
    @sanb3586 5 ปีที่แล้ว

    Hello can you code something for me I am new

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      I'm afraid it doesn't work like that, San b. The idea is that you watch the videos, and follow along if it's something that interests you. That way, you learn how to do something, rather me doing your homework for you! There are plenty of videos here for newcomers - but you will have to put in the effort to watch and understand the content - there are no short-cuts!

    • @sanb3586
      @sanb3586 5 ปีที่แล้ว

      Well it's not like I didn't try but the code keep failing on me and I don't know about the coding thing thats why I am asking

    • @sanb3586
      @sanb3586 5 ปีที่แล้ว

      I watched every single video about my topic it's ether not related to my topic or I don't want to do that exactly

    • @sanb3586
      @sanb3586 5 ปีที่แล้ว

      And I have a code related to my topic but it's not complete some code is missing or wasn't there

    • @sanb3586
      @sanb3586 5 ปีที่แล้ว

      I was searching from last 8 days on TH-cam and Google

  • @Nawaz7861
    @Nawaz7861 7 ปีที่แล้ว

    sir add the code link for above video

    • @RalphBacon
      @RalphBacon  7 ปีที่แล้ว

      Hello Nawaz! Thanks for the reminder, I've updated the video description with a link to a GitHub repository with the sketch used in the video. Enjoy!

  • @Roy_Tellason
    @Roy_Tellason 4 ปีที่แล้ว

    Windows or mac? Some of us run Linux! :-)

    • @RalphBacon
      @RalphBacon  4 ปีที่แล้ว +1

      What's Linux? Never heard of it.

    • @Roy_Tellason
      @Roy_Tellason 4 ปีที่แล้ว

      @@RalphBacon Seriously? You don't know what you're missing out on... It's a *much* better operating system than windoze.

    • @Roy_Tellason
      @Roy_Tellason 4 ปีที่แล้ว

      I've run nothing else on my computers since 1999.

  • @george8bitsworth
    @george8bitsworth 6 ปีที่แล้ว

    Did you ever wonder why these RTC’s store the time-date values in BCD as opposed to straight binary? You must believe there is a practical reason for doing that.
    And there is a very practical reason for doing that. Note that when the Serial.print function displays a number like say 17 it must convert it first to essentially BCD (because it is stored in binary-0b00010001) and then do a logic OR on each BCD digit with 0x30 (to convert it to ASCII).
    Now if you use the Serial.print function to print a hexadecimal number, the number doesn’t have to be converted to BCD. All that has to be done is to separate the digits (with a couple of OR’s and AND’s and Shifts)-no multiplying or dividing-and then send as ASCII to the serial port.
    Now to be fair I haven’t seen any video on TH-cam about the RTC’s where the hexadecimal values are utilized. The all convert the BCD back to binary (note: that is what your “bcdToDecimal(byte val)” function does). In fact, on that point, the DS1307 library for the Arduino also converts the BCD values to binary.
    It is very simple displaying the BCD values from the RCT’s.
    In your sketch you display the seconds value with the line:
    Serial.print(second, DEC);
    Had the value of seconds not been converted from BCD you would simply use the function:
    Serial.print(second, HEX); to display the value.

    • @RalphBacon
      @RalphBacon  6 ปีที่แล้ว

      Exactly so, #captrox. BCD is useful many times (not just to save storage space!). But I didn't realise that Serial.print also made use of this feature though. But RTCs do, of course and I explore this further in video #8 "Simple Demo of BCD". Thanks for that information #captrox, it's that sort of background knowledge that can really help us Arduinites understand what's going on 'behind the scenes' rather than blinding using some helper method. Thanks for posting.

  • @muppetpaster
    @muppetpaster 5 ปีที่แล้ว

    ds3231 costs about 0.65 euro

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      This is the current price? If I mentioned a price in this video that was from 3 years ago now so I would not be surprised if they are now cheaper.

  • @whoopy15d
    @whoopy15d 2 ปีที่แล้ว +1

    Hi Raplh,
    Thanks for this code. I have been strugeling with this for months. Finnaly a code that works.
    I just have one problem and was hoping you good help me. I am not new with programming, but I am new with arduino
    I want to save the time to a SD card.
    I am using this code, where i save the distance. That works fine
    // if the file opened okay, write to it:
    if (myFile) {
    //Serial.print(distance);
    myFile.print("Koe aanwezig: ");
    myFile.println(distance);
    myFile.close();
    }
    But I get an error if i want to place the time in there. something like this:
    myFile.print(displayTime();)
    Is there a simple way to at the time in my file on the same line.?
    Thnaks for your help

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      The time variable is something special. You probably need to extract the hour and minutes and save them as separate strings (or, possibly, integers).