Simple I2C question. I just want to write and read

All i am trying to do is write a hex number and then have it read it back to me so i can feel comfortable it has received it.

The hex numbers i want to write are 0x7A (High Byte) and 0x53 (Low Byte) to a device whose EEPROM address is 0x20 and address is 0x00. Attached is a copy of my code. I am pretty certain that the “write” was performed correctly, but am not certain how to read it. I think my problem lies in trying to display the hex number on my serial.print. Thank you !

#include <Wire.h>

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

void loop(){
int data = 0;
// Erase information in EEPROM address 0x00 (EEPROM → To max)
Wire.beginTransmission(0x00); // Transmit information to this salve_address
Wire.send(0x20); // Send command 001x xxxx + 00 = b0010 0000
Wire.send(0x00); // Send lowByte (0’s to clear)
Wire.send(0x00); // Send highByte (0’s to clear)
Wire.send(0x43); // Send PEC
Wire.endTransmission(); // stop transmitting
delay(10);
// Write information to EEPROM address 0x00 (EEPROM → To max)
Wire.beginTransmission(0x00); // Transmit information to this slave_address
Wire.send(0x20); //
Wire.send(0x53); //
Wire.send(0x7A); //
Wire.send(0x11); //
Wire.endTransmission(); // stop transmitting
delay(10);
Serial.println(“Completed writing 7A53 to To max (0x20)”);

delay(3000);

Wire.beginTransmission(0x00);
Wire.send(0x20);
Wire.endTransmission();

Wire.beginTransmission(0x00);
Wire.requestFrom(0x00,2);
if (Wire.available()) {
data = Wire.receive();
}
if (Wire.available()) {
data |= Wire.receive() << 8;
}
Wire.endTransmission();
Serial.println(data);

Wire.beginTransmission(0x00); // Transmit information to this salve_address

I thnk this should be 0x20.

What are you trying to talk to a I2C eeprom or a second arduino ?. what device is it ?.

0x20 is the eeprom address i want to write to. 0x00 is the slave address. I am trying to control an IR temperature probe. (set object temp MAX and read from its RAM etc.)

You cant have a slave address of 0x00 , its a reserved address . I think for the master , so I think its talking to its self !!!

o.k , you have started with Wire.begin(); without a parameter which makes you the master device .

With :- Wire.beginTransmission(0x00); you are talking to slave address 0x00 . eg the BUS ADDRESS --- NOT the address inside the eeprom where you want to send or collect data from .

I`m sure It should be 0x20 or 0x40 !!!!!!

Then send the data to that device, by sending the command register address / then the data to the device.

I don`t know what device you are talking to, but it should be something like this for a eeprom and that the address may need a shift eg should be 0x40

Wire.beginTransmission(0x40); // Transmit information to this salve_address Wire.send(0x00); // low byte of eeproms command address Wire.send(0x00); // high byte of eeproms command address Wire.send(0x43); // Send the data to be stored Wire.endTransmission(); // stop transmitting delay(10);

Read back with :-

int tmp; Wire.beginTransmission(0x40); Wire.send(0); // low byte of command address of eeprom Wire.send(0); // high byte of command address of eeprom Wire.endTransmission(); Wire.requestFrom(0x40, 1); // ask for one byte and switch to read tmp=Wire.receive(); // put data in tmp

AND make sure you have added the resistors.