I2C Read() wrong data received
Scenario 1:
MCU1: ATMega328p I2C Role: Master Library used: Arduino_Software_I2C Receiving 1 byte of data from slave (id 10).
MCU2: ATMega328p I2C Role: Slave (Slave id: 10) Library used: Standard Wire.h library Sending 1 byte of data to master on request. Printing sent data to Serial monitor.
Result: Most of the cases received wrong/partial data at the master end.
Scenario 2:
MCU1: ATMega328p I2C Role: Master Library used: Standard Wire.h library Receiving 1 byte of data from slave (id 10).
MCU2: ATMega328p I2C Role: Slave (Slave id: 10) Library used: Standard Wire.h library Sending 1 byte of data to master on request. Printing sent data to Serial monitor.
Result: Always receive correct data at the master end
Probable issue: Bit receiving time-stretching due to heavy load at the slave end.
Slave Code:
#include <Wire.h> void setup() { Serial.begin(9600); Wire.begin(10); Wire.onRequest(requestEvent); }
void loop() {}
void requestEvent() { byte x= B01010101; Wire.write(x); LED(x); //Dummy load at the slave end }
void LED(byte x) //Dummy load at the slave end { pinMode(13,OUTPUT); for(int i=0; i<8;i++) { bool a=digitalRead(13); digitalWrite(13,!a); Serial.print("Sent: "); Serial.println(x,BIN); } }
Master Code (Scenario 1)
#include <Wire.h>
void setup() { Serial.begin(9600); Wire.begin(); }
void loop() { Read_(); delay(1000); }
void Read_() { Wire.requestFrom(10, 1); byte a= Wire.read(); Serial.print(a,BIN); Serial.print("\t"); Serial.println(a); }
Master Code (Scenario 2)
#include "SoftwareI2C.h" SoftwareI2C w;
void setup() { Serial.begin(9600); w.begin(3, 2); // sda=d3, scl=d2 }
void loop() { Read_(); delay(1000); }
void Read_() { w.requestFrom(10, 1); byte a= w.read(); Serial.print(a,BIN); Serial.print("\t"); Serial.println(a); }
Output at the Master end (Scenario 2)
Expected data: 1010101 85
Received data:
10101 21 11111111 255
Note
If I remove the heavy load [ LED (byte x) ] from the slave end, then both the standard Wire library and your library work fine. The problem happens when there is a load at the slave end while using your i2c library as a master.
Please resolve this issue if possible. Thanks in advance.
Sincerely, Manzar
Hello @manzarehassin,
Could you please share a connection diagram of your master and slave setup? I will try to reproduce this issue on my end.
Best Regards, Lakshantha
Master end (Arduino uno)
SDA= D3 SCL= D2 GND= GND
Slave End
SDA = A4 SCL = A5 GND = Connect to ground (GND) connection of Master
Hello,
I'm sorry it took me so long to reply to you.
Do you still have this problem now?
Hello,
I'm sorry it took me so long to reply to you.
Do you still have this problem now?
I've downloaded the latest code from your repository and used the same sample code and connections as mentioned before. I've got the same result as before. Yes, I'm getting the same issue.
I don't have this hardware device at the moment, I will try to fix it once I have it and let you know the result
This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 7 days.
This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 7 days.
This issue was closed because it has been stalled for 7 days with no activity.