Arduino MCP23017 16 bit IO Expander. 8 chips,128 added pins!

Here are 8 mcp23017 chips daisy-chained using an Arduino pro mini. There are now 128 added addressable pins using the I2c SDA and SCL pins. I’m not really sure what uses this project could have, but it was fun to make! One possible evolution of it could be as a midi controller…maybe a lighted fretboard on a guitar that shows you the notes to play…or something.

In the code there are two functions that will generate patterns for the LEDs: “Propeller” and “Clockwise”. You can watch the video to see the output of each routine.

The delay time between LED firings is dependent on the ultrasonic distance sensor: (LV-MaxSonar-EZ0 by MaxBotix). That is, the closer the object to the front of the sensor, the smaller the delay interval between pin firings.

/*
Ty Miller - 2014
Based on
http://tronixstuff.wordpress.com/2011/08/26/tutorial-maximising-your-arduinos-io-ports/
*/

#include "Wire.h"
const int sonarPin = A0;
const int ledPin = 13;

//const int buttonPin0 = 2;     
//const int buttonPin1 = 4;     

// variables will change:
//int buttonState0 = 0;         // variable for pushbutton status
//int buttonState1 = 0;         


void setup()
{
    // initialize the pushbutton pin as an input:
 // pinMode(buttonPin0, INPUT);    
  //pinMode(buttonPin1, INPUT); 
  
 pinMode(ledPin, OUTPUT);  
 Serial.begin(19200);



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();
Wire.beginTransmission(0x20);
Wire.write(0x01); // IODIRB register
Wire.write(0x00); // set all of port B to outputs
Wire.endTransmission();
 Wire.beginTransmission(0x21);
 Wire.write(0x00); // IODIRA register
 Wire.write(0x00); // set all of bank A to outputs
 Wire.endTransmission();
Wire.beginTransmission(0x21);
 Wire.write(0x01); // IODIRB register
 Wire.write(0x00); // set all of bank B to outputs
 Wire.endTransmission();
 Wire.beginTransmission(0x22);
 Wire.write(0x00); // IODIRA register
 Wire.write(0x00); // set all of bank A to outputs
 Wire.endTransmission();
Wire.beginTransmission(0x22);
 Wire.write(0x01); // IODIRB register
 Wire.write(0x00); // set all of bank B to outputs
 Wire.endTransmission();
  Wire.beginTransmission(0x23);
 Wire.write(0x00); // IODIRA register
 Wire.write(0x00); // set all of bank A to outputs
 Wire.endTransmission();
Wire.beginTransmission(0x23);
 Wire.write(0x01); // IODIRB register
 Wire.write(0x00); // set all of bank B to outputs
 Wire.endTransmission();
  Wire.beginTransmission(0x24);
 Wire.write(0x00); // IODIRA register
 Wire.write(0x00); // set all of bank A to outputs
 Wire.endTransmission();
Wire.beginTransmission(0x24);
 Wire.write(0x01); // IODIRB register
 Wire.write(0x00); // set all of bank B to outputs
 Wire.endTransmission();
  Wire.beginTransmission(0x25);
 Wire.write(0x00); // IODIRA register
 Wire.write(0x00); // set all of bank A to outputs
 Wire.endTransmission();
Wire.beginTransmission(0x25);
 Wire.write(0x01); // IODIRB register
 Wire.write(0x00); // set all of bank B to outputs
 Wire.endTransmission();
  Wire.beginTransmission(0x26);
 Wire.write(0x00); // IODIRA register
 Wire.write(0x00); // set all of bank A to outputs
 Wire.endTransmission();
Wire.beginTransmission(0x26);
 Wire.write(0x01); // IODIRB register
 Wire.write(0x00); // set all of bank B to outputs
 Wire.endTransmission();
  Wire.beginTransmission(0x27);
 Wire.write(0x00); // IODIRA register
 Wire.write(0x00); // set all of bank A to outputs
 Wire.endTransmission();
Wire.beginTransmission(0x27);
 Wire.write(0x01); // IODIRB register
 Wire.write(0x00); // set all of bank B to outputs
 Wire.endTransmission();

}




void blinq(int chip,int bank,int pin){
int distance = analogRead(sonarPin)/(2*0.3937); 
int delayLED = abs(map(distance,15,160,1,100));
//Serial.println(delayLED);  
// LED on
Wire.beginTransmission(chip); //chip select
Wire.write(bank); //bank select
Wire.write(pin); // pin select
delay(delayLED);
Wire.endTransmission();    
// LED off
Wire.beginTransmission(chip); 
Wire.write(bank); 
Wire.write(0); 
delay(delayLED);
Wire.endTransmission();   
}




