Arduino Pro Micro with Joystick Library + MCP23017

Hi I'm building a button box for sim racing with a Pro Micro and I need to use a port expander which I already have a couple of MCP23017 from another project.
I'm using this Joystick Library GitHub - MHeironimus/ArduinoJoystickLibrary: An Arduino library that adds one or more joysticks to the list of HID devices an Arduino Leonardo or Arduino Micro can support.

While the other project went well, this time it's giving me a problem.
Basically while the buttons wired directly to the arduino are working without issues, the one I wired to the MCP for testing it's like it's always being pressed.
I tried also not using pullup resistor without any improvement and since it's been a while from the last time I used arduino I a little lost and cannot figure out what I'm missing to make this work as expected.

I'll leave the code and some screenshots of the wiring and the windows controller settings
In the wiring screenshot are missing two additional buttons that are wired to the 4 and 5 pins, but they're working without issues.
Also I want to specify (Maybe it's a relevant thing or maybe not) that in the final version of this project the MCP pins will be wired to a buttons matrix while arduino pins will be wired to the encoders and potentiometers

Edit: I Tried it with my other project which involved the port expander with buttons and leds on it and works flawlessly.

#include <Adafruit_MCP23017.h>
#include <Joystick.h>

#define BRAKE A3
#define GEAR_UP 4
#define GEAR_DOWN 5
#define MCP_BUTTON1 15

Adafruit_MCP23017 mcp;

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_MULTI_AXIS, 32, 0, false, false, false, false, false, false, false, false, false, true, true);

const bool initAutoSendState = true;

int lastGearUpState = 0;
int lastGearDownState = 0;
int lastButton1State = 0;

int brake_ = 0;

void setup() {
pinMode(GEAR_UP, INPUT_PULLUP);
pinMode(GEAR_DOWN, INPUT_PULLUP);

mcp.begin(0);
mcp.pinMode(MCP_BUTTON1, INPUT_PULLUP);

Joystick.begin();
}

void loop() {
  brake_ = analogRead(BRAKE);
  brake_ = map(brake_, 0, 1023, 0, 255);
  Joystick.setBrake(brake_);

  int currentLastGearUpState = !digitalRead(GEAR_UP);
  if(currentLastGearUpState != lastGearUpState)
  {
    Joystick.setButton(0, currentLastGearUpState);
    lastGearUpState = currentLastGearUpState;
  }

  int currentLastGearDownState = !digitalRead(GEAR_DOWN);
  if(currentLastGearDownState != lastGearDownState)
  {
    Joystick.setButton(1, currentLastGearDownState);
    lastGearDownState = currentLastGearDownState;
  }

  int currentLastButton1State = !mcp.digitalRead(MCP_BUTTON1);
  if(currentLastButton1State != lastButton1State)
  {
    Joystick.setButton(2, currentLastButton1State);
    lastButton1State = currentLastButton1State;
  }

  delay(10);
}

Screenshot 2023-10-29 111338


mcp

MCP23017 Datasheet: https://cdn-shop.adafruit.com/datasheets/mcp23017.pdf

Pins Connections:

-Arduino VCC to:

  • MCP VDD (pin 9)
  • RESET with 10k Ohm Resistor (pin 18)

-Arduino GND to:

  • MicroSwitch Common Pin
  • MCP VSS (pin 10)
    *MCP Pins A0,A1,A2 for I2C address (Pins 15,16,17)

-Arduino Pin 2 (SDA) to:

  • MCP SDA (Pin 13)

-Arduino Pin 3 (SCL) to:

  • MCP SCL (Pin 12)

-MCP Pin GPB7 (Pin 8) to:

  • MicroSwitch NO (Normally Open) pin

There is this really bad habit of fritzing pictures all over the internet.
I have some questions:
Did you use a small test-code that is well known to work properly to test your wiring.

What is the name of the Pin on the MCP23017-chip you connected the resistor to?
What is the name of the Pin on the MCP23017-chip you connected the switch to?
What are the names of the MCP23017-chip that you connected to
hm I can't read the numbers on the microcontroller-board.

Would you mind to unload the work of searching for the datasheet of the MCP23017 from other users shoulders and post a link to the MCP23017-datasgheet yourself?

Would you mind and please post a real schematic with easy to read chip-pin-numbers?

Read how to provide easy to read informations about hardware and wiring

best regards Stefan

Edited the post.

Is that enough?
I can't do a proper diagram now, but fritzing diagram with the added list of pin connections and the pinout of the port expander it should be ok

more or less

did you look at the diagram that I have drawn in the tutorial?
Here is a 5 minute schematic where all relevant information is onto it.
Proper does not mean exactly drawn each pin at its physical place.
Sufficient is if all relevant information is on the drawing. Wiggly waggly corrected is sufficient

The used pin-numbers are named with their number and their function.
And that's it.

What is so hard about such a hand-drawing? I say nothing.

So my questions are:

  • Did you check if you get a response at all from the MCP23017 if you run the I2C-scanner-sketch?
  • did you run a test-sketch that simply prints out the state of all 16 IO-pins to the serial monitor?

What does the check all MCP23017-io-pins code show if you connect a pin to +Vcc / GND?

Does the MCP23017-library provide a function-result if the microcontroller can communicate with the MCP23017?

I looked up the demo-codes on github. There is one for buttons

All of these examples do check if communication with the MCP23017 work

  if (!mcp.begin_I2C()) {
    Serial.println("Error.");
    while (1);
  }

So what are your answers to the questions?

So you have to check

  • does communication on the I2C-bus work at all with I2C-scanner
  • does reading in the switch work at all by printing to the serial monitor

checking if sending button-pressed works at all.

  • using a code that does send hardcoded joystick commands
    hardcoded means
    inside loop do nothing more than
    set button to pressed with a function-call with the state coded as a number 1
    wait a second
    set button to **released ** with a function-call with the state coded as a number 0

This way you reduce the number of possible reasons what might not work to a mimimum.

best regards Stefan

Ok I tried these things:

  • Triple checked all the wiring and compared it to multiple website and tutorials.
  • Used the I2C Scanner and successfully detects the chip.
  • Tried to change the i2c address, it's still being detected.
  • Checked if when declaring pullup resistors it was realling pulling them up, and yes it is.
  • All of these operations where done on all af the the 7 MCP chips I have and both with the Arduino Pro Micro and with the Arduino Uno

In every situation I get always 0 from the serial monitor

Datasheet shows MCP23017 pin 15 as A0.      ???

@danielrama35

we (the other forum-users) can't see on your desk
we can't see over your shoulder while you are doing a test.

This means you have to provide very detailed information of what you did.

  • posting the comlete sketch as a code-section that you used

  • explaining all the details of what you really did as

Did you use a code that does the check

 if (!mcp.begin_I2C()) {
    Serial.println("Error resulted in false");
    while (1);
  }

??

and then really post what was printed to the serial monitor.

Do you see the attention-mark?
if ( !mcp.

very important detail. It is the not-operator inverting the result.
the meaning is if not mcp.begin()

Did you really use a test-code that reads in all 16 IO-pins from the MCP23017?

Did you directly connect +Vdd one by one to each of the 16 IO-pins and watched what was printed to the serial monitor?

Did you directly connect ground one by one to each of the 16 IO-pins and watched what was printed to the serial monitor?

Anyway post a real picture taken from vertically above so that it will be easy to see which wire is connected to what.

best regards Stefan

When I'll have time I'll reply

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.