Go Down

Topic: Problems with Wire.requestFrom (Arduiono Uno v1.0.3) (Read 2 times) previous topic - next topic

Nick Gammon


In the WireRequestFrom(0x21, 1); // request 1 byte from 0x42h - revision
statement I am reading an I2C device on the secondary device which is why the address is different
as I want to verify that the power did switch on/off on the secondary device.


You don't modify the device address to read from it. The library does that. You are reading a different device that way (which probably doesn't exist).

Fix that first.

irethedo

Thanks for the reply...
I tried your suggestion and have posted the code and results for two different scenarios.
1. Reading from the  I2C with the address offsets.
2. Reading from the  I2C per your suggestion.
I had to leave the writing to set the power up as I had it otherwise the power would not switch on and off as I intend for it to do...

1. Reading from the  I2C with the address offsets.
Code: [Select]
  Serial.flush();  // flush the serial buffer
   delay (300);
   Serial.println("---");
     // ------------ turn on ext power ------------------
   Wire.beginTransmission(0x22);        // write to I2c Address 44h to set power condition
//   Wire.beginTransmission(0x44);      // writing to I2c Address 44h to set power condition - DOES NOT WORK
   Wire.write(0xAF);                    //  write command to turn off ext headbox power
   timeout = Wire.endTransmission();    // Check status of data sent- did it get there?
   delay(4500);
   Serial.print(" Power IS ON- timeout= ");    // Check status of data sent- did it get there?
   Serial.print(timeout); 

   Wire.beginTransmission(0x21);        // write to I2c Address 42h to check if ext device is available
    Wire.write(0x00);                   //  write command to test to see if device exists
   timeout = Wire.endTransmission();    // Check status of data sent- did it get there? 
   
   Serial.print(" check power on- timeout= ");
   Serial.print(timeout); 
   
   v44on= 0;                    // initialize v44off to zero
   Wire.requestFrom(0x21, 1);    // request 1 byte from 0x42h - revision
   
   while(Wire.available())    // slave may send less than requested
   {
        v44on = Wire.read();    // receive a byte as character
   }
   Serial.print(" v44onf= ");
   Serial.println(v44on);
   
     // ------------ turn off ext power ------------------
   Wire.beginTransmission(0x22);        // write to I2c Address 44h to set power condition
//   Wire.beginTransmission(0x44);      // writing to I2c Address 44h to set power condition - DOES NOT WORK
   Wire.write(0xFA);                    //  write command to turn off power
   timeout = Wire.endTransmission();    // Check status of data sent- did it get there?
   delay(4500);   
   Serial.print(" Power IS OFF- timeout= ");
   Serial.print(timeout);

   Wire.beginTransmission(0x21);        // write to I2c Address 42h to check if ext device is available
   Wire.write(0x00);                    //  write command to test to see if device exists
   timeout = Wire.endTransmission();    // Check status of data sent- did it get there?
   
   Serial.print(" check power off- timeout= ");
   Serial.print(timeout);

   v44off= 0;                    // initialize v44off to zero
   Wire.requestFrom(0x21, 1);    // request 1 byte from 0x42h - revision
   
   while(Wire.available())    // slave may send less than requested
   {
        v44off = Wire.read();    // receive a byte as character
   }
   Serial.print(" v44on= ");
   Serial.println(v44on);
   
     // ------------ turn on ext power ------------------
   Wire.beginTransmission(0x22);        // write to I2c Address 44h to set power condition
//   Wire.beginTransmission(0x44);      // writing to I2c Address 44h to set power condition - DOES NOT WORK
   Wire.write(0xAF);                    //  write command to turn off ext headbox power
   timeout = Wire.endTransmission();    // Check status of data sent- did it get there?
   delay(4500);
   Serial.print(" Power IS Turned Back ON- timeout= ");    // Check status of data sent- did it get there?
   Serial.println(timeout); 
   


===============================================================================

With the device connected:
(I2C addr 42h = 1 when power is on
No data is displayed when power is off)
When testing to see if the device is there, Wire.endTransmission returned "0"

I tried writing to 0x44 instead of 0x22 but then my power would not switch on and off.
(the library does not accomodate how the I2C parts are wired on the device that I am communcating with...)

Here is a sample of the output seen (device connected):

Quote
ð---
Power IS ON- timeout= 0 check power on- timeout= 0 v44onf= 1
ø---
Power IS ON- timeout= 0 check power on- timeout= 0 v44onf= 1


