Go Down

Topic: 4 x I2C devices check not work propary (Read 422 times) previous topic - next topic

Midway

Jun 18, 2013, 11:27 pm Last Edit: Jun 18, 2013, 11:29 pm by Midway Reason: 1
what i am doing wrong.
i need at start-up a I2C check if the connection is made proparly.

Code: [Select]
  byte Respond1;                            // At startup, check if I2C Device responding
  byte Respond2;                            // At startup, check if I2C Device responding
  byte Respond3;                            // At startup, check if I2C Device responding
  byte Respond4;                            // At startup, check if I2C Device responding


Code: [Select]
void setup()
{
  Serial.begin(9600);
  lcd.begin(24, 2);                                 // Type LCD
  lcd.clear();
  Wire.begin();                                     // Wake up I2C Master. Connect pin 4 (the clock, or SCL, pin) and pin 5 (the data, or SDA, pin)
   
   lcd.setCursor(4,0);
   lcd.print("I2C Initializing");
   lcd.setCursor(0,1);
   
Wire.beginTransmission(Tuner_address_TX);
    Wire.requestFrom(Tuner_address_TX, 1);                      // request 1 bytes from Tuner_address_TX 0x60
    Respond1 = Wire.endTransmission();
  if (Respond1 == 0)
    lcd.print("");
    else
    lcd.setCursor(0,1);
    lcd.print("  I2C ERROR Tuner 0x60  ");
   
delay(2500);
    lcd.setCursor(0,1);

Wire.beginTransmission(PCF1_BUTTON);
    Wire.requestFrom(PCF1_BUTTON, 1);                      // request 1 bytes from PCF1_BUTTON 0x3E
//    Respond2 = Wire.endTransmission();
  if (Respond2 == 1)
    lcd.print("");
    else
    lcd.setCursor(0,1);
    lcd.print(" I2C ERROR Buttons 0x3E ");

delay(2500);
    lcd.setCursor(0,1);

Wire.beginTransmission(PCF2_LED);
    Wire.requestFrom(PCF2_LED, 1);                      // request 1 bytes from PCF2_LED 0x3D
    Respond3 = Wire.endTransmission();
  if (Respond3 == 0)
    lcd.print("");
    else
    lcd.setCursor(2,1);
    lcd.print("  I2C ERROR Led's 0x3D  ");

delay(2500);
    lcd.setCursor(0,1);

Wire.beginTransmission(PCF3_RELAY);
    Wire.requestFrom(PCF3_RELAY, 1);                      // request 1 bytes from PCF3_RELAY 0x3B
    Respond4 = Wire.endTransmission();
  if (Respond4 == 0)
    lcd.print("");
    else
    lcd.setCursor(1,1);
    lcd.print(" I2C ERROR Relay's 0x3B ");

    delay(2500);
    lcd.setCursor(0,1);
   
    lcd.clear();
    lcd.setCursor(7,0);
    lcd.print("I2C Ready");
   
    delay(2000);
    lcd.clear();

HugoPT

#1
Jun 19, 2013, 12:00 am Last Edit: Jun 19, 2013, 12:05 am by HugoPT Reason: 1
Quote
Wire.beginTransmission(Tuner_address_TX);
   Wire.requestFrom(Tuner_address_TX, 1);                      // request 1 bytes from Tuner_address_TX 0x60
   Respond1 = Wire.endTransmission();

From here:http://arduino.cc/en/Reference/WireBeginTransmission
Quote
Begin a transmission to the I2C slave device with the given address. Subsequently, queue bytes for transmission with the write() function and transmit them by calling endTransmission().

What are you transmitting?
If you just want read the byte from the slave you should just use the requestFrom() to invoke slave the byte.
Then you should use the available() and read() functions.  

Quote
Respond1 = Wire.endTransmission();

This returns a boolean value
What are you receiving from it?
Debian,Mint,Ubuntu
Arduino Mega 2560
Arduino Nano
Arduino Duemilanove
MAC OS Montain Lion
Raspberry PI Model B

daveclark5

what is the problem?  when i experimented with something similar i had an issue that any time there was a problem it just hung!  and so i couldn't report anything.

also make sure that you have the right value for a resistor; especially if you have over a half meter of cable (check the spec).

we just spent an hour trying to debug an i2c (three arduinos) and found that if one was powered off, or the code was wrong everything hung and it hung on the Wire.endTransmission (which is of course when the data is transmitted).

you raise a good point though - how to debug things like this.  the truth is that once you get it working you probably won't go back again; so it might not be worth the trouble to check this.

another thing we bumped into was volatile memory  (volatile int).  i'm not sure if you can store data in there and harvest it after a reset or not. 

Nick Gammon

First things first. Get your shift key repaired. When you refer to yourself it is I, not i.

Code: [Select]
Wire.beginTransmission(PCF3_RELAY);
    Wire.requestFrom(PCF3_RELAY, 1);                      // request 1 bytes from PCF3_RELAY 0x3B
    Respond4 = Wire.endTransmission();
  if (Respond4 == 0)
    lcd.print("");
    else
    lcd.setCursor(1,1);
    lcd.print(" I2C ERROR Relay's 0x3B ");


Better read this: http://gammon.com.au/i2c

If you are planning to have " I2C ERROR Relay's 0x3B " appear if there is an error, you need some more braces than you have shown.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up