arduino I2C with MCP23008 no readout

goodday,

first of all: i'm fearly new to the arduino i've already made some small projects like with RFID and DCF and some more smaller stuff.

but now i've a problem i'm buzy trying to make a small I2C circuit with 4x the MCP23008, i've made a small test circuit on a prototype board with 1 MCP23008.
the problem is that when i try and readout the MCP23008 i only get zeros.

i just don't know what i do wrong so if you can help, please.

this is what i've written now: (this is a code where i've tried several things in to try to get it to work but nothing does so any good ideas ?)

#include <Wire.h>                                     

int ALCMswitchPos[8];
int i;
int j;
int ALCMultiplier;
int ALCMswitchPos1, ALCMswitchPos2, ALCMswitchPos3, ALCMswitchPos4, ALCMswitchPos5, ALCMswitchPos6, ALCMswitchPos7, ALCMswitchPos8;
byte I2CrecievedByte;

void setup() {
  
  Wire.begin();                                        // start I2C communicatie
  Serial.begin(9600);                                  // start serial communicatie met 9600 bps:
  
// Wire.beginTransmission(B01001110);
// Wire.send(0x00);//select IODN register
// Wire.send(B00000000);//set register value-all low
// Wire.endTransmission();
}


void loop(){
    
Wire.requestFrom(B01001111, 1);
if(Wire.available()){
  for ( i = 0; i < 9; i++){
ALCMswitchPos[i] = Wire.receive();

  }
}
Wire.endTransmission();
        
  int j;
  for (j = 0; j < 8; j++) {
  Serial.print(ALCMswitchPos[i]);
  }
  // CrackByte(I2CrecievedByte, ALCMswitchPos );
                                                       
    ALCMswitchPos1 = ALCMswitchPos[1]*1;
    ALCMswitchPos2 = ALCMswitchPos[2]*2;
    ALCMswitchPos3 = ALCMswitchPos[3]*3;
    ALCMswitchPos4 = ALCMswitchPos[4]*4;
    ALCMswitchPos5 = ALCMswitchPos[5]*5;
    ALCMswitchPos6 = ALCMswitchPos[6]*6;
    ALCMswitchPos7 = ALCMswitchPos[7]*7;
    ALCMswitchPos8 = ALCMswitchPos[8]*8;
    
    ALCMultiplier = ALCMswitchPos1 + ALCMswitchPos2 + ALCMswitchPos3 + ALCMswitchPos4 + ALCMswitchPos5 + ALCMswitchPos6 + ALCMswitchPos7 + ALCMswitchPos8;
    
  Serial.print("CValue: ");
  Serial.println(ALCMultiplier, DEC);
  delay(1000);
}

thanks already

Wire.requestFrom(B01001111, 1);

Is requesting 1 byte from your device.

Then:-
for ( i = 0; i < 9; i++)
You go on and read 9 values from it.

Read the data sheet, you will see you can't just request to read all the internal registers at once. You have to specify what register you want to read.

look at the code of this project:-
http://www.thebox.myzen.co.uk/Hardware/Pendulum.html
This uses the MCP23016 which is the dual version of the same chip.

first of all thanks for your reaction.

i know what you mean but that isn't the intention i have (to read out all internal registers).

the thing i just want to do (and for as far as i know it should be fairly simple) is to read out the values of the "input pins"
but it seems that i just can't get it to work :o :cry:

i have also tried this : http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1254331900/4
and still i get nothing.

i have also tried to set outputs MCP23008 high and low and that works.

i've now tried an other program based on the programs i found on this forum and other Internet sites.

#include <Wire.h>
byte tmp;

void setup() {
   Wire.begin();                      // initialise the wire library and hardware
   Serial.begin(9600);
}



void loop() {
  
  //First the DDR is 0 and 0 = output , 1 = input  
Wire.beginTransmission(0x27); // 0x20 + (A2*4) + (A1*2) + (A0*1) 
Wire.send(0);//< ----- this is the DDR
Wire.send(255);//< ----- 255 (B11111111) = all inputs 
Wire.endTransmission();

//data read
Wire.beginTransmission(0x27);
Wire.send(9);
Wire.endTransmission();

if(Wire.available()){
Wire.requestFrom(0x27, 1);
tmp = Wire.receive();
Serial.print("available ");
Serial.println("data: ");
Serial.println(tmp, BIN);
delay(500);
}

if(!Wire.available()){
  Serial.print("not available");
  delay(500);
}

}

and this always give me "not available"

