Easy Real Time Clock Interrupt - How to Use the DS3231 SQW Pin

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 พ.ค. 2024
  • In this video, I'll be talking about the DS3231 real time clock. Specifically, I'll explain how to use the square-wave output from the real time clock as an external interrupt signal for the Arduino to refresh its date and time settings once every second.
    Link to breadboard layout and Arduino sketch: github.com/DuranioMakes/DS323...

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

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

    Very useful information about interrupts. I believe that's the best way to use this module.

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

    Useful info for my project, thanks!!

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

      Glad it was helpful!

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

    Thank you so much for so nice example. I have one project with the rtc and in it I have to use interruption because I need to triger Arduino UNO (my master) to read some information from slaves with period 10 seconds. The example help me a lot.

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

      @Vitaliy Reznikov - Great! I'm glad you found this video usefull. Happy programming!

  • @asMdl
    @asMdl 3 หลายเดือนก่อน +1

    Nice work!

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

    Question I am working on a project using this board an I have the components but what I wanna accomplish is I'm doing a traffic light set up using a 4 way intersection an traffic lights going down at each entersection ok so at a certain time I wanna have my yellow/ amber leds on my traffic lights to go on caution at a certain time and then go back to working order after that time is over. So basically the Arduino is still working real time an then going to flash at another time. So let's say working order traffic lights 12pm - 5pm. Then go on caution at 5:05pm and back to working order at 9:00pm.

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

    Thanks for your video on use of interrupt. Could you post a link to your modified sketch?

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

      Sure! I just posted the sketch on GitHub along with a copy of the breadboard layout as JPEG and Fritzing files. Here is the link:
      github.com/DuranioMakes/DS3231_RTC_SQW_1Hz_Interrupt
      Thanks for watching!

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

    Thank you 𝘿𝙪𝙧𝙖𝙣𝙞𝙤 for the instruction. Very helpful.
    A remark though: Your Arduino is now sitting there and waiting for the interrupt trigger and to print out the date.
    Wouldn't it make sense to include the 𝚍𝚒𝚜𝚙𝚕𝚊𝚢𝚃𝚒𝚖𝚎 routine in the ISR, so that the 𝚕𝚘𝚘𝚙 can take care of other things rather than being trapped in the 𝚒𝚏 (𝙴𝙳𝙶𝙴 == 𝟶) loop?
    If a longlasting routine would be running in the 𝚕𝚘𝚘𝚙, despite the interrupt, the time would not be updated regularly.
    Or do you have a specific reason, why you only change a volatile variable in the ISR?

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

      Hello value 1 - So for an interrupt service routine (ISR), the general rule is to keep them as short as possible. They should not contain parameters or routines, such as, "displayTime". It is also recommended that shared variables between the main progrm and ISR are declared as volatile. More information on interrupts can be found here: www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
      and here: gammon.com.au/interrupts

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

    Thank you, great info, useful for my project. .I have design a cock and using pin no 2 Arduino uno for segment A can i use pin no 12 for sqw pin out for secnd showing 2 leds and other thing I do no want to use the led conected with SDA pin can i remove it .

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

      Hello Sukhbir Singh - You can remove the LED connected to the SDA pin with no problems. It is not required. If I am understanding your other question about pin 12, you should be able to drive a second LED with it as long as you use a 220 or 330 Ohm resistor. It sounds like you want to light this LED every second?

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

    Hi, thank you for the tutorial How can i edit the sketch to run on a Mega?

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

      Hi Aubrey - This should be relatively easy. In the Arduino IDE program, you will need to go to "Tools" and then scrool down and select the Arduino Mega you have from the list of options. That should be all you need to do, unless you are using a different Mega pin to monitor the SQW output. If so, you need to change the SQWinput pin in the sketch to match the Mega pin you are using. Make sure it is an interrupt capable pin (2, 3, 18, 19, 20 or 21).

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

    Thank you, great info, useful for my project.
    I do have a question though. your code mentions You are using this to enable pull-up resistor.
    pinMode(SQWinput, INPUT);
    digitalWrite (SQWinput, HIGH);

    Isn't it easier to do a simple
    pinMode(SQWinput, INPUT_PULLUP);
    instead?

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

      Good question! I have not tried that. It would be easy to experiment with making that change in the program to see what happens. If no errors are reported, it probably works.

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

      @@DuranioLabs Thank you, I tried it and seems to have worked.
      I use SQWinput to trigger the alarm, but otherwise works fine.

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

      @mihaisor Glad that worked!

  • @mr.google1463
    @mr.google1463 ปีที่แล้ว

    Great, thank you so much for the video. Sir, my RTC is not sending interrupt while on coin cell but it worked fine on 3.3 or 5V Vcc. can you help me please.

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

      Interesting. I have not tried running the the RTC on the coin celll alone. The DS3231 datasheet mentions a 0.1uF decoupling capacitor should be placed in parallel with the coin cell if it is the primaty power source. I would need to investigate this further.

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

      It's complicated. I tried this yesterday and failed. First RTC is not powered by a coin cell, where there is power on VCC. When you disconnect that power putting it for example on arduino output pin and setting it low, it instantly triggers interrupt cause SQW/INT pin goes from high to high impedance. Then there's a chance that RTC may be powered via data pins.And when I measured current with SDA and SCL disconnect, it was 600uA, whereas with connection to Arduiono, it draws something like 90uA. It's not horrible.
      Also note there's a BBSQW enable bit in either config or status register (battery backed square wave).

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

    Thank you for sharing this information.
    I just want to ask you that can we make frequency less than zero like 0.0002 etc in line of code "rtc.writeSqwPinMode(DS3231_SquareWave1Hz)" so that we can have falling edge every hour?

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

      The slowest frequency setting available for the SQW pin is 1Hz but you can accomplish what you want a different way. If your project still requires a real time clock, you can test the state of minutes and seconds and change the state of an Arduino pin when minutes and seconds both equal 00.
      For example you can add the following lines to the Arduino sketch.
      1) At the beginning of the sketch, assign an Arduino pin for the new output signal.
      const int PIN = 3; // Arduino pin 3 called "PIN". You can rename it if you'd like
      2) Within setup, "void setup ()" add the following lines:
      pinMode(PIN, OUTPUT); //Set the new Arduino pin (pin 3) as an output
      digitalWrite(PIN, HIGH); //Set the initial output state of PIN as HIGH
      3) Within "void displayTime ()" add the following:
      if (now.minute() == 00 && now.second() == 00){
      digitalWrite(PIN, LOW); //If minutes are 00 and seconds are 00, it's the top of the hour so set PIN low
      }
      if (now.minute() == 00 && now.second() == 30){
      digitalWrite(PIN, HIGH); //After 30 seconds, set PIN high
      }
      You can add an LED and 220 Ohm resistor to the output to test the changes.
      Good luck!

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

    Nice info sir, but can we calculate the RTC DS3231 to get the time in millisecond?

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

      *calculate = set

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

      Good question. The DS3231 does not have resolution below seconds, but a counter could be written in the software to provide counts down to the millisecond, while still being reset every 1,000msec by the 1Hz squarewave.

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

      @@DuranioLabs could u provide the tutorial or the steps? I really have no idea. This is for my final project on university:( i'll be thankful for the tutorial

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

      @DONNY ALI SANJAYA - So I think an even better way to count down to milliseconds with an Arduino is to use the millis() function. The millis() function returns the number in milliseconds since the board began running its program. A description of millis() is here:
      www.arduino.cc/reference/en/language/functions/time/millis/
      And a detailed description of it use can be found here:
      th-cam.com/video/qn8SP93L3iQ/w-d-xo.html
      Anyway, I'm not sure if you're building a real time clock or something more like a stopwatch with Arduino. If you need time expressed down to the millisecond, then the millis() function is where you should start.

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

      @@DuranioLabs Got it! I'll try with milliseconds. Thx for the explanation sir! Really apreciate it!

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

    What is the DEC variable in the code for?

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

      DEC is for decimal and instructs the Arduino to print the value in decimal (base 10) format. I found that it is redundant and you could just leave it out. For example:
      Serial.print(now.second(), DEC);
      can be changed to:
      Serial.print(now.second());
      and it will still work as expected. You can change the DEC to HEX which displays the value in hexidecimal (base 16) just to see what happens.
      Have Fun!

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

      @@DuranioLabs thank you so much for replying! I been trying to understand this part for a while, thank you!

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

    Arduino se 3231 को। केसा बताऐ। ओर बेकीग। करे।