DS3231 log to NVRAM

Hi there and may i say Arduino is awesome.

So, I'd like to learn the in's and outs of programming an arduino and I have begun this journey with a
Nano and a DS3231 RTC.

Currently I am using the ds3231's built in thermometer to take arbitrary measurements just to get a feel for how this would work, and it's working well

What I'd like to ask is, does anyone have a link to a good example of using the temperature sensor to log temperature to the nvram and then later download that to the PC via serial using a python script or java ..?

I have aconcept of how to write in general to the NVRAM, but I'm not familiar at all with C so it's gonna take some learning on my behalf but any help on the programming of the arduino writing to the NVRAM would be greatly appreciated...

Cheers

// DS3232 I2C NVRAM test
#include "Wire.h"
#define DS3232_I2C_ADDRESS 0x68

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

void writeNVRAM(byte data, byte location)
// writes data to location
{
  Wire.beginTransmission(DS3232_I2C_ADDRESS);
  Wire.write(location); 
  Wire.write(data);
  Wire.endTransmission();  
}

byte readNVRAM(byte location)
// retrieves data from DS3232 NVRAM location
{
  byte result;
  Wire.beginTransmission(DS3232_I2C_ADDRESS);
  Wire.write(location); 
  Wire.endTransmission();  
  Wire.requestFrom(DS3232_I2C_ADDRESS, 1);
  result = Wire.read();
  return result;
}

int a;
byte b;

void loop()
{
  Serial.println("Writing ...");
  for (a=20; a<256; a++)
  {
    writeNVRAM(a,a);
  }
  Serial.println("Reading ... ");
  for (int a=20; a<256; a++)
  {
    b=readNVRAM(a);
    Serial.println(b, DEC);
  }
  delay(2000);
}

According to the datasheet the DS3231 has zero memory.

Some DS3231 modules have a NVRAM built on, post a link to the module and the part number on the ram chip if it has one.
Mine has an Atmel 24C32 EEPROM, but I've no clue how to use it. Google "DS3231 nvram".
https://www.mouser.com/datasheet/2/268/doc0336-1180695.pdf

The details of the 24C32 Serial EEPROM with the DS3231 RTC Module.

The DS3231 (the chip) has no memory available for the user. If RTC module/board has additional chip like AT24C32 it is another story. On I2C bus, it is autonomous unit with self I2C address.

Thanks for the replies, it seems to me there is some confusion about which model has the nvram and which ones don't.

The sketch I posted works, but I don't yet know enough to be able to modify it. I want to accomplish this with wire because it's a very handy tool to know... :slight_smile:

So does any one know how I might point the 3231 to write the time and temp every hour and then later download those stats through serial? I think it would be a handy thing to understand and have examples for when others come looking too... :slight_smile:

Also many thanks GolamMostafa for the graphic, it's a very nice summary of the setup.. (I'm using a nano through the usb and only the ds3231 onboard temp sensor)

And thanks also Outsider for the days sheet.. Interesting.... Ish

No more input? Seems like a fairly standard RTC, so it's handy to know how the nvram could be written to...

andyfof:
So does any one know how I might point the 3231 to write the time and temp every hour and then later download those stats through serial? I think it would be a handy thing to understand and have examples for when others come looking too... :slight_smile:

As pointed out in Replies #3 and #4, the RTC and EEPROM devices are two independent entities that just happen to be mounted on the same PWB. They also appear to live on the same I2C bus, at different addresses. Presumably, they are both I2C slave devices. Therefore, you can't "point the 3231 to write to the EEPROM". You can, however, write code for your processor (Arduino Nano, the I2C master) to read the information from the RTC and then write it to the EEPROM.

Acquiring Temperature Signal from DS3231 RTC Chip and storing it into 24C32 EEPROM

(1) If using the DS3231 RTC Module, we have 24C32 EEPROM on that module with the following schematic diagram. The I2C address of the EEPROM is: 0x57.
ee32.png

(2) If we are not using the dS3231 RTC Module, then we have to place a 24C32 EEPROM chip on the breadboard and connect it with the Arduino UNO as per following diagram. The I2C address of the EEPROM is: 0x57
ee32x.png

(3) Codes to test the EEPROM (Write a data byte 0x12 at location 0x0234 and read it back and show on Serial Monitor).

