Problem to adapt existing Libraries via MCP23017


I have some problems to adapt the existing library to work via a Port Expander.

Library to adapt: IRremote
Library to communicate: Adafruit
Port Expander: MCP23017
Arduino: Uno

My idea was to “just” edit the input pin, that it uses one MCP pin and sends the signal to the Arduino.

I attached both the edited .cpp and .h file.

To make it easier, I will post here what I edited:

I’ll start with the .cpp file. I added this at the top to get the libraries in and started:

#include "../Adafruit-MCP23017-Arduino-Library-master/Adafruit_MCP23017.h"
#include "C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire\Wire.h"

Adafruit_MCP23017 mcp;


Then I changed this at line 366/367 to activate the right pin at the MCP:

  // set pin modes
  mcp.pinMode(irparams.recvpin, INPUT);

After this I went to line 389 to read from the right pin:

  uint8_t irdata = (uint8_t)mcp.digitalRead(irparams.recvpin);

That was all for the .cpp file. Now comes the .h file. I just added some lines in the beginning:

#include "../Adafruit-MCP23017-Arduino-Library-master/Adafruit_MCP23017.h"
#include "C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire\Wire.h"

So my problem with this probably wrong solution is that it never stops to compiling. I’m actually expecting an error with the strange import of the wire library, but I didnot found anything else to stop giving this error message to me:

C:\Users\MYUSERNAME\Documents\Arduino\libraries\Simpletest\Simpletest.cpp:1:18: fatal error: Wire.h: No such file or directory
#include <Wire.h>
compilation terminated.
Error compiling.

So, does anyone of you know what I am doing wrong? Any help appreciated. Thanks.



IRremote.cpp (36.5 KB)

IRremote.h (5.42 KB)

The IRremote library reads the pin during a timer interrupt. If the MCP library requires the interrupts to be enabled it won't work.

Hey johnwasser,

thanks for your reply. I think I tried to solve all my problems at once without perfectly knowing what I am doing. So i switched from the IR Library to just an own wrote BlinkLed Library.

But I’m still struggling to bring them both together into one library. To give you an idea I will post the error message, the library and my sketch.

Error message:

MCP_BlinkLED\MCP_BlinkLED.cpp.o: In function MCP_BlinkLED::MCP_BlinkLED(int)': C:\Program Files (x86)\Arduino\libraries\MCP_BlinkLED/MCP_BlinkLED.cpp:15: undefined reference to Adafruit_MCP23017::begin()’
C:\Program Files (x86)\Arduino\libraries\MCP_BlinkLED/MCP_BlinkLED.cpp:16: undefined reference to Adafruit_MCP23017::pinMode(unsigned char, unsigned char)' MCP_BlinkLED\MCP_BlinkLED.cpp.o: In function MCP_BlinkLED::Blink()’:
C:\Program Files (x86)\Arduino\libraries\MCP_BlinkLED/MCP_BlinkLED.cpp:22: undefined reference to Adafruit_MCP23017::digitalWrite(unsigned char, unsigned char)' C:\Program Files (x86)\Arduino\libraries\MCP_BlinkLED/MCP_BlinkLED.cpp:24: undefined reference to Adafruit_MCP23017::digitalWrite(unsigned char, unsigned char)’
collect2.exe: error: ld returned 1 exit status
Fehler beim Kompilieren.

Has anyone an idea or maybe a tutorial for combining different libraries? Any help is appreciated. Thanks.

Greetings Alex.

MCP_BlinkLED.cpp (526 Bytes)

MCP_BlinkLED.h (410 Bytes)

Test_LIB_MCP_BlinkLED.ino (212 Bytes)

Hey guys,
okay, so thanks to this post, i have seen that there is only a “workaround” to include selfmade libraries:

Now the compiler works fine, but still my LED is not blinking. I controlled the connection with the I2C Scanner.

Again I added all my sketches. Any advice is really appreciated.

Short summary on my problem:

I am struggling to get libraries running via a MCP23017 Port Expander (Arduino <-> I2C <-> MCP_23017 <-> Devices).
So to understand my problem better, i am trying to write an own library.

Greetings Cyancali.

MCP_BlinkLED.cpp (526 Bytes)

MCP_BlinkLED.h (410 Bytes)

Test_LIB_MCP_BlinkLED.ino (244 Bytes)

my BlinkLED Lib is running now, but it looks quite complicated to me. :smiley:
So if anyone has an idea to make it without the initialize() function, i would be really thankful. Thank you very much.

And also does anyone know why it is not possible that the library intializes itself? I don’t get any error message with the other version above, but the LED is not doing anything.

Here comes the sketch:


#include “Adafruit_MCP23017.h”
#include “MCP_BlinkLED.h”


void setup() {
// put your setup code here, to run once:

void loop() {
// put your main code here, to run repeatedly:

…and the .cpp file:

#include “Arduino.h”
#include “MCP_BlinkLED.h”

Adafruit_MCP23017 mcp;

MCP_BlinkLED::MCP_BlinkLED(int pin)
_pin = pin;

void MCP_BlinkLED::initialize()
mcp.pinMode(_pin, OUTPUT);

void MCP_BlinkLED::Blink()
mcp.digitalWrite(_pin, HIGH);
mcp.digitalWrite(_pin, LOW);

…and the .h file:

#ifndef MCP_BlinkLED_h
#define MCP_BlinkLED_h

#include “Arduino.h”

class Adafruit_MCP23017;

class MCP_BlinkLED
MCP_BlinkLED(int pin);
void initialize();
void Blink();
int _pin;


Greetings Alex.