I’ve got two arduinos. I’m trying to demonstrate both the MasterReader example code and MasterWriter examples on the arduino forum. For the masterRead I’m just requesting a “ready” reply from the slave. For the MasterWriter I’m sending a number via the i2c link then incrementing. On the slave side i set the “ready” transmission up for the master’s request and then set the recieve to print out the value recieved.
The problem. The master is very clearly sending out data (checking via serial prints) however the slave never returns a reply. The slave doesn’t appear to be recieveing or acknowledging the requests or recieves.
/**********
Declarations
***********/
#include <Wire.h>
int z = 0;
int n = 0;
/****************
SETUP
****************/
void setup()
{
//setup information
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(13, OUTPUT);
//initializes the serial port uses pin 2 and 3
Serial.begin(9600);
ClearLCD();
Serial.print("READY ");
delay(100);
Serial.print(" SET ");
delay(100);
Serial.print(" GO!");
delay(100);
//initializes the I2C interface using pins 4 and 5.
//set as master (no address)
Wire.begin();
}
/**************************************
Main Code
***************************************/
void loop()
{
n++;
Wire.requestFrom(8, 5); //request ready
while(Wire.available())
{
char c = Wire.receive();
Serial.print(c);
n = 0;
}
Serial.print("request sent");
Serial.println(n);
ClearLCD();
delay(500);
Wire.beginTransmission(8); //send new value
Wire.send(z);
Wire.endTransmission();
if(z > 250)
{
z = 0;
}
z++;
delay(500);
Serial.print("Z = ");
Serial.println(z);
}
/***********************************
Functions
***********************************/
//Clears LCD function
void ClearLCD()
{
serCommand();
Serial.print(0x01, BYTE);
}
//command LCD
void serCommand()
{
Serial.print(0xFE, BYTE);
}
/**********
Declarations
***********/
#include <Wire.h>
/****************
SETUP
****************/
void setup()
{
//setup information
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(13, OUTPUT);
//initializes the serial port uses pin 2 and 3
Serial.begin(9600);
ClearLCD();
Serial.print("READY ");
delay(100);
Serial.print(" SET ");
delay(100);
Serial.print(" GO!");
delay(100);
//initializes the I2C interface using pins 4 and 5.
//set as slave with address of 8
Wire.begin(8);
Wire.onReceive(recieveEvent);
Wire.onRequest(requestEvent);
}
/**************************************
Main Code
***************************************/
void loop()
{
ClearLCD();
}
/***********************************
Functions
***********************************/
void recieveEvent(int x)
{
while(0 < Wire.available())
{
int t = Wire.receive();
Serial.println(t);
delay(500);
}
}
//function executes on request signal from maincontroller
void requestEvent()
{
Wire.send("ready");
ClearLCD();
delay(50);
Serial.println("request sent");
}
//Clears LCD function
void ClearLCD()
{
serCommand();
Serial.print(0x01, BYTE);
}
//command LCD
void serCommand()
{
Serial.print(0xFE, BYTE);
}
The first reference I found on i2c is the instrucatbles link I posted. I did try it without the pull-ups, but got nothing so I added in some 1.5k or 1.8k R’s still nothing.
I’m going to move to a PCB based connection with the 1.5k pull-ups.
I am not familiar with Arduino code, so I can’t help in that respect. I tried to search for the underlying AVR-code/C or description of changes to the hardware but couldn’t find anything. I wonder if the pin settings (internal pull-ups, high-impedance to output-low and vice-versa) are correctly done. Then again, that ‘Wire’-lib has probably been used extensively with success to know it works.
At best I can suggest to get an oscilloscope or logic analyser and see what happens to the bits in transit. Working blind is never good.
I wouldn’t worry too much about the pull up resistors as it shouldn’t have a big impact on this circuit. Did you try just using the posted sketches for Master Read and Write from your links without the extra code you added? If that doesn’t work then you may may have a wiring problem. You can also debug the communications a little bit by printing the return value from Wire.requestFrom, this could point you in the right direction also.