DS3231 display Correct Temperature

Hello Everyone,

I am trying to display the correct temperature with my RTC module. The time keeping is correct, but temperature is printing the same over and over. The code is below:

#include "Wire.h"
#define DS3231_I2C_ADDRESS 0x68

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val){
  return ((val/10*16) + (val%10));
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val){
  return ((val/16*10) + (val%16));
}

void setup(){
  Wire.begin();
  Serial.begin(9600);

  // Set the initial time here:
  // DS3231 second, minute, hour
  // setDS3231time(0, 0, 0, 0, 35, 51, 12); // This section comment out after uploading
}

void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year){
  // Sets time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0x0E); // Select register
  Wire.write(0b00011100); // Write register bitmap, bit 7 is /EOS
  Wire.write(decToBcd(second)); // Set seconds
  Wire.write(decToBcd(minute)); // Set minutes
  Wire.write(decToBcd(hour)); // Set hours
  Wire.write(decToBcd(dayOfWeek)); // Set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // Set date (1 to 31)
  Wire.write(decToBcd(month)); // Set month
  Wire.write(decToBcd(year)); // Set year (0 to 99)
  Wire.endTransmission();
}

void readDS3231time(byte *second, byte *minute, byte *hour, byte *dayOfWeek, byte *dayOfMonth, byte *month, byte *year){
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // Set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7); // Request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}

float readTemperature(){
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0x11); // Set DS3231 register pointer to temperature MSB
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 2); // Request two bytes of temperature data
  byte msb = Wire.read();
  byte lsb = Wire.read();
  byte tempH = ((msb << 2) | (lsb >> 6)); // High byte of temperature
  byte tempL = ((lsb & 0x7F) << 1); // Low byte of temperature
  float temperature = (tempH + tempL * 0.25); // Combine high and low bytes and convert to Celsius
  return temperature;
}

void displayTimeAndTemperature() {
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // Retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  
  // Send the time to the serial monitor vertically
  Serial.print(String(hour, DEC));
  if (minute < 10) {
    Serial.print("0");
  }
  Serial.print(String(minute, DEC));
  if (second < 10) {
    Serial.print("0");
  }
  Serial.print(String(second, DEC));
  
  // Retrieve temperature from DS3231
  float temperature = readTemperature();
  
  // Convert Celsius to Fahrenheit
  float temperatureF = (temperature * 9 / 5) + 32;
  
  Serial.print(" Temperature: ");
  Serial.print(temperatureF);
  Serial.println("°F");
}

void loop(){
  displayTimeAndTemperature(); // Display the real-time clock data and temperature on the Serial Monitor
  delay(1000); // Delay for one second
}

Have You tested any example code for this device?
Schematics would be useful and maybe powering.
A link to the datasheet of the RTC would be fine. Surprised that an RTC measures time.

Yes I have. This is based on the example code

Good. What does the pure example code show?
I built a project based on example code and it turned out very well. Why doesn't Your project do that? We'll see. Likely You changed some key line/lines and the code is out of order now.

I'm not sure your calculation reflects the structure of the temperature registers. The datasheet says:

Temperature Registers (11h–12h)
Temperature is represented as a 10-bit code with a
resolution of 0.25°C and is accessible at location 11h and
12h. The temperature is encoded in two’s complement
format. The upper 8 bits, the integer portion, are at location 11h and the lower 2 bits, the fractional portion, are in
the upper nibble at location 12h. For example, 00011001
01b = +25.25°C. 

Also, if you will be experiencing negative C temps, then I think you would want to read the register values as int8_t values. Byte is unsigned.

Anyway, see if you can get your algorithm to produce the +25.25 degrees result shown above for 00011001b and 01000000b

Edit: Since these registers are supposed to be two's complement, I assume -25 degrees would have 11100111b in reigister 0x11. But what would -25.25 degrees look like? Presumably the same in 0x11, but what about 0x12? Would it be 01000000b, or 11000000b?

Question was....did you actually USE the example code to test.??

From https://github.com/NorthernWidget/DS3231/blob/master/DS3231.cpp

float DS3231::getTemperature() {
	// Checks the internal thermometer on the DS3231 and returns the
	// temperature as a floating-point value.

  // Updated / modified a tiny bit from "Coding Badly" and "Tri-Again"
  // http://forum.arduino.cc/index.php/topic,22301.0.html

  byte tMSB, tLSB;
  float temp3231;

  // temp registers (11h-12h) get updated automatically every 64s
  _Wire.beginTransmission(CLOCK_ADDRESS);
  _Wire.write(0x11);
  _Wire.endTransmission();
  _Wire.requestFrom(CLOCK_ADDRESS, 2);

  // Should I do more "if available" checks here?
  if(_Wire.available()) {
    tMSB = _Wire.read(); //2's complement int portion
    tLSB = _Wire.read(); //fraction portion

    int16_t  itemp  = ( tMSB << 8 | (tLSB & 0xC0) );  // Shift upper byte, add lower
    temp3231 = ( (float)itemp / 256.0 );              // Scale and return
  }
  else {
    temp3231 = -9999; // Impossible temperature; error value
  }

  return temp3231;
}