1

I have a DS3231 Real Time Clock module and I'd like to use the Square Wave output.

I wonder how can I set the frequency of the pulse?

How long is the (time) length of the pulse?

UPDATED QUESTION

In order to use the DS3231 as an 1PPS interrupt, I want to use the Square Wave output. Here's how I connected it to an Arduino.

enter image description here

I connected to the SQW output a 10kΩ pull-up resistor and an LED. After that I tested the code which shows the time and worked fine.

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;

void setup () {
   Serial.begin(9600);
   #ifdef AVR
      Wire.begin();
   #else
      Wire1.begin(); 
   #endif
   rtc.begin();

  if (! rtc.isrunning()) {
     Serial.println("RTC is NOT running!");
     rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    }  
  }

void loop () {

   DateTime now = rtc.now();
   Serial.print(now.year(), DEC);
   // etc
   }

(The DS3231 works fine with the DS1307 library)

In this case the LED was just flickering, so I thought that the default frequency of the SQW should be grater than 1Hz. I googled a bit and realized that I had to change some registers. So I found this piece of code which I called from the setup() function, but with no success, the LED is still flickering.

void set1Hz() {
   // Frequency is stored in register 0x0e in bit 3 and 4
   Wire.beginTransmission(0x68);
   Wire.write(0x0e);
   Wire.endTransmission();
   Wire.requestFrom(0x68, 1);
   uint8_t register0E = Wire.read();

   // clear bits 3 and 4 for 1Hz
   register0E &= ~(1 << 3);
   register0E &= ~(1 << 4);

   // put the value of the register back
   Wire.beginTransmission(0x68);
   Wire.write(0x0e);
   Wire.write(register0E);
   Wire.endTransmission();
 }

Since I'm now qualified for low level programming, any suggestions on how to change the registers would be appreciated.

2

3 Answers 3

2

Even though it is unusual for someone to answer his/her own question, I did some more digging and managed to solve the problem. So, I'd like to mention one or two items in order to help someone who may encounter the same problem.

  • I used the Jack Christensen's library https://github.com/JChristensen/DS3232RTC, but I had to update the Time.h library.

  • In the setup() I add this line in order to enable the SQW output and set it to 1PPS.

    RTC.squareWave(SQWAVE_1_HZ);     
    

    Other options are SQWAVE_1024_Hz, SQWAVE_4096_Hz, SQWAVE_8192_Hz and SQWAVE_NONE

  • Using the LED and resistor (390Ω) as shown in the figure above, it works as a voltage divider. In that case the voltage between SQW output and Ground didn't exceed 2.0V. Thus someone should be careful if wants to have an LED indicator and wants to use SQW output as interrupt.

1
  • Please remember to mark your answer as solution in 3 days ;) May 5, 2015 at 8:02
0

with DS1307 Tiny RTC library:

"rtc.writeSqwPinMode(0x10);" in the setup loop...

sets DS output to 1 Hertz, squarewave

Its not exactly 1PPS signal, which is typically a narrow pulse,

but this 50% duty cycle signal will do as a 1 second interrupt.

0

SQWV output only pulls low, it is an open drain output.

Wire up like this: 5V to LED anode, LED cathode to current limit resistor, other leg of resistor to SQWV pin.

Check the current capability of the pin. 10K will make very dim (if seen at all) LED. Try 1K.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.