Mcp23017: Skirtumas tarp puslapio versijų

Iš Žinynas.
Jump to navigation Jump to search
7 eilutė: 7 eilutė:
 
= ESP32 Wiring =
 
= ESP32 Wiring =
  
* SDA -> pin 21
+
* Board 3.3V output to MCP23017 Vdd
* SCL -> pin 22
+
* Board ground/GND to MCP23017 Vss
* VSS -> GND
+
* Board SCL (pin 22) to MCP23017 SCL
* VDD -> VIN
+
* Board SDA (pin 21) to MCP23017 SDA
* 17 -> GND
+
* MCP23017 SCL to 4.7 KΩ resistor connected to 3.3V
* 16 -> GND
+
* MCP23017 SDA to 4.7 KΩ resistor connected to 3.3V
* 15 -> GND
+
* MCP23017 A0 to ground
 +
* MCP23017 A1 to ground
 +
* MCP23017 A2 to ground
 +
* MCP23017 reset to 1K resistor connected to 3.3V
 +
 
 +
== I2C Scanner ==
  
 
<Syntaxhighlight lang="cpp">
 
<Syntaxhighlight lang="cpp">
// ESP32 I2C Scanner
 
// Based on code of Nick Gammon http://www.gammon.com.au/forum/?id=10896
 
// ESP32 DevKit - Arduino IDE 1.8.5
 
// Device tested PCF8574 - Use pullup resistors 3K3 ohms !
 
// PCF8574 Default Freq 100 KHz
 
 
#include <Wire.h>
 
#include <Wire.h>
void setup()
+
{
+
void setup() {
Serial.begin (115200);  
+
  Wire.begin();
Wire.begin (21, 22); // sda= GPIO_21 /scl= GPIO_22
+
  Serial.begin(115200);
 +
  Serial.println("\nI2C Scanner");
 
}
 
}
void Scanner ()
+
{
+
void loop() {
Serial.println ();
+
  byte error, address;
Serial.println ("I2C scanner. Scanning ...");
+
  int nDevices;
byte count = 0;
+
  Serial.println("Scanning...");
Wire.begin();
+
  nDevices = 0;
for (byte i = 8; i < 120; i++)
+
  for(address = 1; address < 127; address++ ) {
{
+
    Wire.beginTransmission(address);
Wire.beginTransmission (i); // Begin I2C transmission Address (i)
+
    error = Wire.endTransmission();
if (Wire.endTransmission () == 0) // Receive 0 = success (ACK response)  
+
    if (error == 0) {
{
+
      Serial.print("I2C device found at address 0x");
Serial.print ("Found address: ");
+
      if (address<16) {
Serial.print (i, DEC);
+
        Serial.print("0");
Serial.print (" (0x");
+
      }
Serial.print (i, HEX); // PCF8574 7 bit address
+
      Serial.println(address,HEX);
Serial.println (")");
+
      nDevices++;
count++;
+
    }
 +
    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);        
 
}
 
}
 +
</syntaxhighlight>
 +
 +
== Example code ==
 +
 +
<syntaxhighlight lang="cpp">
 +
// 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...");
 
}
 
}
Serial.print ("Found ");  
+
 
Serial.print (count, DEC); // numbers of devices
+
void loop() {
Serial.println (" device(s).");
+
  Serial.println("Turn on");
}
+
  mcp.digitalWrite(LED_PIN, HIGH);
void loop()
+
  delay(1000);
{
+
  Serial.println("Turn off");
Scanner ();
+
  mcp.digitalWrite(LED_PIN, LOW);
delay (100);
+
  delay(1000);
 +
  Serial.println("loop again");
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

11:54, 26 lapkričio 2022 versija

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

  • 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

#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

// 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

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