Components · 8 min read

PCF8574 Arduino: I2C I/O Expander for 8 Extra Pins, with Code

PCF8574 Arduino guide: 8 extra I/O pins over I2C, A0-A2 address table, wiring LEDs and buttons, INT interrupt, the I2C LCD backpack and MCP23017 comparison.

The PCF8574 is a cheap I2C I/O expander that gives your Arduino 8 extra input/output pins over just two wires (SDA and SCL). Using a PCF8574 with Arduino pays off whenever you run out of pins: when an LCD, a sensor and a few relays have already taken most of them, the PCF8574 simply joins the same I2C bus and hands you 8 more pins for buttons and LEDs. With the A0, A1 and A2 address pins you can put up to 8 modules on one bus and reach 64 extra pins.

In this guide you will learn about the PCF8574's quasi-bidirectional pins, why that makes LEDs light up when you write LOW, and how to read buttons. You will get the address table (PCF8574 and PCF8574A), read buttons through the INT pin with an interrupt, discover that the I2C LCD backpack you use every day is really a PCF8574, and see how it compares with the MCP23017. The code is ready for Arduino Uno, ESP32 and Raspberry Pi Pico (MicroPython).

I2C module with a PCF8574 chip
I2C module: the PCF8574 on it connects 8 pins to the two-wire I2C bus; LCD backpacks use the very same chip.

What is the PCF8574 and how does it work?

Inside the PCF8574 is an 8-bit output latch. The Arduino sends a single byte over I2C and each bit sets the state of one pin: P0 is the least significant bit, P7 the most significant. The pins do not behave like classic push-pull outputs. When LOW, a pin is tied to GND through a strong switch and can sink up to 25 mA. When HIGH, it is held up only by a weak current source of about 100 µA. This is called a quasi-bidirectional design, and it has two important consequences: wire LEDs so the pin sinks the current (anode to VCC), and write a 1 to a pin before using it as an input. When a button pulls that HIGH pin to GND, you read a 0. At power-up all pins are HIGH.

FeatureValue
Pins8 (P0–P7), quasi-bidirectional
Supply2.5–6 V (works at 3.3 V and 5 V)
InterfaceI2C, up to 100 kHz
AddressPCF8574: 0x20–0x27, PCF8574A: 0x38–0x3F
LOW output currentUp to 25 mA per pin (sink)
HIGH output currentAbout 100 µA (weak pull-up)
InterruptINT pin, open-drain, active LOW
PackageDIP-16 (PCF8574P) and SO-16 (PCF8574T)

PCF8574 pinout and wiring

PCF8574 / moduleArduino UnoESP32Raspberry Pi Pico
VCC5V3V33V3 (OUT)
GNDGNDGNDGND
SDAA4GPIO 21GP4
SCLA5GPIO 22GP5
INT (optional)D2GPIO 4GP6
P0–P3LEDs: anode to VCC through a resistor, cathode to the pinSameSame
P4–P7Buttons: one leg to the pin, the other to GNDSameSame

In the examples, four LEDs sit on P0–P3 and four buttons on P4–P7. Each LED's anode goes to VCC through a 330 Ω resistor and its cathode to the PCF8574 pin; when you write 0 to the pin, current flows through the LED into the pin and the LED lights up. At 5 V, 330 Ω means (5 - 2) / 330 ≈ 9 mA for a red LED; pick the right value for your LED with the LED resistor calculator. The buttons need no resistor of their own, because the chip's weak current source acts as a pull-up. On long breadboard wires an external 10 kΩ pull-up still helps against noise; the basics are in pull-up and pull-down resistors. If you use the bare chip in a DIP-16 package, pins 1–3 are A0–A2, 4–7 and 9–12 are P0–P7, 8 is GND, 13 INT, 14 SCL, 15 SDA and 16 VCC.

If you wire an LED the other way round, with the anode on the PCF8574 pin and the cathode to GND, it will not light or will glow very faintly, because a HIGH pin only supplies about 100 µA. With an ESP32 or Pico, power the module from 3.3 V: on 5 V its pull-up resistors pull SDA and SCL to 5 V, which can damage the pins of 3.3 V boards. When many LEDs are on at once, keep the total current below 80 mA.

Setting the address: A0, A1, A2 and the PCF8574A

