I2C Tutorial: Digital I/O Expansion with PCF8574 I2C Module

Veröffentlicht von

We’ve all been there. You start a project with one microcontroller or dev board in mind, but after a few revisions, added features, and a sensor or two that you hadn’t considered, you’re out of I/O. You could choose to switch to a more capable controller at this point, but what if—for whatever reason—you’d prefer to stick with your original board?

The good news is that there are several ways to expand your device’s I/O, including multiplexing and digital I/O expansion via an SPI or I2C interface. One popular interface is the PCF8574 and PCF8574A IC, made by Texas Instruments and NXP, which we’ll discuss more later in this article. Each of these integrated circuits gives the user eight extra GPIO pins, and each circuit can operate between 2.5 and 6V.

You can also use multiple modules together by setting each module to one of eight addresses via three selection pins. PCF8574 chips are set to hexadecimal addresses from 0x20 to 0x27, while PCF8574A chips are set to 0-38 through 0x3F. This otherwise identical design allows for up to 128 expansion I/O to exist on a single bus, with eight of each type occupying all possible addresses. That should be enough, right?

Beyond simply expanding the number of I/O available for your project, this type of IC is very handy for wiring consolidation, as you’ll only need one set of SCL and SDA lines (plus power and ground) to access a wide range of modules.

Getting Started

Feel free to use any microcontroller capable of I2C communication for this kind of control. As an introduction to the technology, I’ll demonstrate its use with an Arduino Nano. Along with the Arduino, I’ll use a development module by Waveshare which breaks out an onboard PCF8574 IC’s I/O, interrupt pin, Vcc, GND, SDA, and SCL inputs. The development module also provides a trio of jumpers for address selection. The board includes male headers for I2C connections on one end, and corresponding female connections on the other. With this setup, you can physically stack multiple modules on top of each other—up to 16, as we previously noted.

To calculate which address you have, you could use both jumper position and whether or not you have the /A version, but there’s an easier way. To figure out the address and check your setup at the same time, use an I2C scanner.

1. Load the program onto your Arduino

2. Open a serial port monitor to see the address

You can use the jumpers to change the address as needed or record them for interfacing in the next step.

LED Test 

Here’s a quick guide to how to perform the first test.

1. Hook up the SDA pin on the PCF8574 module to Nano pin A4, then SCL to A5.

2. Attach 5V to VCC and Arduino ground to the PCF8574 module ground.

3. Next, connect a pair of LEDs to the output P0 and P1 on your PCF8574 module with the appropriate resistors. You’ll notice that they turn on immediately, regardless of which I/O you’re using and without any other interaction. The modules emit a weak positive voltage signal on powerup, something that you’ll want to keep in the back of your mind for future consideration.

4. Install the PCF8574 library on your Arduino IDE, which will allow the program to interact with the device easily.

5. Load “2-LED-Blink” example to your board, changing the I2C address to match your module.

If you’ve set everything up correctly, the LEDs will blink with 250ms between transition. This signal demonstrates the new outputs at your disposal. You can expand them as needed to many other situations.

Button Input

You can use these modules to multiply sensors on user inputs as well. Wire a button from +5V on your Arduino board to input P0 on the PCF8574 module. Optionally, you can also add a pulldown resistor to keep the input from floating, as you can see in the image above. However, this step didn’t appear necessary during our testing. An LED with an appropriate resistor connects to P1. The Input Response code will light up the LED when you press the button, and it will also generate serial signals for the state of the input if you need to do any troubleshooting.

You can see the most notable portion of this code on line 24:

“uint8_t val = pcf8574.digitalRead(P0);”

This code reads only the P0 input and assigns it to the variable “val.” Alternately, we can read all digital pins in one command using “digitalReadAll();”.

Interrupt Pin

Along with its purely I2C interface, PCF8574 modules feature an “INT” pin that outputs a signal based on input changes. You can wire this directly to the Arduino’s interrupt pin to signal a change in the module’s input state. The microcontroller can then check this I2C device immediately for faster I/O response.

More Inputs

It’s easy to see how up to 128 extra I/O pins would be extremely helpful in many applications. If you need even more pins, theoretically, you could employ a multiplexer setup to create additional I2C buses, multiplying the potential number of additional I/O pins to an extraordinary degree. Whether you ran out of I/O and you want a few more to complete your build, or you’re planning a build that will need a vast number of I/O points, the I/O expander module could be exactly what you need.

Overstock AV Page Footer 560x140 2 1 1

Neue Beiträge

Leider ergab Ihre Suche kein Ergebnis

Aktuelles über Elektronikkomponenten­

Wir haben unsere Datenschutzbestimmungen aktualisiert. Bitte nehmen Sie sich einen Moment Zeit, diese Änderungen zu überprüfen. Mit einem Klick auf "Ich stimme zu", stimmen Sie den Datenschutz- und Nutzungsbedingungen von Arrow Electronics zu.

Wir verwenden Cookies, um den Anwendernutzen zu vergrößern und unsere Webseite zu optimieren. Mehr über Cookies und wie man sie abschaltet finden Sie hier. Cookies und tracking Technologien können für Marketingzwecke verwendet werden.
Durch Klicken von „RICHTLINIEN AKZEPTIEREN“ stimmen Sie der Verwendung von Cookies auf Ihrem Endgerät und der Verwendung von tracking Technologien zu. Klicken Sie auf „MEHR INFORMATIONEN“ unten für mehr Informationen und Anleitungen wie man Cookies und tracking Technologien abschaltet. Das Akzeptieren von Cookies und tracking Technologien ist zwar freiwillig, das Blockieren kann aber eine korrekte Ausführung unserer Website verhindern, und bestimmte Werbung könnte für Sie weniger relevant sein.
Ihr Datenschutz ist uns wichtig. Lesen Sie mehr über unsere Datenschutzrichtlinien hier.