Basics · 10 min read

Arduino Not Enough Pins? 74HC595, MCP23017 and Multiplexers

Arduino not enough pins? Use analog pins as digital, add 74HC595/74HC165, PCF8574/MCP23017 and CD74HC4067. Comparison table and when to pick Mega or ESP32.

The Arduino not enough pins problem is something almost everyone runs into as projects grow. The Uno has 14 digital and 6 analog pins; a 16x2 LCD takes 6, a 4x4 keypad takes 8, and once you add a few relays and sensors they're all gone. The good news is that you usually don't need a new board. You can use the analog pins as digital ones, get dozens of outputs from 3 pins with cheap chips like the 74HC595, or hang more than a hundred inputs and outputs on the 2 pins of the I2C bus.

In this guide you'll first see how to use the pins you already have without wasting them, then the four main expansion methods with working code: the 74HC595 for outputs, the 74HC165 for inputs, the PCF8574 and MCP23017 bidirectional I2C expanders, and the CD74HC4067 multiplexer for analog inputs. At the end there's a table comparing every method by pin count and speed, plus a decision guide on when moving to an Arduino Mega or an ESP32 makes more sense.

Arduino Nano pins: the not enough pins problem
Arduino Nano: same processor as the Uno, but two extra analog inputs thanks to A6 and A7.

First count what you have: how many pins does the Uno really have?

The Uno has 20 usable I/O pins: D0–D13 and A0–A5. Some of them, though, are reserved for other jobs. D0 and D1 are the serial (USB) line; connect something there and uploading and the Serial Monitor will act up. D13 is wired to the onboard LED and adds a slight load when used as an input. A4 and A5 are also the I2C SDA and SCL lines, and D10–D13 are SPI. So in a project that uses I2C and SPI, the number of truly free pins drops to 12–14. For the full list see the Arduino Uno pinout guide or the pinout tool.

Method 1: using analog pins as digital (A0 = 14)

This is the easiest fix, and most people don't know it: the Uno's A0–A5 are fully featured digital pins. Arduino numbers them 14 to 19; the constant A0 is 14 on the Uno. pinMode(A0, OUTPUT), digitalWrite(A1, HIGH) or pinMode(A2, INPUT_PULLUP) work just like on D2, and they have internal pull-up resistors too. That takes you from 14 digital pins to 20 without a single extra part. The only exception is A6 and A7 on the Nano and Pro Mini: they are analog inputs only, can't be read digitally and can't be outputs.

C++ (Arduino IDE)
1// Analog pinleri dijital kullanmak (Uno): A0-A5 = 14-19
2// Using analog pins as digital (Uno): A0-A5 = 14-19
3const int LED_PINS[] = {A0, A1, A2};   // 14, 15, 16: dijital çıkış / digital outputs
4const int BUTTON_PIN = A3;             // 17: dijital giriş / digital input
5int step = 0;
6
7void setup() {
8  Serial.begin(9600);
9  for (int i = 0; i < 3; i++) {
10    pinMode(LED_PINS[i], OUTPUT);
11  }
12  pinMode(BUTTON_PIN, INPUT_PULLUP);   // analog pinde de dahili pull-up var / internal pull-up works here too
13  Serial.print(F("A0 = "));
14  Serial.println(A0);                  // Uno'da 14 yazar / prints 14 on the Uno
15}
16
17void loop() {
18  if (digitalRead(BUTTON_PIN) == LOW) {          // basılıyken yürüyen ışık / chaser while pressed
19    for (int i = 0; i < 3; i++) {
20      digitalWrite(LED_PINS[i], i == step ? HIGH : LOW);
21    }
22    step = (step + 1) % 3;
23  } else {
24    for (int i = 0; i < 3; i++) {
25      digitalWrite(LED_PINS[i], LOW);
26    }
27  }
28  delay(150);
29}
30
On the Nano you can still use A6 and A7 to read buttons: wire the button between the pin and GND and a 10 kΩ resistor between the pin and 5 V, then treat analogRead(A6) < 500 as pressed. Connecting several buttons to a single analog pin through different resistors and working out which one was pressed from the reading is another old pin-saving trick.