A2A1A0PCF8574PCF8574A
0000x200x38
0010x210x39
0100x220x3A
0110x230x3B
1000x240x3C
1010x250x3D
1100x260x3E
1110x270x3F

The address depends on whether A0–A2 are tied to GND or VCC; in the table 1 means VCC and 0 means GND. The common purple expander boards have jumpers or small DIP switches for these three pins, usually set to 0x20 out of the box. Eight modules of the same type give you 64 pins, and mixing the PCF8574 with the PCF8574A gets you 16 modules and 128 pins. If you are not sure about the address, run the I2C scanner from what is I2C or the I2C LCD display guide; if the chip marking contains an "A", the address range is 0x38–0x3F.

First sketch without a library: reading buttons and driving LEDs

C++ (Arduino IDE)
1// PCF8574: P0-P3 LED (LOW = yanar), P4-P7 düğme (GND'ye) / P0-P3 LEDs (LOW = on), P4-P7 buttons (to GND)
2#include <Wire.h>
3
4const uint8_t PCF_ADDR = 0x20;           // A0, A1, A2 = GND
5uint8_t lastButtons = 0x0F;
6
7void pcfWrite(uint8_t value) {
8  Wire.beginTransmission(PCF_ADDR);
9  Wire.write(value);                     // 8 pinin hepsi tek baytta / all 8 pins in one byte
10  Wire.endTransmission();
11}
12
13uint8_t pcfRead() {
14  Wire.requestFrom(PCF_ADDR, (uint8_t)1);
15  return Wire.available() ? Wire.read() : 0xFF;
16}
17
18void setup() {
19  Serial.begin(9600);
20  Wire.begin();                          // Uno: SDA = A4, SCL = A5
21  pcfWrite(0xFF);                        // hepsi HIGH: LED'ler sönük, girişler hazır / all HIGH: LEDs off, inputs ready
22}
23
24void loop() {
25  uint8_t buttons = pcfRead() >> 4;      // P4-P7, basılı = 0 / pressed = 0
26  // Üst 4 bit 1 kalmalı (giriş); basılı düğme kendi LED'ini yakar
27  // Upper 4 bits must stay 1 (inputs); a pressed button lights its LED
28  pcfWrite(0xF0 | buttons);
29  if (buttons != lastButtons) {
30    lastButtons = buttons;
31    Serial.print("Buttons P7..P4: ");
32    Serial.println(buttons | 0x10, BIN); // baştaki 1 sıfırları göstermek için / leading 1 keeps the zeros visible
33  }
34  delay(20);
35}

You do not even need a library to talk to a PCF8574: one byte sent with Wire.write() sets all 8 pins at once, and one byte read with Wire.requestFrom() returns the current state of all 8 pins. Shifting the byte 4 bits to the right moves the buttons on P4–P7 into the lower four bits, and a pressed button reads as 0. When writing, 0xF0 | buttons does two jobs: it keeps the upper four bits at 1 so P4–P7 stay inputs, and it copies the button states into the lower four bits. Because a pressed button's bit is 0, the matching LED pin goes LOW and the LED lights. If bit operations are new to you, number systems and bit operations is a good place to start.

The INT pin: reading buttons with an interrupt instead of polling

The sketch above keeps the I2C bus busy dozens of times per second. The PCF8574's INT pin goes LOW as soon as any input has changed since the last read and stays there until the chip is read or written. Connect it to an Arduino interrupt pin and you only read when something changes; the rest of the time the bus and the processor stay available for other work. The next example uses Rob Tillaart's PCF8574 library, which you can install from the Library Manager. From version 0.4 of the library on, you have to call Wire.begin() yourself before pcf.begin().

