Go Down

Topic: Problem with I2C communication (Read 705 times) previous topic - next topic

clerkwithknife

I'm trying to get a number of devices using atmega328p chips to communicate over I2C and i'm not having any success. So I tried something much simpler, I simply programmed 2 arduinos with these codes, and tried to get some results but nothing happened. I'm using 1.8K pullup resistors. Is there a problem with the code, or is there a hardware issue?

Master code:
Code: [Select]
#include <Wire.h>
byte bytein;

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

void loop()
{

     Wire.beginTransmission('a');
     Wire.send('a');
     Wire.endTransmission();
     if(Wire.available()>0)
     {
         bytein = Wire.receive();
           Serial.println(int(bytein));
     }
           delay(10);
}


Slave:
Code: [Select]
#include <Wire.h>
void setup()
{
 Wire.begin('a');
 Wire.onReceive(process);
}

void loop()
{
}

void process(int bytes)
{
       Wire.send('a');
}

Grumpy_Mike

You appear to have the slave address set at 0x61 by the use of:-
Wire.beginTransmission('a');
Also the data you are sending is the same number as the address

Have you tried it with a real number for the address like 0x04 as used in the examples?

You only need one pull up per line not one on each board and while 1K8 should work I would use 4K7.

borref

For the slave side of I2C - onReceiveService - is an input only function. You should use this to process the inbound data, but not return anything. To build some confidence and see that communication is working, you could change process (on the slave) as follows:

Code: [Select]
void process(int bytes)
{
  // Wire.send('a');
 while (Wire.available()) Serial.print((char)Wire.receive());



To return data to a master you should implement the "onRequestService" routine in the slave - and the master should call the "requestFrom" function to poll for data. Typically this is done in sequence - First send a byte (begin, send, end) to tell the slave what you want and then request (requestFrom) the number of bytes you expect in return.

Address 'a' should be ok (the only reserved address is zero).

Go Up