Method 2: 8, 16 or 32 outputs from 3 pins with the 74HC595

The 74HC595 is a serial-in, parallel-out shift register. The Arduino sends data bit by bit on the DS line, the bits move one place on each clock pulse (SH_CP), and a latch pulse (ST_CP) moves all 8 bits to outputs Q0–Q7 at once. Its real power is chaining: connect the first chip's Q7' output to the second chip's DS input and you get 16 outputs from the same 3 pins; with 4 chips 32, with 10 chips 80. Each output comfortably supplies about 6 mA and the chip's total limit is around 70 mA, so use 330 Ω–1 kΩ resistors for LEDs and drive relays and motors through a transistor or a ULN2003. More details in the 74HC595 guide.

74HC595 pinNameConnection (chip 1)
16VCC5 V
8GNDGND
14DS (SER)Arduino D11
11SH_CP (SRCLK)Arduino D13
12ST_CP (RCLK)Arduino D10
10MR (SRCLR)5 V (reset not used)
13OEGND (outputs enabled); on a PWM pin it sets brightness
9Q7'Chip 2 pin 14 (DS)
15, 1–7Q0–Q7To LEDs through resistors
C++ (Arduino IDE)
1// İki 74HC595 ile 3 pinden 16 çıkış / 16 outputs from 3 pins with two 74HC595
2const int DATA_PIN  = 11;   // DS (pin 14)
3const int LATCH_PIN = 10;   // ST_CP / RCLK (pin 12)
4const int CLOCK_PIN = 13;   // SH_CP / SRCLK (pin 11)
5// 1. entegrenin Q7' (pin 9) çıkışı 2. entegrenin DS girişine / chip 1 Q7' (pin 9) to chip 2 DS
6
7void writeOutputs(uint16_t value) {
8  digitalWrite(LATCH_PIN, LOW);                              // kaydırma başlıyor / start shifting
9  shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, highByte(value));  // önce uzaktaki 2. entegre / far chip first
10  shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, lowByte(value));   // sonra 1. entegre / then the first chip
11  digitalWrite(LATCH_PIN, HIGH);                             // 16 çıkış aynı anda değişir / all 16 update at once
12}
13
14void setup() {
15  pinMode(DATA_PIN, OUTPUT);
16  pinMode(LATCH_PIN, OUTPUT);
17  pinMode(CLOCK_PIN, OUTPUT);
18  writeOutputs(0);                                           // hepsi sönük / all off
19}
20
21void loop() {
22  for (int i = 0; i < 16; i++) {                             // ileri / forward
23    writeOutputs((uint16_t)1 << i);
24    delay(60);
25  }
26  for (int i = 14; i > 0; i--) {                             // geri / back
27    writeOutputs((uint16_t)1 << i);
28    delay(60);
29  }
30}
31

The code sends a 16-bit number as two bytes. The byte sent first gets pushed all the way to the end of the chain, so highByte lands in the far second chip and lowByte in the near first one. While the latch pin is LOW the outputs don't change during shifting; the moment it goes HIGH all 16 LEDs update together, which prevents flicker. On the Uno, shiftOut() sends a byte in about 0.1 ms. We chose D11 and D13 on purpose: if you need more speed, you can use SPI.transfer() with the same wiring and send a byte in a few microseconds at 8 MHz.

Method 3: 8 buttons from 3 pins with the 74HC165

The 74HC165 is the opposite of the 74HC595: parallel-in, serial-out. When the SH/LD pin gets a short LOW pulse, the current state of inputs D0–D7 is copied into the chip; then on each clock pulse one bit flows out of the QH output to the Arduino. Chaining works here too: connect the second chip's QH output to the first chip's SER input (pin 10) and you read 16 buttons with the same 3 pins. The catch is that the first bit (H, i.e. D7) is ready before any clock pulse; Arduino's built-in shiftIn() raises the clock first and then reads, so it can miss that first bit. That's why the code below reads first and then clocks.