#include <Wire.h>
#define slaveAddress 0x57
#define eepAddress 0x0234
#define databyte 0x12
void setup()
{
  Serial.begin(9600);
  Wire.begin();    //TWI Bus is formed at default speed

  Wire.beginTransmission(slaveAddress);    //
  Wire.write(highByte(eepAddress));       //0x02
  Wire.write(lowByte(eepAddress));        //0x34
  Wire.write(databyte);                   //0x23
  Wire.endTransmission();                 //all steps: a - g done 
  delay(5);         //write cycle time of EEPROM is about 5 ms.
  
  //---------------------------------------------------------------------
  Wire.beginTransmission(slaveAddress);    //Roll call; 
  Wire.write(highByte(eepAddress));       //0x02  pointing EEPROM address
  Wire.write(lowByte(eepAddress));        //0x34
  Wire.endTransmission();                 //all steps: a - g done 
  //---------------------------------------------------------------------
  Wire.requestFrom(slaveAddress, 1); //request to slave 1-byte data to read
  byte x = Wire.read();              //data is read from unseen buffer into x 
  //---------------------------------------------------------------------
  Serial.println(x, HEX);             //show data on Serial Monitor; should be 12
} 

void loop()
{

}

(4) Codes to Read temperature signal from DS3231 RTC; store it into 24C32 EEPROM; read back the temperature from EEPROM and show it on Serial Monitor.

#include<Wire.h>                //this file comes with IDE; it is needed for TWI Bus operation
#define deviceAddress 0b1101000 //address of the DS3231 RTC Chip = 0x68
int eepromAddress = 0x0234;
byte x;                         //global variable

union
{
  float tempC;
  byte myTemp[4];
} myData;

void setup()
{
  Serial.begin(9600);
  Wire.begin();       //TWI (aka I2C Bus) Bus is formed
}

void loop()
{
  Wire.beginTransmission(deviceAddress); //START, deviceAdr, data direction (write) are queued
  Wire.write(0x0E);                     //Control Register Address is queued
  Wire.write(0x20);                     //Start Temperature Conversion command byte is queued
  Wire.endTransmission();               //queued information are transferred on ACK

  do
  {
    Wire.requestFrom(deviceAddress, 1);   //command to the slave to send 1-byte data
    x = Wire.read();                      //data is read from FIFO buffer
  }
  while (bitRead(x, 5) != LOW);           //bit-5 of Control Register is LOW indicates end-of-conversion

  //--- read Temperature Signal----------------
  Wire.beginTransmission(deviceAddress);  //
  Wire.write(0x11);                       //point higher byte of Temperature Register; queued
  Wire.endTransmission();                 //transfer the above queued information

  Wire.requestFrom(deviceAddress, 2);       //request to send 2-byte data of Temperature Register
  float tempUpper = (float)Wire.read();    //integer part is saved as: XX.00000…….
  byte tempLower = Wire.read();            //fractional part as: XX000000

  float tempLowerx = (float) ((tempLower >> 7) * 0.5 + ((tempLower >> 6) & 0x01) * 0.250);
  float temp = tempLowerx + tempUpper;
  Serial.println();
  Serial.println("===Reading Temp from DS3231 RTC Module");
  Serial.println(temp, 2);
  myData.tempC = temp;  //savig into union
  //Serial.println(myData.tempC, 2);

  //---save temp into EEPROM starting at location 0x0120-----
  Serial.println("===Write and Read Temp to/from EEPROM======");
  Wire.beginTransmission(0x57);    //EEPROM I2C address 0x57;
  Wire.write(highByte(eepromAddress));//pointing EEPRPM location address
  Wire.write(lowByte(eepromAddress));
  Wire.write(myData.myTemp, sizeof(myData.myTemp));
  Wire.endTransmission();
  delay(5);   //write cycle delay
  //-----read back------
  Wire.beginTransmission(0x57);    //EEPROM I2C address 0x57;
  Wire.write(highByte(eepromAddress));//pointing EEPRPM location address
  Wire.write(lowByte(eepromAddress));
  Wire.endTransmission();
  //--------------------
  Wire.requestFrom(0x57, 4);
  myData.myTemp[0] = Wire.read();
  myData.myTemp[1] = Wire.read();
  myData.myTemp[2] = Wire.read();
  myData.myTemp[3] = Wire.read();
  Serial.println(myData.tempC, 2);
  eepromAddress = eepromAddress + 4; //next EEPROM location
  delay(2000);
}

ee32y.png

ee32.png

ee32x.png

ee32y.png

@Golam
You could get inspired by Nick Gammon and create your own web pages. I guess you have enough of material.