C++ (Arduino IDE)
1// PCF8574 + INT pini: yalnız değişiklik olunca oku / read only when something changes
2#include <Wire.h>
3#include <PCF8574.h>                     // Rob Tillaart
4
5#ifndef IRAM_ATTR
6#define IRAM_ATTR                        // AVR'de boş / empty on AVR
7#endif
8
9#if defined(ESP32)
10const int INT_PIN = 4;                   // PCF8574 INT -> GPIO 4
11#else
12const int INT_PIN = 2;                   // PCF8574 INT -> D2 (Uno kesme pini / Uno interrupt pin)
13#endif
14
15PCF8574 pcf(0x20);
16volatile bool changed = false;
17uint8_t lastState = 0xFF;
18
19void IRAM_ATTR onPcfInt() {
20  changed = true;                        // yalnız bayrak kaldır / just raise a flag
21}
22
23void setup() {
24  Serial.begin(115200);
25#if defined(ESP32)
26  Wire.begin(21, 22);                    // SDA, SCL
27#else
28  Wire.begin();
29#endif
30  if (!pcf.begin(0xFF)) Serial.println("PCF8574 not found");
31  pinMode(INT_PIN, INPUT_PULLUP);        // INT open-drain çıkış, pull-up şart / INT is open-drain, needs a pull-up
32  attachInterrupt(digitalPinToInterrupt(INT_PIN), onPcfInt, FALLING);
33}
34
35void loop() {
36  if (!changed) return;
37  delay(20);                             // kontaklar otursun (tuş arkı) / let the contacts settle (bounce)
38  changed = false;
39  uint8_t state = pcf.read8();           // okumak INT'i sıfırlar / reading clears INT
40  uint8_t fell = lastState & ~state;     // 1'den 0'a inen bitler = yeni basışlar / bits that went 1 -> 0 = new presses
41  lastState = state;
42  for (uint8_t i = 4; i < 8; i++) {
43    if (fell & (1 << i)) {
44      pcf.toggle(i - 4);                 // P4 -> P0 LED, P5 -> P1 LED ...
45      Serial.print("LED P");
46      Serial.println(i - 4);
47    }
48  }
49}

The interrupt routine only raises a flag, because I2C traffic inside an interrupt causes trouble on both the Uno and the ESP32. When loop() sees the flag, it waits 20 ms for the button contacts to stop bouncing, then reads the whole port with read8(), which also clears INT. The expression lastState & ~state finds the bits that went from 1 to 0, in other words new button presses. Each new press flips the matching LED with toggle(); since toggle() works from the output byte the library keeps, the input pins still get a 1. For the general idea behind interrupts, see Arduino interrupts.

The INT pin is an open-drain output and cannot go HIGH on its own, so INPUT_PULLUP on the Arduino or an external 10 kΩ pull-up is required. You can tie the INT pins of several PCF8574s to the same interrupt line; when it fires, read them one after another to find the one that changed. On the ESP32 the interrupt routine must be marked IRAM_ATTR; the #ifndef block in the code turns that word into nothing on the Uno.

The I2C LCD backpack is really a PCF8574

Take a close look at the blue or black I2C board soldered to the back of a 16x2 LCD and you will find a chip marked PCF8574T or PCF8574AT. The LiquidCrystal_I2C library drives the 8 pins of that chip as if they were wired straight to the LCD's parallel interface and sends data in 4-bit mode. That is also why the LCD address is usually 0x27 or 0x3F: on the backpack, A0–A2 are pulled up to VCC, so all three bits are 1, which gives 0x27 on a PCF8574T and 0x3F on a PCF8574AT. Bridge the A0–A2 solder pads on the back to change the address and run several LCDs on the same bus.

PCF8574 pinLCD connection
P0RS (command or data)
P1RW (read or write)
P2E (enable)
P3Backlight (through a transistor)
P4–P7D4–D7 (4-bit data bus)
2x16 LCD with I2C module
2x16 LCD with I2C module: the PCF8574 on the back cuts the LCD's 16-pin parallel interface down to 4 wires (GND, VCC, SDA, SCL).

Using the PCF8574 with ESP32 and Raspberry Pi Pico

On the ESP32 both examples run unchanged; the only difference is that Wire.begin(21, 22) names the I2C pins explicitly, and the interrupt example already does that inside its #if defined(ESP32) block. On the Raspberry Pi Pico, MicroPython's machine.I2C class does the same job in a few lines: writeto() writes a byte and readfrom() reads one. The code below scans the I2C bus at startup, shows a running light on P0–P3 while no button is pressed and lights the matching LED when you press a button. Stay at 100 kHz; the PCF8574 datasheet does not guarantee higher speeds.

