Arduino_Software_I2C icon indicating copy to clipboard operation
Arduino_Software_I2C copied to clipboard

I2C Read() wrong data received

Open manzarehassin opened this issue 3 years ago • 2 comments

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

manzarehassin avatar Apr 13 '22 12:04 manzarehassin

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

lakshanthad avatar Apr 22 '22 03:04 lakshanthad

Master end (Arduino uno)

SDA= D3 SCL= D2 GND= GND

Slave End

SDA = A4 SCL = A5 GND = Connect to ground (GND) connection of Master

manzarehassin avatar Apr 22 '22 06:04 manzarehassin

Hello,

I'm sorry it took me so long to reply to you.

Do you still have this problem now?

Lesords avatar Oct 16 '24 02:10 Lesords

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.

manzarehassin avatar Oct 16 '24 04:10 manzarehassin

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

Lesords avatar Nov 08 '24 12:11 Lesords

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.

github-actions[bot] avatar Jan 08 '25 04:01 github-actions[bot]

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.

github-actions[bot] avatar Jan 22 '25 04:01 github-actions[bot]

This issue was closed because it has been stalled for 7 days with no activity.

github-actions[bot] avatar Jan 30 '25 04:01 github-actions[bot]