Giter Site home page Giter Site logo

wollewald / mcp23017_we Goto Github PK

View Code? Open in Web Editor NEW
40.0 6.0 5.0 4.29 MB

Library for the MCP23017 and MCP23S17 I/O Expander

Home Page: https://wolles-elektronikkiste.de/en/port-expander-mcp23017-2

License: MIT License

C++ 99.17% C 0.83%
arduino arduino-library portexpander i2c mcp23017 mcp23s17 spi mcp23018 mcp23s18 attiny85

mcp23017_we's Introduction

MCP23017_WE

This is a library for the MCP23017, MCP23S17, MCP23018 and MCP23S18 I/O Port Expander.

You'll find some examples including the wiring (examples folder) which should enable you to use the library. Furthermore I have added a list of commands.

I have implemented an option to use it with an ATtiny and the TineWireM library.

In case you like this library it would be cool if you give it a star.

If you find bugs, please inform me!

Important notice: In 2022 Microchip has unfortunately updated the design of the MCP23017. GPA7 and GPB7 have lost their input function:
new_design_mcp23017
You can only use these pins as output. There is no change to the MCP23S17, MCP23018 and MCP23S18. You find this information in latest datasheet: https://www.microchip.com/en-us/product/mcp23017

Written by Wolfgang (Wolle) Ewald

http://wolles-elektronikkiste.de/portexpander-mcp23017 (German)

https://wolles-elektronikkiste.de/en/port-expander-mcp23017-2 (English)

mcp23017_we's People

Contributors

thijstriemstra avatar wollewald avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

mcp23017_we's Issues

v1.6.1 breaks something

my mcp23s17 + mcp23017 esp32 application seems to crash on v1.6.1, 1.6.0 seems to work fine.

Crash dump (if it helps at all):

mode:DIO, clock div:2
load:0x3fff0030,len:1184
load:0x40078000,len:13132
load:0x40080400,len:3036
entry 0x400805e4
Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.

Core  1 register dump:
PC      : 0x400d28d8  PS      : 0x00060a30  A0      : 0x800d29f9  A1      : 0x3ffb2720  
A2      : 0x1119010a  A3      : 0x000f4240  A4      : 0xffffffff  A5      : 0x00060a23  
A6      : 0x00060a20  A7      : 0x00000001  A8      : 0x00000001  A9      : 0x3ff44000  
A10     : 0x00000000  A11     : 0xfffc1fff  A12     : 0x00060a20  A13     : 0x00060c23  
A14     : 0x007bdc20  A15     : 0x003fffff  SAR     : 0x0000001b  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x11190126  LBEG    : 0x400d49b1  LEND    : 0x400d49c0  LCOUNT  : 0x00000000  


Backtrace:0x400d28d5:0x3ffb27200x400d29f6:0x3ffb2750 0x400d2a44:0x3ffb2770 0x400d2b35:0x3ffb2790 0x400d2cf2:0x3ffb27b0 0x400d1924:0x3ffb27d0 0x400d19b7:0x3ffb2800 0x400d553a:0x3ffb2820 




ELF file SHA256: 0000000000000000

Rebooting...
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2

Multiple MCP23017 outputs don't always work

I have a project that has 3 MCP23017 connected and was working fine, until I upgrade to V1.6.5. Sometimes when I compile my program all 24 output ports work fine and sometimes ports 9-24 will not work but output ports 1-8 always work. All 24 inputs work 100% of the time. So the second and third connected MCP output A ports don't always work. I downgraded to V1.6.0 and that seems to work all the time. I have tested the code on 2 different PCBA and both exhibit the same symptoms. All outputs are on port A of the MCP.

Works for Arduino?

Hi there, do your examples work with Arduino? I've just tried using one of the examples but it couldnt compile in my Arduino IDE? Thanks for any help! Paul

4 Rotary encoders

@wollewald thanks for this library, it looks better (maintained) than the other alternatives.

I have 4 rotary encoders with internal push buttons wired to a mcp23017 on an ESP32 wrover. This is how they're wired to the MCP23017:

Rotary Encoder Pin Label MCP23017 Pin Type
1 CLK 7 Digital Input
1 DT 4 Digital Input
1 Button 3 Digital Input
2 CLK 5 Digital Input
2 DT 6 Digital Input
2 Button 2 Digital Input
3 CLK 9 Digital Input
3 DT 10 Digital Input
3 Button 0 Digital Input
4 CLK 8 Digital Input
4 DT 11 Digital Input
4 Button 1 Digital Input

I have an interrupt pin (19) and reset pin (23) connected to the ESP32. The interrupt pin connects to ITA on the MCP23017. With the following code I get serial prints for everything on port A but port B is not printed (e.g. caught by interrupt). Everything from ports 0-7 is printed in terminal monitor, but not from ports 8-15. Can you help figure out what's wrong? How do I read the state of B pins?