(pin connections i have now:
pin 1 to analog 5
pin 2 to analog 4
pin 3,4,5 5V
pin 6 5V
pin 7, 8 float
pin 9 0V
pin 10 - 17 a random binary code for testing (00010110) +5 and 0V
pin 18 +5V

pull-up resistors are 4k7

i have also tried to set outputs MCP23008 high and low and that works.

If you can change the outputs then the bits in the data direction register are being set to be outputs and consequently you will receive nothing if this is the case.

It is no good trying code you find at random, you need to read the data sheet. That last code you posted has nothing in the setup. The setup section of the code should as the name implies set up the chip to do what you want. In this case you want to read the data that is on them.
Have you downloaded the code from the Pendulum project? This has all 16 lines on this chip to be inputs. You have only half this chip. You will see the setup section does a few things like setting the data direction registers and other things. In the main body of the code you have the bit that reads the inputs.
However it is important that you read the data sheet and try and understand the code in the light of that.

If you don't like my code then there are plenty of other examples for example:-
http://www.google.co.uk/url?q=http://www.arduino.cc/playground/Code/I2CPortExpanderAndLCDs&sa=X&ei=1F7MS8-PM6Ti0ATMlL2HDA&ved=0CBEQzgQoADAA&usg=AFQjCNFYXvaGxPywbLg4Kp_PqcuouZsjyQ
Just search for arduino MCP23008

i have now completly remade the code and it does work now as i want it to work (this program is made for one MCP23008, i'm need a total of 4 but that shoudn't be a problem)

#include <Wire.h>

byte tmp;
int variable[8];
int calc1 = 0;
int calc2 = 0;
int calc3 = 0;
int calc4 = 0;
int calc5 = 0;
int calc6 = 0;
int calc7 = 0;
int calc8 = 0;
int compleetcalc = 0;

void setup() {
   Wire.begin();                      // initialize the wire library and hardware
   Serial.begin(9600);             // initialize serial communication
   
   
//First the DDR is 0 and 0 = output , 1 = input  
Wire.beginTransmission(0x27); // 0x20 + (A2*4) + (A1*2) + (A0*1) 
Wire.send(0);//< ----- this is the DDR
Wire.send(255);//< ----- 255 (B11111111) = all inputs
Wire.endTransmission();

}



void loop() {
  
//data read
Wire.beginTransmission(0x27);
Wire.send(9);                            // set the register from where you want to read from
Wire.endTransmission();

Wire.requestFrom(0x27, 1);      // receive one byte from MCP23008 with address 0x27 
tmp = Wire.receive();              // write the received byte to the variable byte tmp
Serial.println("data: ");            // print text to serial port
Serial.println(tmp, BIN);          // print the value of tmp to the serial port
delay(500);                            // wait for 500ms

  CrackByte( tmp, variable ); // split the received byte in void Crackbyte

calc1 = (variable[7]*1);        // multiply the value of tmp bit 7 with 1
calc2 = (variable[6]*2);        // multiply the value of tmp bit 6 with 2
calc3 = (variable[5]*3);        // multiply the value of tmp bit 5 with 3
calc4 = (variable[4]*4);        // multiply the value of tmp bit 4 with 4
calc5 = (variable[3]*5);        // multiply the value of tmp bit 3 with 5
calc6 = (variable[2]*6);        // multiply the value of tmp bit 2 with 6
calc7 = (variable[1]*7);        // multiply the value of tmp bit 1 with 7
calc8 = (variable[0]*8);        // multiply the value of tmp bit 0 with 8

// add all calcs
compleetcalc = (calc1 + calc2  + calc3  + calc4  + calc5  + calc6  + calc7  + calc8 ); 

Serial.println(calc1);           // print value of calc1 to serial port
Serial.println(calc2);           // print value of calc2 to serial port
Serial.println(calc3);           // print value of calc3 to serial port
Serial.println(calc4);           // print value of calc4 to serial port
Serial.println(calc5);           // print value of calc5 to serial port
Serial.println(calc6);           // print value of calc6 to serial port
Serial.println(calc7);           // print value of calc7 to serial port
Serial.println(calc8);           // print value of calc8 to serial port
Serial.println(compleetcalc);           // print value of compleetcalc to serial port

}


void CrackByte( byte tmp, int variable[8] )  // split the received byte
{
  byte i;
  
  for ( i=0; i < 8; ++i )
  {
    variable[i] = tmp & 1;
    tmp = tmp >> 1;
  }
}

and i must admit i did not read the data sheet correctly, have read it but not the right way. :o :-X
but the other post on the forum and the link you've posted has helped :wink:

(i posted the code so other people can use it as an example)