mardi 9 mai 2017

Extender GPIO mcp23017 avec Nodemcu (ESP8266)

Le  mcp2317 permet d'ajouter des GPIO via I2C à L'esp 8266.

Brochage :




Librairie : 


Cabalage:

Voici un petit exemple pour ajouter un bouton poussoir sur un des GPIO du mcp2317


Code :


#include <ESP8266WiFi.h>

#include <Wire.h>
// https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library
#include "Adafruit_MCP23017.h"

// Basic pin reading and pullup test for the MCP23017 I/O expander
// public domain!

// Connect pin #12 of the expander to Analog 5 (i2c clock)
// Connect pin #13 of the expander to Analog 4 (i2c data)
// Connect pins #15, 16 and 17 of the expander to ground (address selection)
// Connect pin #9 of the expander to 5V (power)
// Connect pin #10 of the expander to ground (common ground)
// Connect pin #18 through a ~10kohm resistor to 5V (reset pin, active low)

// Input #1 is on pin 22 so connect a button or switch from there to ground
// constants won't change. They're used here to
// set pin numbers:

Adafruit_MCP23017 mcp;
const int buttonPin = 1;     // the number of the pushbutton pin
const int ledPin =  LED_BUILTIN;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  Serial.begin(115200);

  mcp.begin();      // use default address 0
  mcp.pinMode(buttonPin, INPUT);
  mcp.pullUp(buttonPin, HIGH);  // turn on a 100K pullup internally

  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
}

void loop() {

  // read the state of the pushbutton value:
  buttonState = mcp.digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is LOW:
  if (buttonState == LOW) {
    // turn LED on:
    digitalWrite(ledPin, LOW);
    Serial.println("ON");  
  } else {
    // turn LED off:
    digitalWrite(ledPin, HIGH);
    Serial.println("OFF");
  }
}