13+6 pins often a limitation of Arduino in projects where we need many pins. Here is how to increase the number of digital pins in Arduino. You know that 13 pins are digital. 6 analog pins also can be used as digital in this way :
1 2 3 4 5 6 | Pin 14 = Analog in 0 Pin 15 = Analog in 1 Pin 16 = Analog in 2 Pin 17 = Analog in 3 Pin 18 = Analog in 4 Pin 19 = Analog in 5 |
So actually we can refer Analog Pin 5 in this way :
1 | digitalWrite(19,HIGH); |
Technically we can use the TX/RX. But TX/RX on board already use Pin 0 and Pin 1. If you load a blank sketch, you will measure 5 V on the Pins 0 and 1. Also it is practical to avoid Pin 0 and Pin 1 in some cases where Serial.begin()
like function is used in the sketch. So the total number of Pins going round 17. But with 17 Pins how we can really can control many LEDs as simplest example? How to increase the number of digital pins in Arduino? We can use an IC like device which will “magnify” one Pin. A common way to expand the set of available output Pins on the Arduino is to use shift registers like the 74HC595 IC. 74HC595 IC needs 3 pins to control these chips – Clock, Latch, Data and increase the number the Pins. Display drivers like MAX7219 IC actually expand the pins in TM 1637 like 7 Segment LED display modules. But MAX7219 IC is costly. MCP23017 is a commonly used I/O port expander chip for Arduino like microcontroller boards. Here is data sheet of MCP23017 :
---
1 | http://ww1.microchip.com/downloads/en/DeviceDoc/20001952C.pdf |
How to Increase the Number of Digital Pins in Arduino With MCP23017 Port Extender
MCP23017 chip will use 2 Pins on Arduino and give 16 I/O ports or Digital Pins! So technically you can use 8 MCP23017 to extend one Arduino’s 16 Pins to 16 x 8 = 128 Pins. MCP23017 is an I/O Port Expander, uses the I²C bus and protocol. Arduino has library for I²C bus and protocol named Wire.h
. Things are actually near ready, you simply need this kind of circuit :
You can use other additional libraries like Adafruit has to make the “coding” easy :
1 | https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library |
For this schematic :
You can test with this code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include "Wire.h" void setup() { Wire.begin(); // wake up I2C bus // set I/O pins to outputs Wire.beginTransmission(0x20); Wire.write(0x00); // IODIRA register Wire.write(0x00); // set all of port A to outputs Wire.endTransmission(); } void loop() { Wire.beginTransmission(0x20); Wire.write(0x12); // address bank A Wire.write((byte)0xAA); // value to send - all HIGH Wire.endTransmission(); delay(500); Wire.beginTransmission(0x20); Wire.write(0x12); // address bank A Wire.write((byte)0x55); // value to send - all HIGH Wire.endTransmission(); delay(500); } |