How to Use ESP8266 With PCF8574 - 4 Input and 4 Output

18,150

52

13

About: Software, electronics, do-it-yourself

How to increase the number of I/O pins on the ESP8266 using the PCF8574. This example will use four buttons (as inputs) and four relays (as outputs) and only one PCF8574 chip. The four pins of the PCF8574 chip will be set as inputs and four pins as outputs. In this example I will use NodeMCU but it works well on ESP8266-01. In this tutorial I will also show you, how to use jQuery AJAX with ESP8266. The software for the ESP8266 was written in the Arduino IDE. NodeMCU, ESP8266 is a very good solution for IOT (Internet of things).

This article can also see here:


How to use ESP8266 with PCF8574 - 4 input and 4 output

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Components

Step 2: Schematic

Step 3: Software

To use PCF8574 on ESP8266 I use this library pcf8574_esp.
This number "0b00111000" is the address of the PCF8574 expander.
The last 3bit (0b00111000) can be set using address pins A0, A1, A2. Thanks to which
we can connect more expanders to the same I2C bus. In this tutorial, I use an interrupt to
notify the ESP8266 microcontroller that it has made a change in the input state.
PIN_INT defines interrupt pin D5.
	PCF857x pcf8574(0b00111000, &Wire);
	#define PIN_INT D5
	#define PIN_SDA D1
	#define PIN_SCL D2
Configure WIFI connection and HTTP server. Before you upload the software to ESP8266 setup
your WiFi connection. The HTTP server allows us to control relayers using a web browser.
	//WIFI i server HTTP
	ESP8266WebServer server(80);
	#define WIFI_SSID ""
	#define WIFI_PASSWORD ""
	#define HOST_NAME "onoff"
In this code fragment we setup the i2c bus and our PCF8574 I/O expander. Using this "pcf8574.begin (0xF0)"
we set 4 pins in high as inputs and 4 pins in low as outputs.
  Wire.pins(PIN_SDA, PIN_SCL);//SDA - D1, SCL - D2
  Wire.begin();
  
  pinMode(PIN_INT, INPUT_PULLUP);

  pcf8574.begin( 0xF0 ); //4 pin input, 4 pin output
  pcf8574.resetInterruptPin();
 
This part of the code is responsible for handling the PCF8574 interrupt. If PIN_INT is low,
our expander notifies us of a change in input. After reading the pin values from the expander,
PIN_INT will return to high.
 if( digitalRead(PIN_INT)==LOW ){
    delay(50);
    byte b = pcf8574.read8();
    Serial.println( "INT: " + String(b));

    byte keys = ((~b) >> 4) & 0x0F;
    
    if( CheckKey(keys, 0) ){
      Serial.println( "KEY 0");
      SW_toggle(0);
    }

    if( CheckKey(keys, 1) ){
      Serial.println( "KEY 1");
      SW_toggle(1);
    }

    if( CheckKey(keys, 2) ){
      Serial.println( "KEY 2");
      SW_toggle(2);
    }

    if( CheckKey(keys, 3) ){
      Serial.println( "KEY 3");
      SW_toggle(3);
    }
  }
Download source code: ESP8266_PCF8574.ino

Step 4: ESP8266 Firmware Upload With Arduino IDE

  1. Select your board Tools -> Board -> NodeMCU 1.0(ESP-12E Module)
  2. Press the upload button
  3. After uploading the software in Serial Monitor you can see the IP of your ESP8266.

Step 5: The End