C++ (Arduino IDE)
1// 74HC165 ile 3 pinden 8 buton / 8 buttons from 3 pins with a 74HC165
2const int LOAD_PIN  = 7;    // SH/LD (pin 1)
3const int CLOCK_PIN = 6;    // CLK (pin 2); CLK INH (pin 15) GND'ye / to GND
4const int DATA_PIN  = 5;    // QH (pin 9)
5// Her giriş (D0-D7) 10 kΩ ile 5 V'a, buton girişten GND'ye / each input 10 kΩ to 5 V, button to GND
6
7byte lastState = 0xFF;
8
9byte readInputs() {
10  digitalWrite(LOAD_PIN, LOW);           // 8 girişin anlık görüntüsünü al / snapshot of the 8 inputs
11  delayMicroseconds(5);
12  digitalWrite(LOAD_PIN, HIGH);          // kaydırma moduna geç / switch to shift mode
13  byte value = 0;
14  for (int i = 7; i >= 0; i--) {         // ilk bit H (D7) / first bit is H (D7)
15    if (digitalRead(DATA_PIN)) bitSet(value, i);
16    digitalWrite(CLOCK_PIN, HIGH);       // sonraki biti getir / bring the next bit
17    delayMicroseconds(2);
18    digitalWrite(CLOCK_PIN, LOW);
19  }
20  return value;
21}
22
23void setup() {
24  Serial.begin(9600);
25  pinMode(LOAD_PIN, OUTPUT);
26  pinMode(CLOCK_PIN, OUTPUT);
27  pinMode(DATA_PIN, INPUT);
28  digitalWrite(LOAD_PIN, HIGH);
29  digitalWrite(CLOCK_PIN, LOW);
30}
31
32void loop() {
33  byte state = readInputs();
34  if (state != lastState) {              // yalnız değişince yaz / print only on change
35    lastState = state;
36    for (int i = 0; i < 8; i++) {
37      if (!bitRead(state, i)) {          // LOW = basılı / LOW = pressed
38        Serial.print(F("Buton / button D"));
39        Serial.println(i);
40      }
41    }
42  }
43  delay(20);                             // basit sıçrama önleme / simple debounce
44}
45
Never leave the 74HC165's unused inputs or its CLK INH pin (pin 15) floating. A floating CMOS input picks up noise and flips randomly between 0 and 1, and you'll see button presses that never happened. Put a 10 kΩ pull-up on every input, tie CLK INH to GND and connect the unused SER input to GND as well.

Method 4: the PCF8574 and MCP23017 I2C expanders

I2C expanders are the most flexible option because they give both inputs and outputs and use only SDA and SCL. Since these two lines are already shared with other I2C devices (LCD, RTC, sensors), the expander comes almost for free. Each chip's address is set with pins A0–A2, so 8 of them can sit on the same bus. The PCF8574 gives 8 pins and is the chip used on I2C LCD backpacks; its outputs are strong at sinking current and weak at sourcing it, so you connect an LED between the pin and 5 V and light it by driving the pin LOW. The MCP23017 offers 16 pins, programmable direction, internal pull-ups and interrupt outputs. For details see the PCF8574 guide and the what is I2C article.

