RTC not detected on Teensy 4.0

rebelbot

New member
Greetings.

Usually I never have problems with the Teensys but I'm helping someone on this new project and we are noticing some issues when setting the RTC. Does it need a battery attached explicitly even when powered?

I went back and used the example code to check it:


#include <DS1307RTC.h>
#include <Time.h>
#include <Wire.h>

void setup() {
Serial.begin(9600);
while (!Serial) ; // wait for serial
delay(200);
Serial.println("DS1307RTC Read Test");
Serial.println("-------------------");
}

void loop() {
tmElements_t tm;

if (RTC.read(tm)) {
Serial.print("Ok, Time = ");
print2digits(tm.Hour);
Serial.write(':');
print2digits(tm.Minute);
Serial.write(':');
print2digits(tm.Second);
Serial.print(", Date (D/M/Y) = ");
Serial.print(tm.Day);
Serial.write('/');
Serial.print(tm.Month);
Serial.write('/');
Serial.print(tmYearToCalendar(tm.Year));
Serial.println();
} else {
if (RTC.chipPresent()) {
Serial.println("The DS1307 is stopped. Please run the SetTime");
Serial.println("example to initialize the time and begin running.");
Serial.println();
} else {
Serial.println("DS1307 read error! Please check the circuitry.");
Serial.println();
}
delay(9000);
}
delay(1000);
}

void print2digits(int number) {
if (number >= 0 && number < 10) {
Serial.write('0');
}
Serial.print(number);
}



Get the following output on the monitor:

DS1307RTC Read Test
-------------------
DS1307 read error! Please check the circuitry.


i2c basically shows a request for the device 0x68 but no response.

I'm sure there is something being overlooked but would appreciate input.

Rebelbot
 
RTC on T_4.x is inbuilt hardware - not a DS1307 device.
See examples - perhaps this one?: {local install}\hardware\teensy\avr\libraries\Time\examples\TimeTeensy3\TimeTeensy3.ino
 
And assuming you are using a DS1307 wired to the Teensy, the datasheet says this:

"If a backup supply is not required, VBAT must be grounded. "
 
Thanks for both suggestions. I don't know the history of the project yet to know if they perhaps switch devices over time and this is an open transition issue.
 
Code:
#include <DS1307RTC.h>
#include <Time.h>
#include <Wire.h>

void setup() {
	Serial.begin(9600);
	while (!Serial); // wait for serial
	delay(200);
	Serial.println("DS1307RTC Read Test");
	Serial.println("-------------------");
}

void loop() {
	tmElements_t tm;

	if (RTC.read(tm)) {
		Serial.print("Ok, Time = ");
		print2digits(tm.Hour);
		Serial.write(':');
		print2digits(tm.Minute);
		Serial.write(':');
		print2digits(tm.Second);
		Serial.print(", Date (D/M/Y) = ");
		Serial.print(tm.Day);
		Serial.write('/');
		Serial.print(tm.Month);
		Serial.write('/');
		Serial.print(tmYearToCalendar(tm.Year));
		Serial.println();
	}
	else {
		if (RTC.chipPresent()) {
			Serial.println("The DS1307 is stopped. Please run the SetTime");
			Serial.println("example to initialize the time and begin running.");
			Serial.println();
		}
		else {
			Serial.println("DS1307 read error! Please check the circuitry.");
			Serial.println();
		}
		delay(9000);
	}
	delay(1000);
}

void print2digits(int number) {
	if (number >= 0 && number < 10) {
		Serial.write('0');
	}
	Serial.print(number);
}
In future when posting code for help could you please enclose it between CODE tags using the # button which is on the top line when creating a help request.
It makes it so much easier to understand your code and is good etiquette.
 
One other thing to think of is if you are using an I2C real time clock, depending on the whim of the manufacturer, the clock may have pull-up resistors on the I2C pins. In general, newer Adafruit and Sparkfun I2C devices will have pull-up resistors, but older devices typically did not have a pull-up resistor. If you use the PJRC audio shield or prop shield, those devices have pull-up resistors. You only need one set of pull-up resistors on the bus.

I find myself always adding 2.2K pull-up resistors to both SCL and SDA pins every time I wire up a Teensy, even if I don't immediately plan to use an I2C device. A pull-up resistor is a parallel connection between the data pin and power (i.e. 3.3v). On 3.3v systems, typically 2.2K resistors are good enough. On 5v systems, 4.7K resistors are the norm. Some devices have 10K resistors. The downside is the more pull-up resistors you have on the bus, can make the I2C bus slower.
 
Back
Top