With the device disconnected:
Quote
(all data is seen- power doesn't matter as device is not connected)


Power IS ON- timeout= 0 check power on- timeout= 2 v44onf= 0
Power IS OFF- timeout= 0 check power off- timeout= 2 v44on= 0
Power IS Turned Back ON- timeout= 0


2. Reading from the  I2C per your suggestion.

Code: [Select]
  Serial.flush();  // flush the serial buffer
   delay (300);
   Serial.println("---");
     // ------------ turn on ext power ------------------
   Wire.beginTransmission(0x22);        // write to I2c Address 44h to set power condition
//   Wire.beginTransmission(0x44);      // writing to I2c Address 44h to set power condition - DOES NOT WORK
   Wire.write(0xAF);                    //  write command to turn off ext headbox power
   timeout = Wire.endTransmission();    // Check status of data sent- did it get there?
   delay(4500);
   Serial.print(" Power IS ON- timeout= ");    // Check status of data sent- did it get there?
   Serial.print(timeout); 

    Wire.beginTransmission(0x42);       // write to I2c Address 42h to check if ext device is available
    Wire.write(0x00);                   //  write command to test to see if device exists
   timeout = Wire.endTransmission();    // Check status of data sent- did it get there? 
   
   Serial.print(" check power on- timeout= ");
   Serial.print(timeout); 
   
   v44on= 0;                    // initialize v44off to zero
     Wire.requestFrom(0x42, 1);    // request 1 byte from 0x42h - revision
   
   while(Wire.available())    // slave may send less than requested
   {
        v44on = Wire.read();    // receive a byte as character
   }
   Serial.print(" v44onf= ");
   Serial.println(v44on);
   
     // ------------ turn off ext power ------------------
   Wire.beginTransmission(0x22);        // write to I2c Address 44h to set power condition
//   Wire.beginTransmission(0x44);      // writing to I2c Address 44h to set power condition - DOES NOT WORK
   Wire.write(0xFA);                    //  write command to turn off power
   timeout = Wire.endTransmission();    // Check status of data sent- did it get there?
   delay(4500);   
   Serial.print(" Power IS OFF- timeout= ");
   Serial.print(timeout);

   Wire.beginTransmission(0x42);        // write to I2c Address 42h to check if ext device is available
   Wire.write(0x00);                    //  write command to test to see if device exists
   timeout = Wire.endTransmission();    // Check status of data sent- did it get there?
   
   Serial.print(" check power off- timeout= ");
   Serial.print(timeout);

   v44off= 0;                    // initialize v44off to zero
    Wire.requestFrom(0x42, 1);    // request 1 byte from 0x42h - revision
   
   while(Wire.available())    // slave may send less than requested
   {
        v44off = Wire.read();    // receive a byte as character
   }
   Serial.print(" v44on= ");
   Serial.println(v44on);
   
     // ------------ turn on ext power ------------------
   Wire.beginTransmission(0x22);        // write to I2c Address 44h to set power condition
//   Wire.beginTransmission(0x44);      // writing to I2c Address 44h to set power condition - DOES NOT WORK
   Wire.write(0xAF);                    //  write command to turn off ext headbox power
   timeout = Wire.endTransmission();    // Check status of data sent- did it get there?
   delay(4500);
   Serial.print(" Power IS Turned Back ON- timeout= ");    // Check status of data sent- did it get there?
   Serial.println(timeout); 


Per your suggestion, I wrote to and read from 0x42 instead of 0x21-
I tried writing to 0x44 instead of 0x22 but then my power would not switch on and off.
(the library does not accomodate how the I2C parts are wired on the device that I am communcating with...)

With the device connected:
(I2C addr 42h = 0
When testing to see if the device is there, Wire.endTransmission returned "4"
When power is on and no data is displayed when power is off)

Here is a sample of the output seen (device connected):

Quote
ð---
Power IS ON- timeout= 0 check power on- timeout= 4 v44onf= 0
ð---
Power IS ON- timeout= 0 check power on- timeout= 4 v44onf= 0


With the device disconnected: (timeout is now 2 but data did not change).
Here is a sample of the output seen (device not connected):

Quote
Power IS ON- timeout= 0 check power on- timeout= 2 v44onf= 0
Power IS OFF- timeout= 0 check power off- timeout= 2 v44on= 0
Power IS Turned Back ON- timeout= 0

pylon

In a post before you told that you're accessing two different devices. I think it's time to tell exactly what devices these are and post links to their datasheets. Everythings let us guess and not help you. And try to draw how you wired everything together.

Nick Gammon

I'm getting confused now. Can you please run the I2C scanner and post what it returns?

http://www.gammon.com.au/forum/?id=10896&reply=6#reply6

irethedo

There are no links to datasheets for these items as these devices are custom and company
confidential information.

Can someone send me a link to the Wire commands in v1.0.3?

If I could ask the same question in an easier way, it might go like this:

The problem seems to be when I try to write to I2C address 44h with the following code
which shuts the power off to the device that  i am attempting to communicate or get a
status if whether it is connected or not and the data returned from Wire.endTransmission()
(and all the code from that point to the end of my loop closing bracket) is ignored...

Is there some sort of error handling going on behind the scenes that is causing a break
statement (of sorts) in my loop?

I need more information on the v 1.0.3  Wire library I think.

Please see comments in the code below


// ------------ turn off ext power ------------------
   Wire.beginTransmission(0x22);              // write to I2c Address 44h to set power condition
//   Wire.beginTransmission(0x44);           // (Use address in line above) because this one DOES NOT WORK
   Wire.write(0xFA);                               //  write command to turn off power
                                                          // ***** power does turn off here with the command in the line above as expected ***********
   timeout = Wire.endTransmission();        // Check status of data sent- did it get there?
   delay(4500);   
   Serial.print(" Power IS OFF- timeout= ");  // This time out is not being displayed in Serial monitor .. Why is that?
   Serial.print(timeout);                  // and all  the code through the end of my loop end bracket is also ignored

Go Up