#define MCP_ADDRESS 0x20 // (A2/A1/A0 = LOW) 

#include <Arduino.h>
#include <Wire.h>
#include <MCP23017.h>

int interruptPin = 19;
int resetPin = 23;

volatile bool event = false;
byte intCapReg; 

MCP23017 myMCP(MCP_ADDRESS, resetPin);

void IRAM_ATTR eventHappened(){
  Serial.println("event happened");
  event = true;
}

void setup(){ 
  pinMode(interruptPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(interruptPin), eventHappened, RISING);

  Serial.begin(115200);
  Wire.begin(SDAPin, SCLPin);

  myMCP.Init();
  myMCP.setPortMode(B00000000, A);  // Port A: all pins are INPUT
  myMCP.setPortMode(B00000000, B);  // Port B: all pins are INPUT
  delay(1000);
  myMCP.setInterruptPinPol(HIGH);
  delay(10);
  // interrupt pins, port, DEFVALB
  myMCP.setInterruptOnDefValDevPort(B11111111, A, B00001111);
  myMCP.setInterruptOnDefValDevPort(B11111111, B, B00001111);
  // pull-up for A0-A3
  myMCP.setPortPullUp(B00001111, A);
  event = false;
  Serial.println("ready");
}

void loop(){ 
  intCapReg = myMCP.getIntCap(A);
  if (event) {
    byte intFlagReg, eventPin; 
    intFlagReg = myMCP.getIntFlag(A);
    eventPin = log(intFlagReg) / log(2);
    intCapReg = myMCP.getIntCap(A);
    Serial.println("Interrupt!");
    /*Serial.print("Interrupt Flag Register: ");
    Serial.println(intFlagReg, BIN); 
    Serial.print("Interrupt Capture Register: ");
    Serial.println(intCapReg, BIN);*/
    Serial.print("Pin No.");
    Serial.print(eventPin);
    Serial.print(" went ");
    if((intFlagReg&intCapReg) == 0){ // HIGH/LOW or LOW/HIGH change?
      Serial.println("LOW");
    }
    else{
      Serial.println("HIGH");
    }
    event = false;
    Serial.println();
  }
}

Make MCP23s17 reset pin optional

There is no optional reset pin for the mcp23s17 like there is for the mcp23017 (#4).

Supplying -1 throws this errors:

[    27][E][esp32-hal-gpio.c:95] __pinMode(): Invalid pin selected
E (24) gpio: gpio_set_level(226): GPIO output gpio_num error
E (25) gpio: gpio_set_level(226): GPIO output gpio_num error
E (39) gpio: gpio_set_level(226): GPIO output gpio_num error

Passing an unused pin, e.g. 27 in my case, seems to work.

add LICENSE

it's not clear what license this library uses. it should be defined in metadata file and i'd personally add a LICENSE file in the root of repo as well. Github also provides a license setting for a repository to make it clear what license is used.

Compile warnings

See travis build: https://travis-ci.com/github/wollewald/MCP23017_WE/jobs/365247364#L419

lib/MCP23017_WE/src/MCP23017.cpp: In member function 'byte MCP23017::getIntFlag(MCP_PORT)':

lib/MCP23017_WE/src/MCP23017.cpp:386:1: warning: control reaches end of non-void function [-Wreturn-type]

 }

 ^

lib/MCP23017_WE/src/MCP23017.cpp: In member function 'byte MCP23017::getPort(MCP_PORT)':

lib/MCP23017_WE/src/MCP23017.cpp:404:1: warning: control reaches end of non-void function [-Wreturn-type]

 }

 ^

lib/MCP23017_WE/src/MCP23017.cpp: In member function 'byte MCP23017::getIntCap(MCP_PORT)':

lib/MCP23017_WE/src/MCP23017.cpp:409:1: warning: control reaches end of non-void function [-Wreturn-type]

 }

 ^

random pins are driven

when i set a pin as a output and try to drive the pin on a event from lvgl gui button press i.e:
void turnoncentrelights() {
myMCP.setPin(7, A, HIGH); // Send high signal to turn on relay
Serial.println("centre lights turned on");
}
The chip drives just any random pin on port A, switching whichever relay it pleases, and not the one specified. i have 4.7k resistors on sda/scl and have wired RST pin to 3v3 with 1k resistor. using addr 0x27

setPinMode vs setPortMode -> INPUT_PULLUP

Hi!

First thanks for your incredible work. I tried the Adafruit library, but for my setup (ESP-01s + 2x MCP23017) it did not work. Your library does work great, but I have an understanding-questing:

Using setPortMode I can set a port to input or output, correct? But can I set it to INPUT_PULLUP too, or is the only way to do this the setPinMode function?

Best

Pascal

Support for MCP23S17

From https://www.best-microcontroller-projects.com/mcp23017.html:

