After googling much I have found that MCP23017 can be used with esp8266-12e for more number of output pins. I have tried all the programs from different tutorials on internet but could not make it work.

Basically my question is , i want to control all the 16 leds connected to GPA0-7 AND GPB0-7 individually , hence i need a program snippet for make it work. I am using AP mode for esp8266-12e and sending get methods with params to identify which led to turn on/off.

Even i have written the basic program , can anyone help me completing the entire program. thanks for the help in advanced.

Pins i have connected are

ESP8266-12E    |   MCP23017
GPIO-14 (scl)  |  PIN-12 (SCL)
GPIO-2 (sda)   |  PIN 13 (SDA)
GND            |  A0,A1,A2

My code:

#include <Wire.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

ESP8266WebServer server(80);

void handleRoot() {
    server.send(200, "text/html", "<h1>You are connected</h1>");

void setup() {
    Serial.print("Configuring access point...");
    WiFi.softAP(ssid, password);

    IPAddress myIP = WiFi.softAPIP();
    Serial.print("AP IP address: ");
    server.on("/", handleRoot);

    server.on("/led1", [](){});
    server.on("/led2", [](){});
    server.on("/led3", [](){});
    server.on("/led4", [](){});
    // up to 16 leds i have written same method.


void loop() {

You have an MCP23017 which connects to the device using the I2C protocol, which means you need the library. You also need to ensure that SCL and SDA are 'pulled up' using 4.7Kohm resistors between the data lines and the +ve line. You will then need to send commands to the device to initialise it and set the pins to OUTPUT. (See http://ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf).

When running you will need to update the state of this pins by sending messages such as 0x09 0xFF to turn them all on.

It is going to be worth looking for a library that already does all this, but writing your own shouldn't be too hard. You will be able to use an Arduino library on an ESP8266.

These chips are the easiest ones to use and learn about I2C, so its worth looking at the manual and trying to write your own "library" (Just start by writing a C++ class within your sketch folder.)

  • A very useful link and the description you have provided Matt. I need to read and understand whats written in the docs and implement it. it will take time for me to do it. But thanks for the guidance. I think i will need more ahead. – KarmaCoding Feb 6 '17 at 18:21
  • hi , i tried to connect 4.7k and 22k resistors for sda , scl sda --22k-- Vcc(+5v) And scl --22k-- vcc(+5v) but still not getting anything. i have been trying to program this since a lot of days , but not be able to do it. can u please help me more in this. i am using this code in arduino github.com/pedrominatel/esp8266-arduino-examples/blob/master/… – KarmaCoding Feb 17 '17 at 20:36
  • the changes in this example i have done are line 152 : MCP23017_init(u8_MCP23017_address, 4,5); i tried this too becuase in some datasheets it has 4=sda and 5=scl – KarmaCoding Feb 17 '17 at 21:21
  • any guidance please. – KarmaCoding Feb 20 '17 at 7:16
  • Step 1 Try using I2CScanner playground.arduino.cc/Main/I2cScanner rather than your own code to make sure that the hardware is functioning. ESP pins number in the code do not match the pin number on the boards usually. Find an image (on Google) of your ESP board with the pin numbers listed and use the GPIO_n numbers. – Code Gorilla Feb 20 '17 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.