Reputation: 2676
I'm trying to write to a separate device's EEPROM to configure the behavior is the device, and am controlling the device with an Arduino Uno.
As according to this webpage, my SCK is connected to pin 13 and my SDA connected to pin 11.
I have two functions, i2c_eeprom_write_byte
and i2c_eeprom_read_byte
, taken from this example.
void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
Wire.begin(deviceaddress); // MUST INCLUDE, otherwise Wire.endTransmission hangs
// called once, in setup
int rdata = data;
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.write(rdata);
Wire.endTransmission(false);
}
byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
byte rdata = 0xFF;
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
delay(10);
Wire.requestFrom(deviceaddress,1);
int avail = Wire.available();
Serial.println(avail);
if (Wire.available()) rdata = Wire.read();
// there's a bug here with Wire.available. It's returning 0 (ie, 0 bytes to be read),
// when it should be returning 1, since I want 1 byte.
return rdata;
}
My problem is that Wire.available()
always returns a 0, meaning that the slave device isn't sending anything to the master device, the Arduino.
How do I read from the slave device?
Upvotes: 2
Views: 2530
Reputation: 21
Looks like you're using an I2C library to write to a SPI device - they are not the same! Most external EEPROMs use I2C (using pins 4 & 5 on an Uno).
Upvotes: 2
Reputation: 2819
Debugging hardware without an oscilloscope or logic analyzer will be a challenge.
A simple double check is to confirm that the address pins have been wired to configure the address you have coded. If lucky, you just find the wrong address, fix it, and everything starts working.
Beware of how you debug. This
delay(10);
you added is a huge amount of time for a device that operates at 100 kbit/s (or faster). The delay is just as likely to disturb the communication as it is to help you debug. You don't mention the device, but confirm whether it can tolerate that large a pause. Writing to memory can take time to perform, but reading is almost instant compared to a 100 kHz clock rate.
Similarly, adding this in the middle of a transaction
println(...
can be part of the problem. If you have Serial at the default 9600 baud, transmitting that one character will interrupt the I2C transaction.
If you have a scope, the only trick I can offer is to replace a pull up resistor with a resistor divider pair that pulls SDA and SCL to a voltage that is logic 1, but not equal to Vcc (5 V in your case?). For example pick a pair of resistors that hold the lines at 4.8 V. With this arrangement you can tell the difference between no devices driving the bus, and a device driving a logic 1.
Upvotes: 0