the MCP23S17 uses the SPI interface that can operate at 10MHz (a lot faster than the I2C version). This SPI device has the same number and arrangement of pins, but uses two unused (I2C) pins to implement the SPI interface. In all other respects it operates the same as the MCP23017.

More on:

s-l1600

license type not mentioned in headers

the license type should be mentioned in the (copyright) headers:

/*****************************************
This is a library for the MCP23017 and MCP23S17 I/O Port Expander
This specific header file is just for introducing the class name MCP23S17. It inherits 
everything from MCP23017, including the constructors. 
You'll find some examples including the wiring which should enable you to use the library. 
Furthermore I have added a list of commands.
You are free to use it, change it or build on it. In case you like it, it would be cool 
if you give it a star.
If you find bugs, please inform me!
Written by Wolfgang (Wolle) Ewald
https://wolles-elektronikkiste.de/port-expander-mcp23017-2?lang=en
*******************************************/

And I would minimize the extra info..

Also note that mcp23x18 is not referenced either.

How to use an AC solid state relay to also switch DC?

Hi Wolfgang,

I'm using a 16 channel AC solid state relay board to switch on and off 24VAC, 0.5A loads. That's all fine. Now I'd like to be able to use the relays to switch on and off a 5VDC, 50mA load too! Of course, I can use these relays to turn on the 5VDC. But I understand
I can't use them to turn off the 5VDC. Sooo, I was wondering, if I reset the relay board- via the pin 5 reset, would that turn off all the relays? And therefore turn off the 5VDC, 50mA load?

Thanks alot,
Paul

Make reset pin optional

I just found out the MCP203017 reset pin was hooked up to a different pin on the ESP32 than one I specified. But it doesn't seem to have made a difference, everything properly worked anyway. So is the reset pin optional?

MCP23S17/MCP23017 missing two input bit of the 16Bit Chip

Problem with follow Chips:

  • MCP23S17
  • MCP23017

when one of the pins (GPA7 or GPB7) are high there will output a strange result:

correct output:
00000000 00000000 (all pins are connected to a other input source at low)
01000000 00000000 (one input are high, correct result)

not correct result:
0000000 00000000 (one bit are missing from bank A or B)

why does the output show results where a 1/0 is missing?

Code:

#include <SPI.h>
#include <MCP23S17.h>
#define CS_PIN 7   
#define RESET_PIN 5 
#define MCP_ADDRESS 0x20

MCP23S17 myMCP = MCP23S17(CS_PIN, RESET_PIN, MCP_ADDRESS);


int wT = 500;
byte portStatusA;
byte portStatusB;


void setup(){ 
 
  Serial.begin(9600);
  SPI.begin();

  if(!myMCP.Init()){
    Serial.println("Not connected!");
    while(1){} 
  }
 
  Serial.println("CHIP: ready");
  delay(wT);

  myMCP.setPortMode(0b00000000, A);   // Port A: all pins are INPUT
  myMCP.setPortMode(0b00000000, B);   // Port B: all pins are INPUT
  delay(wT);
  
  myMCP.setPortPullUp(0b11111111, A);  // pulled up
  myMCP.setPortPullUp(0b11111111, B);  // pulled up
}

void loop(){ 

  portStatusA = myMCP.getPort(A); // query the complete port status 
  portStatusB = myMCP.getPort(B); // query the complete port status 

  Serial.print(portStatusA, BIN);
  Serial.print(" ");
  Serial.print(portStatusB, BIN);
  Serial.println("");
 
  delay(100);
} 

How to change I2C address

hi @wollewald I have one of these mcp23017 devices:

IMG_1531

and the address 0x20 shows up. I want to add another one and change the address to let's say 0x21. Any idea what I need to do with these A0, A1, A2 jumpers?

How to check if MCP23017 is connected and responsive?

Hi wollewald, this is more just a question, not an issue.

I have a sketch which checks all I2C slave devices every 10 seconds, to ensure they are connected and responsive.
If any I2C device does not respond, I then raise an alarm flag for that device.

Is there a library function for this purpose, to check if the MCP23017 is responsive?

I have tried the following:

  if ((mcp_0x20.Init() == true) && (mcp_0x21.Init() == true))
  {
    mcp_error = false;
  }
  else if ((mcp_0x20.Init() == false) || (mcp_0x21.Init() == false))
  {
    mcp_error = true;
  }

For some reason, the above code stops both MCP23017's from working all togeather (they no longer repond to library commads such as setPin and setAllPins).

If I comment-out the above section of code, both MCP23017's function normally again.

Any advice would be greatly appreciated?

Invert pin state

Many of the opto isolated relay banks are active low requiring a switch to ground to turn them on. I have begun using your flexible and versatile library, however it is counter-intuitive or seems illogical to set the output state in code to 'OFF' so that the output pin goes low to turn ON the relay. Is it possible to have an 'invert' function so issuing an 'ON' command sets the output to low on the MCP23017.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.