MicroPython
1# Raspberry Pi Pico + PCF8574 (MicroPython): P0-P3 LED, P4-P7 düğme / P0-P3 LEDs, P4-P7 buttons
2from machine import Pin, I2C
3import time
4
5i2c = I2C(0, sda=Pin(4), scl=Pin(5), freq=100000)   # PCF8574 en fazla 100 kHz / max 100 kHz
6ADDR = 0x20
7print("I2C:", [hex(a) for a in i2c.scan()])          # 0x20 görünmeli / 0x20 should appear
8
9
10def pcf_write(value):
11    i2c.writeto(ADDR, bytes([value]))
12
13
14def pcf_read():
15    return i2c.readfrom(ADDR, 1)[0]
16
17
18pcf_write(0xFF)                       # hepsi HIGH: LED'ler sönük / all HIGH: LEDs off
19led = 0
20while True:
21    buttons = pcf_read() >> 4         # basılı = 0 / pressed = 0
22    if buttons != 0x0F:
23        out = 0xF0 | buttons          # düğmeyi LED'e aynala / mirror buttons to LEDs
24    else:
25        out = 0xF0 | (~(1 << led) & 0x0F)   # yürüyen ışık / running light
26        led = (led + 1) % 4
27    pcf_write(out)
28    time.sleep_ms(150)

PCF8574 vs MCP23017 vs 74HC595

FeaturePCF8574MCP2301774HC595
Extra pins8168 outputs (chainable)
InterfaceI2C, 2 wiresI2C, 2 wires (SPI version MCP23S17)3-wire serial (SPI-like)
Pin directionQuasi-bidirectionalEach pin set as input or outputOutput only
Output currentSinks 25 mA when LOW, very weak when HIGH25 mA per pin, source and sinkA few mA per pin recommended
Internal pull-upWeak current source (fixed)100 kΩ, enabled in softwareNone
Interrupt1 INT2 INT (INTA, INTB)None
I2C speed100 kHzUp to 1.7 MHz-
Best forButtons, LEDs and simple I/OMany pins, real output current and speedLots of LEDs and 7-segment displays

If you only need to read a few buttons and light a few LEDs, the PCF8574 is the cheapest and simplest option. If you want to drive LEDs with HIGH, set the direction per pin, get 16 pins in one chip or run at 400 kHz and beyond, move up to the MCP23017. If you only need outputs for many LEDs or 7-segment displays, the 74HC595 shift register is faster and even cheaper.

PCF8574 project ideas

  • Relay board: Drive an eight-channel relay module from a single I2C bus; active-LOW relay inputs match the PCF8574's logic perfectly.
  • Keypad: Connect the 8 lines of a 4x4 keypad to the PCF8574 and save the Arduino's own pins for other parts.
  • Button panel: Read 8 buttons for a game or a remote through the INT interrupt.
  • Status display: Show the alarm state of several sensors on a bar of 8 LEDs.
  • Multiple displays: Run two LCDs on the same Arduino using backpacks with different addresses.
  • Knight Rider effect: Run a scanning light through the PCF8574 in a project that has no pins left.

Common problems and fixes

SymptomLikely causeFix
Does not show up in the I2C scanSDA and SCL swapped or no common groundCheck the wiring; on the Uno SDA = A4, SCL = A5
Address differs from what you expectedChip is a PCF8574A or A0–A2 set differentlyCheck the 0x38–0x3F range and the jumpers
LED does not light or is very dimTrying to drive the LED with HIGHAnode to VCC through a resistor, cathode to the pin; write 0
Button always reads 0 or always 1A 0 was written to the input pin earlierSend a 1 to the inputs on every write (0xF0 | ...)
Interrupt fires once and never againPort not read, so INT stays LOWRead the port with read8() after every interrupt
Communication fails with many modulesParallel pull-up resistors add up to too low a valueRemove the SDA/SCL resistors on some modules and shorten the wires

The PCF8574 solves pin shortages with two wires and is easy to learn; once the quasi-bidirectional logic clicks, you can run LEDs, buttons, relays and LCDs on the same bus. In the BlueGrays app you will find I2C LCD, calculator, keypad, running light and button projects with step-by-step circuit diagrams and code, ready to be extended with a PCF8574 when you run out of pins.

Frequently asked questions

How do I find the I2C address of a PCF8574?