Take IP from Serial Monitor and type in web browser. You will see the control panel of the ESP8266, thanks to which you can control the relays. You can also control the state of the relays using the buttons. If you turn on the relay with the physical button, the state of the relay will change automatically. This is possible thanks to the use of jquery ajax. The below piece of code that is responsible for it.

  //BEGIN: AJAX ------------------------------------------
  html += "<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js'></script>\r\n";
  html += "<script>\r\n";
  html += "function ajax_read(){\r\n";
  html += " $.ajax({\r\n";
  html += "    url: 'state', \r\n";
  html += "    success: function(result){\r\n";
  html += "      $('#res').html(result);\r\n";
  html += "    }\r\n";
  html += "  }\r\n";
  html += " );\r\n";
  html += "}\r\n";

  html += "function check() {\r\n";
  html += " ajax_read();\r\n";
  html += " setTimeout(function(){ check(); }, 1000);\r\n";
  html += "}\r\n";
  html += "check();\r\n";
  
  html += "</script>\r\n";
  //END: AJAX ------------------------------------------
wwwGoogle pluseTwitter
Microcontroller Contest 2017

Participated in the
Microcontroller Contest 2017

Be the First to Share

    Recommendations

    • CNC Contest

      CNC Contest
    • Make it Move

      Make it Move
    • Teacher Contest

      Teacher Contest

    13 Discussions

    0
    None
    RonL

    27 days ago

    Will this work with the PCF8575?

    0
    None
    Dachshund-Digital

    6 months ago

    This does not work for me. The code runs fine, but the relays never engage. I am using a separate 5v supply for the relay. I know the relay is 3.3.v signal level, because if I hookup the relay (any channel) to D8 on my ESP8266, i can open and close the given relay without issue, it is only through the 8574 module that things do not work. I can see the module on the i2c bus. I even setup the PCF8574 and relay modules on my Raspberry Pi and they works there just fine. So I am really confused by the PCF8574 module does not work with the ESP8266. Oh, and I know the i2c bus is working on the pins I setup I connected a BME280 to it and it works fine as well.

    0
    None
    ManuelC15

    8 months ago

    Excelent material. But I need conect in expansor 8 relay, with every need control in/out for on/off when, por example, temp is 26 celisuis degrees. How conect and how control this case?

    0
    None
    LarsB29

    12 months ago on Introduction

    Am I the only one that find the schematic a bit off! How on earth is the push button in the diagram going to pull "one" input down. Right now each push button is pulling all inputs down to 0. Have a nice day

    0
    None
    RBA2000

    1 year ago

    "....but it works well on ESP8266-01"

    Trying this on a ESP8266-01 but...With the current info you give this is not possible.

    0
    None
    diy_bloke

    2 years ago

    wel done. As your PC8547 link shows, the individual chip often is more expensive than the ready made module. The cheapest I saw the module I think was USD 1.15.
    However... it can be cheaper. One could also buy an I2C LCD module. Those modules are supposed to be soldered to the back of an LCD to make it I2C operational...and those are also based on an 8547 chip.
    Such modules cost 0.55 USD

    https://www.aliexpress.com/item/Free-Shipping-IIC-...

    2 replies
    0
    None
    adachsoftdiy_bloke

    Reply 2 years ago

    It is true that the same chip, and is much cheaper!

    0
    None
    diy_blokediy_bloke

    Reply 2 years ago

    even 49 cts ;-)

    https://www.aliexpress.com/item/IIC-I2C-Interface-LCD1602-2004-LCD-Adapter-Plate-for-Arduino-Free-Shipping-Dropshipping/1362338996.html?spm=2114.10010108.0.0.lYD4QN&scm=1007.12873.42117.0&pvid=32df182f-b1e6-4c92-8df0-4910084175f7&tpp=1

    0
    None
    JacSjoerd

    2 years ago

    i'm trying to understand why not using 2 ESP's and connect them. Isn't that easier? It differs about $1 in price.

    2 replies
    0
    None
    diy_blokeJacSjoerd

    Reply 2 years ago

    You could do that, but then it would be hard to use the 4 buttons and 4 relays from 1 program

    0
    None
    adachsoftdiy_bloke

    Reply 2 years ago

    Exactly, you can use two ESP but if you want one program to handle this then the method is easier.

    0
    None
    Boxoffrogs

    2 years ago

    Excellent, just what I needed and really well explained. Thank you!

    1 reply