FeaturePCF8574MCP23017
I/O count816 (two 8-bit ports)
Address range0x20–0x27 (PCF8574A: 0x38–0x3F)0x20–0x27
Max chips / bus8 (16 with the A version)8, 128 I/O in total
Direction settingNone; write 1 to a pin to use it as inputSet per pin (IODIR)
Pull-upWeak internal current sourceProgrammable 100 kΩ
Output currentSink 25 mA, source about 0.1 mA25 mA per pin
I2C speed100 kHz100 kHz, 400 kHz, 1.7 MHz
Interrupt output1 (INT)2 (INTA, INTB)
C++ (Arduino IDE)
1// MCP23017: 2 I2C pininden 16 G/Ç (kütüphanesiz) / 16 I/O from 2 I2C pins (no library)
2#include <Wire.h>
3
4const uint8_t MCP_ADDR = 0x20;   // A0, A1, A2 GND'de / A0, A1, A2 to GND
5// Kayıt adresleri (IOCON.BANK = 0) / register addresses
6const uint8_t IODIRA = 0x00;     // A portu yön / port A direction
7const uint8_t IODIRB = 0x01;     // B portu yön / port B direction
8const uint8_t GPPUB  = 0x0D;     // B portu pull-up / port B pull-ups
9const uint8_t GPIOB  = 0x13;     // B portu okuma / read port B
10const uint8_t OLATA  = 0x14;     // A portu çıkış / port A output latch
11
12void writeReg(uint8_t reg, uint8_t value) {
13  Wire.beginTransmission(MCP_ADDR);
14  Wire.write(reg);
15  Wire.write(value);
16  Wire.endTransmission();
17}
18
19uint8_t readReg(uint8_t reg) {
20  Wire.beginTransmission(MCP_ADDR);
21  Wire.write(reg);
22  Wire.endTransmission();
23  Wire.requestFrom(MCP_ADDR, (uint8_t)1);
24  return Wire.available() ? Wire.read() : 0xFF;
25}
26
27void setup() {
28  Serial.begin(9600);
29  Wire.begin();                   // Uno: SDA A4, SCL A5
30  Wire.setClock(400000);          // 400 kHz hızlı mod / fast mode
31  Wire.beginTransmission(MCP_ADDR);
32  if (Wire.endTransmission() != 0) {
33    Serial.println(F("MCP23017 bulunamadi / not found"));
34  }
35  writeReg(IODIRA, 0x00);         // GPA0-7 çıkış (LED) / outputs (LEDs)
36  writeReg(IODIRB, 0x7F);         // GPB0-6 giriş, GPB7 çıkış / inputs, GPB7 output
37  writeReg(GPPUB, 0x7F);          // girişlerde 100 kΩ pull-up / 100 kΩ pull-ups on inputs
38}
39
40void loop() {
41  uint8_t pressed = ~readReg(GPIOB) & 0x7F;   // LOW = basılı, terslenir / LOW = pressed, inverted
42  writeReg(OLATA, pressed);                    // her buton kendi LED'ini yakar / each button lights its LED
43  delay(10);
44}
45

This code writes straight to the MCP23017's registers without any library, so you can see how the chip works. In the IODIR register 1 means input and 0 means output; GPPU enables the internal pull-ups, GPIO reads and OLAT writes. For wiring, connect the chip's VDD (pin 9) to 5 V, VSS (pin 10) to GND, SCL (pin 12) to A5, SDA (pin 13) to A4 and A0–A2 (pins 15–17) to GND. According to Microchip's current datasheet, GPA7 and GPB7 should only be used as outputs; that's why the code makes seven pins of port B inputs and the last one an output. If you want simpler code, the Adafruit MCP23017 library offers functions similar to pinMode() and digitalWrite().

The MCP23017's RESET pin (pin 18) is active LOW and has no internal pull-up. Leave it floating and the chip resets randomly, sometimes showing up in an I2C scan and sometimes vanishing. Tie RESET to 5 V directly or through 10 kΩ. The I2C bus also needs 4.7 kΩ pull-up resistors on SDA and SCL; many modules have them, on a bare chip you add them yourself.

Method 5: 16 analog inputs with the CD74HC4067

Shift registers and I2C expanders only work digitally; if you need to read lots of potentiometers, LDRs or analog sensors, the answer is a multiplexer. The CD74HC4067 is like a 16-channel electronic rotary switch: the binary number (0–15) on its 4 select pins S0–S3 decides which channel is connected to the SIG pin. Connect SIG to A0 and you read 16 analog inputs with 5 pins; with two chips, 32. After switching channels, wait a few microseconds before analogRead and consider discarding the first reading, because the ADC's sampling capacitor still holds the previous channel's voltage. The switch is bidirectional, so it can also be used as a digital output, but only one channel is connected at a time. For code and wiring see the CD74HC4067 guide.

Other pin-saving techniques

  • Matrix scanning: a 4x4 keypad reads 16 keys with 8 pins because the keys sit at the crossings of rows and columns. Details in the keypad guide.
  • I2C module instead of parallel: a 16x2 LCD needs 6 pins directly; with a PCF8574 I2C backpack on the back it only uses A4 and A5. For wiring see the I2C LCD guide.
  • Smart LEDs: on a WS2812B strip hundreds of RGB LEDs connect to a single data pin, and each LED's color is set individually.
  • Charlieplexing: taking advantage of LEDs conducting in one direction only, you drive n x (n - 1) LEDs with n pins; 12 LEDs with 4 pins. Only one LED is lit at a time, but fast scanning makes them all appear on.
  • Shared SPI bus: all SPI devices share MOSI, MISO and SCK; each new device needs only one CS pin.
  • Resistor button ladder: connect 5–10 buttons to one analog pin through different resistors and work out which was pressed from the reading.

