Pages: [1]   Go Down
Author Topic: i2c communication problem with two arduinos  (Read 856 times)
0 Members and 1 Guest are viewing this topic.
Portugal
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 Good afternoon, first of all my congratulations to this community.
  My problem is this, I bought 2 arduinos, and i wanted to make communication between them by  i2c .


I wanted to send one value, and then send the contents of two arrays. For this  I followed this link http://arduino.cc/en/Tutorial/MasterReader but not working.


The program is huge so I will only show the Send function, I did.



void setup(){                         //in setup i have this, and more other thing, but of communitation i have only this

 Wire.begin(); 



in loop i have this


 sender(val, array1, array2, , cont1, cont2);  //val is a int val, array1 is a first array, array2 is second array and cont1 is size of first array and cont2 is the size of second array




my function  sender is


void enviar(unsigned int nblocos,unsigned int *posicoes, unsigned int *valorespos, unsigned int conter1, unsigned int conter2 ){
 
 
  Wire.beginTransmission(4); // transmit to device #4
   
   
    Wire.write(nblocos);
   

   for (int i= 0; i< (conter1); i++){   
     
     Wire.write(posicoes);   
   }
 
  for (int i= 0; i< (conter2); i++){   
     
     Wire.write(valorespos);   
   }
 
   Wire.endTransmission();
   delay(1000);
}






and my receiver have this



#include <Wire.h>

void setup()
{
  Wire.begin(4);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
}

void loop()
{
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
  while(1 < Wire.available()) // loop through all but the last
  {
  int x = Wire.read();    // receive byte as an integer
  Serial.println(x);         // print the integer
}







But i dont print anything



the usb is connected in receiver, and i connecter 5v in arduino transmiter to Vin arduino receiver (arduino with usb)




What i make wrong

thanks a lot
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What i make wrong
A number of things. You did not tell us how the Arduinos are connected, to start with.

Quote
The program is huge so I will only show the Send function, I did.
For another. You need to create a simple sketch to send data. When that is known to work, you can then incorporate that into a larger sketch.
Logged

Colorado
Offline Offline
Full Member
***
Karma: 1
Posts: 170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I found this Introduction to I2C very helpful when I setup my first one a few weeks ago. You may want to carefully examine the wiring diagram (where the author has 3 Arduinos) in that tutorial. It can be important to have pull-up resistors. I have 3 ATmega328Ps communicating with I2C and they all share a single pair of pull-up resistors.
« Last Edit: August 24, 2012, 11:41:49 am by BlueJakester » Logged

Portugal
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i m gona try make only this  http://arduino.cc/en/Tutorial/MasterWriter
Logged

Colorado
Offline Offline
Full Member
***
Karma: 1
Posts: 170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

^^^ That shouldn't be difficult. I'm new at all of this and always start out with a simple, fundamental example of something such as I2C, XBee, clocks, displays, etc.  When I have the basic configuration working I move on to expand it or accomplish whatever the project goals are. That has been a good way for me to learn and it may work for you too.
Logged

Portugal
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everybody, sorry to be a bother again, but have not had success in transmission i2c.

Someone could tell me what this evil in these two programs, the master and the slave. Is not operating.


Master

Code:
#include <Wire.h>

int array[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64};
unsigned int sizearr=0;
unsigned int num=0;

void setup(){
  Serial.begin(9600);  

 Wire.begin(); // join i2c bus (address optional for master)
 
 
}

void loop() {
  
  sizearr=64;
  num=4;
  
   enviar(num, array, sizearr);
}

void enviar(unsigned int numbers, int *arr, unsigned int sizear){ //valorpos é valores da posiçao, sizear é o sizearray, cont2 e' o contador 2
  
  //enviar o start bit
  Wire.beginTransmission(4); // transmit to device #4
  
   Wire.write(numbers);

  // enviar conteudo do array four
  for (int i= 0; i< (sizear); i++){  
    
     Wire.write(arr[i]);    
     delay(500);
   }
  
   Wire.endTransmission();
}


Slave


Code:
#include <Wire.h>

void setup()
{
               // join i2c bus with address #4
  
  Serial.begin(9600);           // start serial for output
}

void loop()
{
  delay(100);
    Wire.begin(4);
  Wire.onReceive(receiveEvent); // register event
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
  while(1 < Wire.available()) // loop through all but the last
  {
    int x = Wire.read();    // receive byte as an integer
    Serial.println(x);         // print the integer
  }
  
}






Thanks smiley
« Last Edit: September 05, 2012, 12:05:58 pm by capucho10p » Logged

Leeds, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You dont' say what actually happens or what does not happen that you expect to.

Code:
for (int i= 0; i< (sizear); i++){   
     
     Wire.write(arr);   
     delay(500);
   }

looks wrong as you are constantly sending the same byte. I suspect you want something like

Code:
Wire.write(arr[i]);

Paul
Logged

Portugal
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks paul, but in my original program a have arr[ i], but i dont put code, and arr[ i] is arr ahahahha

I dont find the problem, and I checked the connections.
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5145
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@OP: always use the code tags for code to avoid the interpretation problems that countrypaul mentioned. An array index with variable i:

Code:
array[i] = a;

get to this without the tags:

array = a;

Logged

Portugal
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@OP: always use the code tags for code to avoid the interpretation problems that countrypaul mentioned. An array index with variable i:

Code:
array[i] = a;

get to this without the tags:

array = a;





I make this and nothing. Any ideias?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Hello everybody, sorry to be a bother again, but have not had success in transmission i2c.
You STILL haven't told us how the two Arduinos are connected. You MUST tell us that.
Logged

Portugal
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First, my apologies for my bad english.

My mounting hardware is equal to this.



but the powering the Arduinos are independently,i  connect the 5V output of the Master Arduino to the VIN pin on the slave.The slave is the arduino that have the usb, to print que values.



 
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why is the slave doing this?
Code:
void loop()
{
  delay(100);
    Wire.begin(4);
  Wire.onReceive(receiveEvent); // register event
}
Wire.begin() and Wire.onReceive() should be called in setup(), not loop().

Your master is not giving the slave any time to do anything. It hammers data to it as fast as possible.

Code:
    int x = Wire.read();    // receive byte as an integer
Once again, why? There is no possible benefit to storing the byte in an int.
Logged

Portugal
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Pauls i make the changers and nothing. I dont know why, this dont work.



Code:
#include <Wire.h>

void setup()
{
             
  Serial.begin(9600);           // start serial for output
   // join i2c bus with address #4
 Wire.begin(4);
 Wire.onReceive(receiveEvent); // register event
 delay(100);
 
}

void loop()
{
 
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
  while(1 < Wire.available()) // loop through all but the last
  {
    byte x = Wire.read();    // receive byte as an integer
    Serial.println(x);         // print the integer
  }
 
}


Code:
#include <Wire.h>

//unsigned int array[]={1,2,3,4,5,6,7,8};
int array[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64};
unsigned int sizearr=0;
unsigned int num=0;

void setup(){
  Serial.begin(9600); 

 Wire.begin(); // join i2c bus (address optional for master)
 
 
}

void loop() {
 
  sizearr=64;
  num=4;
 
   enviar(num, array, sizearr);
}

void enviar(unsigned int numbers, int *arr, unsigned int sizear){ //valorpos é valores da posiçao, sizear é o sizearray, cont2 e' o contador 2
  int a=0;

  //enviar o start bit
  Wire.beginTransmission(4); // transmit to device #4
   
   Wire.write(numbers);

  // enviar conteudo do array four
  for (int i= 0; i< (sizear); i++){   
     
    a=arr[i];
   
     Wire.write(a);   
     delay(500);
   }
   
   Wire.endTransmission();
}


It's not working smiley-sad

initially was using int, because later on I'll have to send negative number.

I only want send two arrays by i2c ahahah
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
It's not working
Doesn't tell us squat.

Both slave and master have Serial.begin() statements, but the master never prints anything to the serial port. How do you know that it is sending anything to the slave?

The onReceive() event needs to print something when it is called. Otherwise, you haven't a clue that the slave is doing anything.

Code:
     delay(500);
Why? Why are you making the master wait half a second between values sent?
Logged

Pages: [1]   Go Up
Jump to: