Mcp23017

Iš Žinynas.
Jump to navigation Jump to search

Mcp23017-pinout.jpg.webp

Mcp23017-and-8-leds schem.png-2.webp

This port expander provides you 16 more GPIOs and cost you around 1€. And because it communicates via I²C it only uses two GPIOs of your ESP8266 and you can address up to 8 MCP23017. That makes up to 126 additional GPIOs.

ESP32 Wiring[keisti]

D8fddce06241296363242bc44a0fffc8804a5295 2 641x500.jpg Esquema.png

  • Board 3.3V output to MCP23017 Vdd
  • Board ground/GND to MCP23017 Vss
  • Board SCL (pin 22) to MCP23017 SCL
  • Board SDA (pin 21) to MCP23017 SDA
  • MCP23017 SCL to 4.7 KΩ resistor connected to 3.3V
  • MCP23017 SDA to 4.7 KΩ resistor connected to 3.3V
  • MCP23017 A0 to ground
  • MCP23017 A1 to ground
  • MCP23017 A2 to ground
  • MCP23017 reset to 1K resistor connected to 3.3V

I2C Scanner[keisti]

#include <Wire.h>
 
void setup() {
  Wire.begin();
  Serial.begin(115200);
  Serial.println("\nI2C Scanner");
}
 
void loop() {
  byte error, address;
  int nDevices;
  Serial.println("Scanning...");
  nDevices = 0;
  for(address = 1; address < 127; address++ ) {
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
    if (error == 0) {
      Serial.print("I2C device found at address 0x");
      if (address<16) {
        Serial.print("0");
      }
      Serial.println(address,HEX);
      nDevices++;
    }
    else if (error==4) {
      Serial.print("Unknow error at address 0x");
      if (address<16) {
        Serial.print("0");
      }
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0) {
    Serial.println("No I2C devices found\n");
  }
  else {
    Serial.println("done\n");
  }
  delay(5000);          
}

Example code[keisti]

// Install library first
#include <Adafruit_MCP23X17.h>

#define LED_PIN 0     // MCP23XXX pin LED is attached to

Adafruit_MCP23X17 mcp;

void setup() {
  Serial.begin(115200);
  //while (!Serial);
  Serial.println("MCP23xxx Blink Test!");

  // uncomment appropriate mcp.begin
  if (!mcp.begin_I2C()) {
  //if (!mcp.begin_SPI(CS_PIN)) {
    Serial.println("Error.");
    while (1);
  }

  // configure pin for output
 mcp.pinMode(LED_PIN, OUTPUT);
 Serial.println("Looping...");
}

void loop() {
  Serial.println("Turn on");
  mcp.digitalWrite(LED_PIN, HIGH);
  delay(1000);
  Serial.println("Turn off");
  mcp.digitalWrite(LED_PIN, LOW);
  delay(1000);
  Serial.println("loop again");
}

ESP8266[keisti]

https://medium.com/@wilko.vehreke/more-gpios-for-the-esp8266-with-the-mcp23017-b89f5e15cde3