Methods compared: pin count and speed

MethodPins usedI/O gainedDirectionApprox. speed (Uno)
Analog pins as digital0+6Input / outputNormal pin speed (digitalWrite about 4 µs)
74HC595 (chain)38 per chipOutput0.1 ms per byte with shiftOut, a few µs with SPI
74HC165 (chain)38 per chipInputAbout 0.1 ms per byte
PCF85742 (I2C, shared)8 x 8 = 64Input / outputAbout 0.3 ms per write at 100 kHz
MCP230172 (I2C, shared)16 x 8 = 128Input / output, interruptsAbout 0.1 ms per register write at 400 kHz
CD74HC40675 (4 select + SIG)16 channelsAnalog / digital, one channelAbout 0.11 ms per channel analogRead
Matrix keypad816 keysInputA scan takes a few ms
WS2812B1Hundreds of RGB LEDsOutputAbout 30 µs per LED

When should you switch to an Arduino Mega or an ESP32?

Expander chips aren't a cure-all: each brings extra wiring, extra code and software latency, and jobs that need PWM, interrupts or high-speed counting need a real microcontroller pin. You can use this rough rule: if you're 5–10 pins short and they drive slow things like LEDs, relays or buttons, use an expander. If you're more than 20 pins short, need lots of PWM or hardware serial ports, or are also running out of RAM, switching boards is the cleaner solution. If you also want Wi-Fi, Bluetooth or more processing power, the ESP32 is the right place; for a comparison see the Arduino or ESP32 article.

Arduino Mega 2560: more I/O when pins run out
Arduino Mega 2560: with 54 digital pins, 16 analog pins and 4 hardware serial ports it runs Uno code almost unchanged.
BoardDigital I/OAnalog inPWMLogicWhen to choose
Arduino Uno14 (+6 analog = 20)665 VSmall and medium projects
Arduino Nano14 (+6 analog = 20)8 (A6, A7 analog only)65 VUno power on a breadboard
Arduino Mega 256054 (+16 analog = 70)16155 VLots of buttons and relays, 3D printers, CNC
ESP32 DevKitAbout 25 free GPIO18 channels (ADC1 with Wi-Fi)16 channels, any pin3.3 VWi-Fi, Bluetooth, speed and memory
Raspberry Pi Pico26 GPIO316 channels3.3 VCheap, fast, custom protocols with PIO

Watch out for two things when moving to the ESP32. First, even though 34 GPIOs appear on paper, GPIO 6–11 are reserved for the flash memory, 34–39 are input-only and 0, 2, 12 and 15 are boot pins; the number that's really free is around 25. Second, because the ESP32 runs on 3.3 V logic, 5 V modules may need a level shifter. The Mega, on the other hand, lives in the same 5 V world as the Uno and most libraries work unchanged; only the I2C pins are 20/21 instead of A4/A5 and the SPI pins are 50–53. For the Mega's pin details see the Arduino Mega pinout guide.

Common mistakes and fixes

ProblemCauseFix
Upload fails while something is on D0/D1The serial line is shared with USBDisconnect the part while uploading or move it to another pin
74HC595 outputs change randomlyMR floating or no 100 nF capacitorTie MR to 5 V and put 100 nF between VCC and GND
All LEDs off or very dimOE pin floating or HIGHTie OE to GND
Ghost button presses on the 74HC165Floating inputs10 kΩ pull-up on every input, CLK INH to GND
I2C expander not foundWrong address or no pull-upsRun an I2C scanner, add 4.7 kΩ on SDA/SCL
LED on a PCF8574 output is very dimThe pin can't source currentConnect the LED between the pin and 5 V and drive the pin LOW
Mux readings bleed into each otherThe ADC hasn't settled after switchingWait briefly after switching and discard the first reading
Before starting a project, write a small pin budget on paper: list how many pins each part needs and which need PWM, interrupts or analog. That way you group the parts that will share the I2C and SPI lines from the start and don't have to rebuild the circuit halfway because you ran out of pins. If you get stuck on logic levels and gate operations, the logic gates tool can help too.

