Question regarding PCF8574P usage

Hello guys, I'm pretty new with Arduino and Electronics in general, so please forgive me if this is a common or easy (or even dumb) question, but I simply can't figure out why this is happening.

What I'm trying to do should be very easy, I'm trying to expand the I/O ports of the Arduino, I've found out this tutorial, since the PCF8574 is fairly cheap and easily available where I live, I decided to give it a try.
Well, I've got no luck on lighting up the LEDs, and after fiddling around with the Wire Library, I've managed to get....something. The LEDs were flashing, however in a completely random way. What I did was to create a loop and sending the write signal on it, something like this:

for(int i=0;i<255;i++){
  Wire.beginTransmission(32);
  Wire.write(i);
  Wire.endTransmission();
}

Also, the LEDs were very, very dim. After looking a little more, I've realized they were all lit, and I was actually turning one or another off! That made no sense to me. I've attached the negative end of the LED on the GND and the positive to the ports of the PCF8574P, it should be like this right?
Anyway, afterward I've found this tutorial, and in it, it's actually connecting the LED directly to the 5V (with the resistor in between) and the negative on the PCF8574P, then in the program he negates the address he's writing (~0x01, instead of 0x01 for example), that made no sense to me, but it worked like a charm. Now, can anyone please explain me why? I've attached the Fritzing sketch so you can see how the connections were made.
Also, you can find the datasheet here.

Thanks for the help!

Time to read the datasheet: http://www.nxp.com/documents/data_sheet/PCF8574.pdf

When set HIGH the outputs source a maximum of 300 MICROamps (0.3 mA). That is so you can use them as inputs. When set LOW they can sink about 20 mA. That is why you connect the LED's between +5 and the output and you write it LOW to turn it on.

As usual, it's something that's right in front of me...add this to the fact that I'm not very good reading datasheets...and you have this simple questions!
Thank you so much for explaining this to me.
One last question, which is better, the PCF8574P or the CD4051? Now that I've answered this question, the code makes sense and the I2C protocol, well, I found it extremely handy. I've never used the CD4051, only read about it, was thinking of getting a couple of those and trying it, but if someone could give me a point saying their pros and cons, it would help a lot.

Thanks once again.

Zarnick:
One last question, which is better, the PCF8574P or the CD4051? Now that I've answered this question, the code makes sense and the I2C protocol, well, I found it extremely handy. I've never used the CD4051, only read about it, was thinking of getting a couple of those and trying it, but if someone could give me a point saying their pros and cons, it would help a lot.

Totally different functions.

Look up the datasheets! Then explain what you thought the 4051 might do?

Ok, sorry but let me see if I got this correct. If I use a Multiplexer like the CD4051, I'm not "expanding" the I/O ports but simply using another medium to transfer their data? Like telephone companies using one cable to pass over data of hundreds of land line telephones? While the PCF8574P is actually a port expander, expanding the I/O ports using only the 2 wires for the I2C bus.
Did I got it right?

Thanks for all the help.

The 4051 is an eight-way analog switch. You can use it to expand the number of inputs but since you can connect the switch to only one pin at a time you can't use it to drive more than one output at a time. It is typically used to expand the number of analog inputs but can also be used for digital inputs.

For digital output expansion many people use a 74HC595 shift register (serial in, parallel out) and the shiftOut() function or the SPI bus if you need high speed.

For digital input expansion many people use a 74HC165 shift register (parallel in, serial out) and the shiftIn() function or the SPI bus if you need high speed.

Thanks John, that explained a lot.
So basically I could user both for the same thing (expanding I/O ports) right?

Zarnick:
So basically I could user both for the same thing (expanding I/O ports) right?

If by "both", you mean either a PCF8574P or a 74HC595, yes. The PCF8574P is more flexible as you can use it for inputs if you wish to.

There are pros and cons. I2C uses only 2 wires but you tend to need a library for it as it is moderately complex; you need to use certain pins (or the library becomes more complex again) and you can only employ eight PCF8574Ps on those 2 wires without further complication.

The PCF8574P requires 3 pins for pretty much as many as you need but there are ways of sharing two of the wires; the code is dead simple and you really should not need to use a library. It is substantially cheaper, simpler to use and that code is faster into the bargain.

Thanks for the reply Paul, I believe the second part (saying of the 3 pins and that the code is dead simple) would be about the 74HC595 right? I have no problem with the programming part and really liked the way the PCF8574P works (know that I know how :wink: ). Also, did a little fun attaching more then one and, apart from the cable mayhem on the breadboard, found it was really easy and intuitive (fix the gnd in different pins and you have a new address).
I've ordered the 74HC595 anyway (it IS really cheap) and as soon as it arrives I'll be fiddling with it to see what I think is easier and best.

Only one extra question, between the 74HC595 and the PCF8574P, the PCF8574P is the only one I could user for inputs?

Zarnick:
Only one extra question, between the 74HC595 and the PCF8574P, the PCF8574P is the only one I could user for inputs?

Yes. You would use a 74HC165 for inputs. If you use the SPI interface you can clock data out to a 74HC595 and in from a 74HC165 at the same time.

Ok, so I do believe I'll be using the PCF8574P, at least I have more options in a single chip :wink:

Thanks for all the help!