Depending on A0–A2, the PCF8574 sits between 0x20 and 0x27 and the PCF8574A between 0x38 and 0x3F. The surest way is to run an I2C scanner sketch and read the address in the Serial Monitor. On LCD backpacks it is usually 0x27 or 0x3F.

How many PCF8574 modules can I connect to one Arduino?

Each PCF8574 adds 8 pins and A0–A2 allow 8 addresses, so modules of one type give you 64 pins. Because the PCF8574 and PCF8574A use different address ranges, mixing them gets you to 16 modules and 128 pins. With many modules, watch the combined value of the I2C pull-up resistors.

Why does the LED on a PCF8574 light up when the pin is LOW?

PCF8574 pins can sink up to 25 mA when LOW but only source about 100 µA when HIGH. That is why the LED's anode goes to VCC through a resistor and its cathode to the pin, so writing 0 turns it on. Wired the other way round, the LED stays dark or very dim.

How do I read a button with the PCF8574?

First write a 1 to the pin so it rests HIGH through the weak pull-up. When the button pulls the pin to GND, you read a 0. If you write a 0 to an input instead, it is held LOW permanently and the button can no longer be read.

PCF8574 or MCP23017: what is the difference?

The PCF8574 is a simple chip with 8 quasi-bidirectional pins and a 100 kHz limit. The MCP23017 offers 16 pins, per-pin direction, switchable pull-ups, two interrupt outputs and I2C speeds up to 1.7 MHz. For simple button and LED jobs the PCF8574 is enough; for bigger projects the MCP23017 is the better pick.

Does the PCF8574 work with an ESP32 at 3.3 V?

Yes, the PCF8574 runs from 2.5 to 6 V, so it works directly with the ESP32 and Raspberry Pi Pico. Power it from 3.3 V; at 5 V the pull-ups drag SDA and SCL to 5 V, which can harm 3.3 V boards. On the ESP32, set the I2C pins with Wire.begin(21, 22).

See all projects
LED Chaser
AR-5Intermediate

LED Chaser

Lights that switch on one after another, as if they were running. The Arduino lights six LEDs one at a time; each glows for a brief moment and goes out, so the light walks from start to end and then…

4.6(87)
218
Knight Rider LED Effect
AR-6Intermediate

Knight Rider LED Effect

The classic scanner light: a dot of light slides from one end of six LEDs to the other and then comes back.

4.8(107)
214
LED Control with a Push Button (Push)
AR-9Beginner

LED Control with a Push Button (Push)

Just like a doorbell: the light stays on as long as you press, and goes off when you let go.

4.8(114)
253
Toggle an LED with a Button (On/Off)
AR-10Beginner

Toggle an LED with a Button (On/Off)

Press the button once and the light comes on, press it again and it goes off, just like the light switch in your room.

4.7(103)
177
Using LCD Screen with I2C
AR-32Beginner
Premium

Using LCD Screen with I2C

Give your Arduino its own screen and make it write its first message. The two-line LCD screen hooks up with just a few wires; a greeting appears on the top line, then a second message on the bottom…

4.8(53)
70
ESP32 I2C LCD Display (16x2)
ES-17Intermediate
Premium

ESP32 I2C LCD Display (16x2)

In this project we connect a 16x2 character LCD to the ESP32 over I2C and display text. With I2C, just 2 data pins (SDA and SCL) control the screen, instead of the 6+ pins a classic parallel connection wastes. The code prints a fixed message on the first line and a counter that goes up every second on the second. At startup an I2C scanner also prints the display's address to the Serial Monitor (usually 0x27 or 0x3F).

4.9(23)
40
Arduino I2C Calculator
AR-94Intermediate
Premium

Arduino I2C Calculator

It is an Arduino-based calculator project that performs addition, subtraction, multiplication and division operations using a 4x4 membrane keypad and a 16x2 I2C LCD display. Thanks to I2C, the display connection is provided with only 2 cables, thus reducing cable clutter and keeping more pins open for use. There is also decimal number input and negative number support.

5.0(31)
43
4x4 Keypad Operation
AR-63Beginner
Premium

4x4 Keypad Operation

On the 4x4 keypad, which looks a lot like a phone's buttons, the Arduino instantly knows which key you pressed and writes it to your computer screen.

4.5(27)
35