EEPROM memory/Two wires/03. Two wires and an address
Two wires · 03 of 10

Two wires and an address

A write is a start, the address 0x50, two bytes saying where in the memory, the value, and a stop. A read is the same up to the where, then a second start and the chip talks. Every byte is acknowledged, and only the stop tells the chip to start writing.

A write, frame by frame

Two wires and an address
Show
Frame
START
SDA driven by
your board
Clocks so far
1
SDA falls while SCL is high: every device on the bus starts listening.

Press play and read the sentence under each frame. Green is your board driving SDA, orange is the chip.

Your board calls 0x50 with a 0 on the end, which means "I am going to write". The chip pulls SDA low in the ninth clock, its acknowledge, and the other devices on the bus stay quiet. Then come two bytes of memory address, high then low: this chip has 16,384 places, and one byte only counts to 255. Then the value, then the stop.

The stop is the important frame. Nothing has been stored until it arrives. Then the chip spends up to 5 ms writing, and for that time it answers nobody, not even its own address. Five milliseconds a write is about what that does to a sketch.

A read starts as a write

Switch the figure to the read. It begins exactly like a write, 0x50 and the two address bytes, but sends no value. That "write" only moves the chip's pointer to the byte you want.

Then a second start with no stop in between, so nothing else can take the bus, and 0x50 with a 1 on the end: "now you talk". The chip sends the byte. Your board answers with no acknowledge, which means "that is all", and a stop.

If your board acknowledges instead, the chip sends the next byte, and the next, for as long as it is asked, across the whole chip. That is how the sketches read a block of text in one go.

In Arduino

The same frames, in the Wire library:

Wire.beginTransmission(0x50);   // START, 0x50 + W
Wire.write(0x01);               // address, high byte
Wire.write(0x00);               // address, low byte: byte 256
Wire.write(42);                 // the value
Wire.endTransmission();         // STOP: the write begins now

Wire.endTransmission() returns 0 when every byte was acknowledged, and a number when one was not. The sketches check it.

When it does not work

Why two address bytes? Other examples send one.

Other examples are for smaller chips. 16,384 addresses need 14 bits, which do not fit in one byte, so this chip always takes the high byte first and then the low. A sketch or library that sends one byte talks to the wrong place.

What does NACK mean?

Not acknowledged: nobody pulled SDA low in the ninth clock. After the address byte it means no device at that address answered, which is either a wiring fault or, just after a write, a chip that is busy writing.

Do I need to write this sequence myself?

The sketches in this book do, in about ten lines, so nothing is hidden. Libraries for AT24C-series chips do the same thing; if you use one, pick one that says it sends two address bytes.

Where this goes next

Why VCC goes to 3V3 on a 3.3 V board and 5V on an Uno.

VCC sets the bus

Edit this page — content/books/eeprom-memory/two-wires-and-an-address.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

Ask a question ↗

EEPROM Memory

Loading discussions…

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum