Address counter with Wire.h

Hey guys,

I'm figuring out how to communicate to an RTC module and can't find much information when it comes to setting the address from which I will read and write. I found
this article which first writes the memory address to the i2c device, and then the data. Tutorial: How to Configure I²C Sensors with Arduino Code | Underwater Arduino Data Loggers

So say I want to start the program counter at the RAM register, but don't care about which RAM register the data is written to afterwards; should I write each memory address before each command? for example if I want to write 2 bytes. do I do this:

Wire.beginTransmission(deviceAddress);
Wire.write(registerAddress);
Wire.write(dataByte1);
Wire.write(dataByte2);
Wire.endTransmission();

or this:

Wire.beginTransmission(deviceAddress);
Wire.write(registerAddress1);
Wire.write(dataByte1);
Wire.write(registerAddress2);
Wire.write(dataByte2);
Wire.endTransmission();

I'd start by reading the datasheet for the part you're using (and haven't shared with us).

Is it DS3231 RTC Module?
ds3231Pic-2

Like @gfvalvo says, read the data sheet. Some devices do sequential addressing where you specify the first address and subsequent writes go to sequential registers and some devices need each register specified before the write.

2 Likes

Thank you for explaining.

Almost. It uses a DS1307 which is the same form factor as the smaller chip.

I have started with that days ago, thanks. My question is more related to Wire.h which is why I didn't bother sharing the datasheet. It's alright though, a more useful person answered my question on another forum.

As @groundFungus noted, the information you were seeking is available in the datasheet. And, the proper way to use the Wire library depends on that information. So, the answer I provided was as complete as your incomplete question allowed.

There are many libraries for the DS1307. Search on Github for it. Find a simple one that you can use as an example.

I prefer to be clear what is meant by "address" to avoid confusion:

  • "I2C address", that is the I2C address of the DS1307 on the I2C bus.
  • "register address", is a value or index that points to a certain register inside the DS1307.

Your question on the other forum: https://www.reddit.com/r/arduino/comments/vc6053/address_counter_with_wireh/

The following terminologies are seen in the I2C related literatures:
1. Slave Address or Device Address (It is 7-bit)
2. I2C Address or I2C Bus Address of the Slave (It is 8-bit which is formed by appending 0 or 1 at the right most position of Slave Address during read and write operation.)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.