There's no need to panic when you run out of pins: first bring the analog pins into play, then pick a 74HC595, 74HC165, MCP23017 or CD74HC4067 depending on the job, and only move to a Mega or an ESP32 if you really need a big jump. If you want to try pin-hungry projects like an I2C LCD, a keypad, a 4-digit 7-segment display and LED matrices step by step with a circuit diagram and code, have a look at the related projects in the BlueGrays app; in each one you'll see for yourself how the pins are shared out.

Frequently asked questions

Can Arduino analog pins be used as digital pins?

Yes. On the Uno, A0–A5 are full digital pins and can also be referred to as 14–19; pinMode, digitalWrite, digitalRead and INPUT_PULLUP work normally. The only exception is A6 and A7 on the Nano and Pro Mini, which are analog inputs only.

How many 74HC595 chips can I connect to an Arduino?

There's no theoretical limit; by connecting each Q7' output to the next chip's DS input you can chain as many as you like and still use 3 pins. In practice, a long chain takes longer to update and the clock line weakens; beyond 10–20 chips you'll need to buffer the clock and latch lines and put 100 nF on every chip.

Should I use a PCF8574 or an MCP23017?

For just a few LEDs or buttons, the cheap and common PCF8574 is enough. If you want per-pin direction, internal pull-ups, 400 kHz speed, 25 mA from every pin or two separate interrupt outputs, the MCP23017 is the better choice. Because it gives 16 pins in a single chip, the wiring is tidier too.

How do I get more analog inputs on an Arduino?

The most practical way is a 16-channel analog multiplexer like the CD74HC4067: with 4 select pins and 1 analog pin you read 16 analog sensors. For higher resolution, external I2C ADCs like the ADS1115 offer 4 channels at 16 bits, and four of them fit on the same bus. Switching to a Nano also gains you two analog inputs with A6 and A7.

Can I use pins D0 and D1 on an Arduino?

You can, but they're the serial line over USB. With a part connected there, uploading may fail and Serial.print output will affect the part you connected. If you don't need the Serial Monitor and can disconnect the part while uploading, they can be used as a last resort.

How many pins does the Arduino Mega have?

The Arduino Mega 2560 has 54 digital I/O pins (15 with PWM) and 16 analog inputs; since the analog ones can also be used as digital, that's 70 pins in total. It also has 4 hardware serial ports. Because it runs on 5 V logic, most Uno projects move to the Mega with small pin changes.

See all projects
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)
71
Password Lock with 4x4 Keypad and LCD
AR-64Advanced
Premium

Password Lock with 4x4 Keypad and LCD

A small electronic door lock that keeps out anyone who doesn't know the code. You press the hash key and type the code on the keypad; if it is right, the screen says access granted, the green LED…

4.8(59)
75
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
Displaying 0-9999 on a 4-Digit 7-Segment Display
AR-47Intermediate
Premium

Displaying 0-9999 on a 4-Digit 7-Segment Display

On a four-digit number display, numbers race from 0 to 9999 and then start again from zero.

4.4(19)
25
Using a WS2812B LED Strip
AR-69Intermediate
Premium

Using a WS2812B LED Strip

Meet a smart LED strip where you can colour every single light on its own. The Arduino first lights the first five LEDs red, a second later turns the other five blue, then switches them all off and…

4.8(54)
70
Dot Matrix Clock and Calendar (MAX7219, DS1307)
AR-84Intermediate
Premium

Dot Matrix Clock and Calendar (MAX7219, DS1307)

A glowing board for your desk or wall that shows the time and date in scrolling text.

5.0(37)
59
4x 32x8 Matrix LED Animation and Text
AR-90Intermediate
Premium

4x 32x8 Matrix LED Animation and Text

Stack four long LED matrix modules on top of each other and you get a square screen with more than a thousand dots of light.

4.6(40)
66