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
}