I wrote the following program in arduino uno, and it worked well, using the pin A4 and A5 for the I2C communication like SDA and SCL, respectively. However, I am trying to run it in arduino mega 2560 and it is not working, in fact I did not recive any data...I already checked with a resistence before the SDA and SCL (pin 20 and 21). The question is what is wrong?
#include "Wire.h"
int z;
float positionx,positiony;
void setup()
{
Wire.begin(); // wake up I2C bus
Serial.begin(9600);
}
void SendData()
{
byte x1,x2,y1,y2;
int x,y,num;
z=11;
for (z=11;z<13;z++) // Only for the first two sensors
{
Wire.requestFrom(z, 4); // Get the contents of first two registers"
num = Wire.available();
Serial.print(num);
y1 = Wire.read(); // first received byte stored here
y2 = Wire.read();
x1 = Wire.read();
x2 = Wire.read();
}
Serial.print("\t");
Serial.print(x1); //this is the one in the left side
Serial.print("\t");
Serial.print(x2);
Serial.print("\t");
Serial.print(y1); // angle shown in degrees not radians
Serial.print("\t");
Serial.print(y2);
Serial.print("\n");
}
#include "Wire.h"
int z;
float positionx,positiony;
void setup()
{
Wire.begin(); // wake up I2C bus
Serial.begin(9600);
}
void SendData()
{
byte x1,x2,y1,y2;
int x,y,num;
z=11;
for (z=11;z<13;z++) // Only for the first two sensors
{
Wire.requestFrom(z, 4); // Get the contents of first two registers"
num = Wire.available();
Serial.print(num);
y1 = Wire.read(); // first received byte stored here
y2 = Wire.read();
x1 = Wire.read();
x2 = Wire.read();
}
Serial.print("\t");
Serial.print(x1); //this is the one in the left side
Serial.print("\t");
Serial.print(x2);
Serial.print("\t");
Serial.print(y1); // angle shown in degrees not radians
Serial.print("\t");
Serial.print(y2);
Serial.print("\n");
}
void loop()
{
SendData();
delay(2000);
}
I think it is because I updated my arduino program and some lines have to change...but I dont know what lines exactly.
Thanks.
for (z=11;z<13;z++) // Only for the first two sensors
{
Wire.requestFrom(z, 4); // Get the contents of first two registers"
num = Wire.available();
Serial.print(num);
y1 = Wire.read(); // first received byte stored here
y2 = Wire.read();
x1 = Wire.read();
x2 = Wire.read();
}
Are your device addresses 11 and 12? Also it would be useful to test if you got 4 (in num). If the device doesn't respond, and you don't get 4 returned from Wire.available() then the reads will return rubbish.
I was trying everything. When I remove the wires for SDA and SLC the code runs, but when I connected the wires the program just stop and I did not get anything...everything stops. So, I guess the problem is with the command Wire.requestFrom and the signal. What is the relationship between the cables and the function?
The I2C library can hang, because of its design, if an interrupt doesn't occur when it should. First I would install pull-up resistors (Eg. 4.7K for both SDA and SCL from each of them to 5V).
If that doesn't help try an alternative library which I believe is more resistant to hanging:
I put a 10k resistor between the SDA and ground, and a second 10k between SCL and ground. In the way you suggest (between SDA and +5V) it does not work...But, thanks. It is working now.
Are you sure of that? Because that would fly in the face of how a I2C bus works. Open drain output transistors on all attached devices can only pull-down and the bus relies on passive pull-ups for a valid HIGH state. As the two signal lines are bidirectional the user circuits have to be able to release their output so as to be able to receive on the same signal line.
So if you indeed have a I2C bus working with only two pull-down resistors, then you are probably in line for some kind of technical achievement award at least.
I suspect your sketch code is fooling you into thinking it's working when it actually isn't. You certainly may have changed your symptoms by using two pull-downs, but to say it is functioning correctly flies in all logic.
Thanks for it. I already tested this configuration with another arduino mega and it works, so...I am sharing the picture of it with you. But this configuration only works with arduino mega, with arduino uno it works in a normal way.