void allOff() {
 Wire.beginTransmission(0x20);
 Wire.write(18); // IODIRA register
 Wire.write(0); // set all of bank A to outputs
 Wire.write(19); // IODIRB register
 Wire.write(0); // set all of bank B to outputs
 Wire.endTransmission();
 Wire.beginTransmission(0x21);
 Wire.write(18); // IODIRA register
 Wire.write(0); // set all of bank A to outputs
 Wire.write(19); // IODIRB register
 Wire.write(0); // set all of bank B to outputs
 Wire.endTransmission();
 Wire.beginTransmission(0x22);
 Wire.write(18); // IODIRA register
 Wire.write(0); // set all of bank A to outputs
 Wire.write(19); // IODIRB register
 Wire.write(0); // set all of bank B to outputs
 Wire.endTransmission();
  Wire.beginTransmission(0x23);
 Wire.write(18); // IODIRA register
 Wire.write(0); // set all of bank A to outputs
 Wire.write(19); // IODIRB register
 Wire.write(0); // set all of bank B to outputs
 Wire.endTransmission();
  Wire.beginTransmission(0x24);
 Wire.write(18); // IODIRA register
 Wire.write(0); // set all of bank A to outputs
 Wire.write(19); // IODIRB register
 Wire.write(0); // set all of bank B to outputs
 Wire.endTransmission();
  Wire.beginTransmission(0x25);
 Wire.write(0x00); // IODIRA register
 Wire.write(0); // set all of bank A to outputs
 Wire.write(0x01); // IODIRB register
 Wire.write(0); // set all of bank B to outputs
 Wire.endTransmission();
  Wire.beginTransmission(0x26);
 Wire.write(18); // IODIRA register
 Wire.write(0); // set all of bank A to outputs
 Wire.write(19); // IODIRB register
 Wire.write(0); // set all of bank B to outputs
 Wire.endTransmission();
  Wire.beginTransmission(0x27);
 Wire.write(18); // IODIRA register
 Wire.write(0); // set all of bank A to outputs
 Wire.write(19); // IODIRB register
 Wire.write(0); // set all of bank B to outputs
 Wire.endTransmission();  
}



void propeller() {

int x=32;   // chip 
int y=18;    //bank
int z=128;  //pin

for (int i=0;i< =7;i++){
blinq(x,y,z);
blinq(x+1,y,z);
blinq(x+2,y,z);
blinq(x+3,y,z);
blinq(x+4,y,z);
blinq(x+5,y,z);
blinq(x+6,y,z);
blinq(x+7,y,z);
blinq(x+7,y+1,z);
blinq(x+6,y+1,z);
blinq(x+5,y+1,z);
blinq(x+4,y+1,z);
blinq(x+3,y+1,z);
blinq(x+2,y+1,z);
blinq(x+1,y+1,z);
blinq(x,y+1,z);
z=z/2;
}  
} 


void Clockwise() {
//while (buttonState1 == HIGH) 	{

int z=128;  //reset
//-----------------------------------

z=128;  //reset
for (int i=0;i<=7;i++){
blinq(37,19,z);
z=z/2;
}  

z=128;  //reset
for (int i=0;i<=7;i++){
blinq(38,19,z);
z=z/2;
}  

z=128;  //reset
for (int i=0;i<=7;i++){
blinq(39,19,z);
z=z/2;
} 

z=128;  //reset
for (int i=0;i<=7;i++){
blinq(39,18,z);
z=z/2;
}  

z=128;  //reset
for (int i=0;i<=7;i++){
blinq(38,18,z);
z=z/2;
}  

z=128;  //reset
for (int i=0;i<=7;i++){
blinq(37,18,z);
z=z/2;
}  

z=128;  //reset
for (int i=0;i<=7;i++){
blinq(36,18,z);
z=z/2;
}  

z=128;  //reset
for (int i=0;i<=7;i++){
blinq(36,19,z);
z=z/2;
}  


z=128;  //reset
for (int i=0;i<=7;i++){
blinq(35,18,z);
z=z/2;
}  

z=128;  //reset
for (int i=0;i<=7;i++){
blinq(35,19,z);
z=z/2;
}  

z=128;  //reset
for (int i=0;i<=7;i++){
blinq(34,19,z);
z=z/2;
}  

z=128;  //reset
for (int i=0;i<=7;i++){
blinq(33,19,z);
z=z/2;
}  

z=128;  //reset
for (int i=0;i<=7;i++){
blinq(32,19,z);
z=z/2;
}  

z=128;  //reset
for (int i=0;i<=7;i++){
blinq(34,18,z);
z=z/2;
} 

z=128;  //reset
for (int i=0;i<=7;i++){
blinq(33,18,z);
z=z/2;
}  

z=128;  //reset
for (int i=0;i<=7;i++){
blinq(32,18,z);
z=z/2;
}  
} 
//} 


void loop() {
  
    // read the state of the pushbutton value:
// buttonState0 = digitalRead(buttonPin0);
 // buttonState1 = digitalRead(buttonPin1);
//if (buttonState0 = HIGH) {
//allOff();
//}

 propeller();
  //Clockwise();
  // LED on

} 

You may also like...

8 Responses

  1. CUVILLIER says:

    Nice !! You really are a “tech guy”…I also like the rainbow color of the wires….

  2. Mavi says:

    BRAVO!!!!!!
    “Blinquing here” on a standing ovation, like in soccer stadiums 😀
    Put it on a vacuum cleaner robot and let Nap & Raz have the hunt of their lives!!!!!

  3. Melanie says:

    This is cool! I am trying to get into the technology field, my interest is in front end web development. I am taking a guess that the code is Java, but I could be wrong. Anyway all this is really cool! Have a nice day! 🙂

  4. Joan Lawler says:

    Is Ty Miller have a relative that was on Wagon Train on t.v.

Leave a Reply

Your email address will not be published.