Hello guys and gals.
I have a really strange problem with the aforementioned components. Here's the datasheet for the
DS3231 (from here: the rtc).
I hand soldered the rtc on an adapter board in order to experiment with it using a bluepill, and I hooked it up on the i2c bus.
I use the arduino IDE to program the thing and I wanted to try the RTCLib from adafruit. So I loaded up the basic sketch (the only change I made is the while loop and was suggested to me to circumvent some usb problems).
/* Example implementation of an alarm using DS3231
*
* VCC and GND of RTC should be connected to some power source
* SDA, SCL of RTC should be connected to SDA, SCL of arduino
* SQW should be connected to CLOCK_INTERRUPT_PIN
* CLOCK_INTERRUPT_PIN needs to work with interrupts
*/
#include <RTClib.h>
#include <Wire.h>
RTC_DS3231 rtc;
// the pin that is connected to SQW
#define CLOCK_INTERRUPT_PIN 26
void setup() {
Serial.begin(9600);
while(!Serial)
delay(1);
// initializing the rtc
if(!rtc.begin()) {
Serial.println("Couldn't find RTC!");
Serial.flush();
abort();
}
if(rtc.lostPower()) {
// this will adjust to the date and time at compilation
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
//we don't need the 32K Pin, so disable it
rtc.disable32K();
// Making it so, that the alarm will trigger an interrupt
pinMode(CLOCK_INTERRUPT_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(CLOCK_INTERRUPT_PIN), onAlarm, FALLING);
// set alarm 1, 2 flag to false (so alarm 1, 2 didn't happen so far)
// if not done, this easily leads to problems, as both register aren't reset on reboot/recompile
rtc.clearAlarm(1);
rtc.clearAlarm(2);
// stop oscillating signals at SQW Pin
// otherwise setAlarm1 will fail
rtc.writeSqwPinMode(DS3231_OFF);
// turn off alarm 2 (in case it isn't off already)
// again, this isn't done at reboot, so a previously set alarm could easily go overlooked
rtc.disableAlarm(2);
// schedule an alarm 10 seconds in the future
if(!rtc.setAlarm1(
rtc.now() + TimeSpan(10),
DS3231_A1_Second // this mode triggers the alarm when the seconds match. See Doxygen for other options
)) {
Serial.println("Error, alarm wasn't set!");
}else {
Serial.println("Alarm will happen in 10 seconds!");
}
}
void loop() {
// print current time
char date[10] = "hh:mm:ss";
rtc.now().toString(date);
Serial.print(date);
// the value at SQW-Pin (because of pullup 1 means no alarm)
Serial.print(" SQW: ");
Serial.print(digitalRead(CLOCK_INTERRUPT_PIN));
// whether a alarm happened happened
Serial.print(" Alarm1: ");
Serial.print(rtc.alarmFired(1));
// Serial.print(" Alarm2: ");
// Serial.println(rtc.alarmFired(2));
// control register values (see [url]https://datasheets.maximintegrated.com/en/ds/DS3231.pdf[/url] page 13)
// Serial.print(" Control: 0b");
// Serial.println(read_i2c_register(DS3231_ADDRESS, DS3231_CONTROL), BIN);
// resetting SQW and alarm 1 flag
// using setAlarm1, the next alarm could now be configurated
if(rtc.alarmFired(1)) {
rtc.clearAlarm(1);
Serial.println("Alarm cleared");
}
delay(2000);
}
void onAlarm() {
Serial.println("Alarm occured!");
}
Please do mind that the bluepill has worked fine so far so I have no reason to suspect it not working.
The problem is that upon starting the program I get the "Couldn't find RTC!" message.
I analyzed the i2c bus activity and I clearly see that the pill correctly tries to write to address 0x68 during the RTC begin function, but the message ends with a NAK.
I then manually checked all of the connections and soldering and can confirm (with two multimeters, via buzzing) that:
- pins 5 to 13 are connected to GND
- pin 2 is connected to VCC
- Pin 16 is connected to SCL
- pin 15 is connected to SDA
- every pin on the chip buzzes with the corresponding pin on the breakout board
I also have confirmed that pin 4 goes to VCC without pulling it up (as expected).
I have no activity on pin 1.
I seriously don't know what else to check. Is my rtc defective? Am I so unlucky to have 2 defective components in the same week? (the other one was an eeprom).
thanks for anyone who's willing to help!