TroubleshootingArduino

Arduino Leonardo and DFRobot MCP23017 expansion board

userHead Sean.Haynes 2024-09-08 02:38:03 14 Views1 Replies

Afternoon all - I have been playing with making a controller using various toggle and rotary switches - it became apparent that there were not going to be enough pins on the Arduino Leonardo board that I have. So I purchased an expansion board - DFRobot MCP23017.

Not having used one before I loaded the example button sketch onto the Leonardo so I could figure out how it works. It seemd fairly straight forward - so I then started to incorporate the code into the existing joystick sketch I had been working on. Initially everything seemed to be working fine, but when I came back the next day to carry on I had all sorts of issues. I had thought that perhaps a wire had come lose, but having checked all the wires that isn't the case.

 

If I load the sketch removing all instances that refer to the MCP board , the 3 toggle switches I have assigned on the Leonardo work perfectly - each toggle is on-off-on, or on-off-mom - so 6 pins in all assigned on the Leonardo. 1-6 

 

I have a seperate on-off-on toggle as an experiement on the MCP board. If I load the sketch below but DO NOT attach any of the 3 toggle switches to the Leonardo, the switch on the MCP works fine.  The toggle swiches are 3 pin with the ground/ common the middle pin. the two outer pins are connected to the IO pins on PB0 and PB1 respectively.

 

If however I attach a toggle switch to pins1 and 2 on the Leaornado, after a few seconds of operation the board locks up so I have to reboot the card. The same thing happens if I use the exact same switch on pins 3 and 4........however…… if I attach the exact same switch to pins 5 and 6 it works perfectly in unison with the toggle switch attached to the MCP card!

So in short for some reason I can't use pins 1-4 on the Leonardo with the MCP configured.

I am at a loss - it seems like there is some sort of addressing issue going on but I don't have that indepth kind of knowledge - hoping someone here does - please see sketch below:

 

#include <Joystick.h>

#include <DFRobot_MCP23017.h>

DFRobot_MCP23017 mcp(Wire, 0x20);

#define Collective A0

int rxAxis_ = 0;

#define Throttle A1

int Throttle_ = 0;

#define Button1 PD1

#define Button2 PD2

#define Button3 PD3

#define Button4 PD4

#define Button5 PD5

#define Button6 PD6


 

#define Button7 mcp.eGPB0


 

#define Button8 mcp.eGPB1


 

int lastButton1State = 0;

int lastButton2State = 0;

int lastButton3State = 0;

int lastButton4State = 0;

int lastButton5State = 0;

int lastButton6State = 0;

int lastButton7State = 0;

int lastButton8State = 0;


 

Joystick_ Joystick(0x08, JOYSTICK_TYPE_JOYSTICK,


 

                   9, 0,  // Button Count, Hat Switch Count


 

                   false, false, false,  // X and Y, Z Axis


 

                   true, false, false,  //  Rx, Ry, or Rz


 

                   false, true,  //  rudder, throttle


 

                   false, false, false);  // accelerator, brake, or steering


 

const bool initAutoSendState = true;



 

void setup() {

  Joystick.begin();

  mcp.begin();

  pinMode(Button1, INPUT_PULLUP);

  pinMode(Button2, INPUT_PULLUP);

  pinMode(Button3, INPUT_PULLUP);

  pinMode(Button4, INPUT_PULLUP);

  pinMode(Button5, INPUT_PULLUP);

  pinMode(Button6, INPUT_PULLUP);

  mcp.pinMode(mcp.eGPB0, INPUT_PULLUP);

  mcp.pinMode(mcp.eGPB1, INPUT_PULLUP);

  Serial.begin(9600);

}



 

void loop() {



 

  Throttle_ = analogRead(Throttle) / 04;

  Throttle_ = map(Throttle_, 0, 1023, 0, 255);

  Joystick.setThrottle(Throttle_);



 

  rxAxis_ = analogRead(Collective);

  rxAxis_ = map(rxAxis_, 0, 1023, 0, 255);

  Joystick.setRxAxis(rxAxis_);


 

  int currentButton1State = !digitalRead(Button1);

  if (currentButton1State != lastButton1State) {

    Joystick.setButton(0, currentButton1State);

    lastButton1State = currentButton1State;

  }


 

  int currentButton2State = !digitalRead(Button2);

  if (currentButton2State != lastButton2State) {

    Joystick.setButton(1, currentButton2State);

    lastButton2State = currentButton2State;

  }


 

  int currentButton3State = !digitalRead(Button3);

  if (currentButton3State != lastButton3State) {

    Joystick.setButton(2, currentButton3State);

    lastButton3State = currentButton3State;

  }


 

  int currentButton4State = !digitalRead(Button4);

  if (currentButton4State != lastButton4State) {

    Joystick.setButton(3, currentButton4State);

    lastButton4State = currentButton4State;

  }


 

  int currentButton5State = !digitalRead(Button5);

  if (currentButton5State != lastButton5State) {

    Joystick.setButton(4, currentButton5State);

    lastButton5State = currentButton5State;

  }


 

  int currentButton6State = !digitalRead(Button6);

  if (currentButton6State != lastButton6State) {

    Joystick.setButton(5, currentButton6State);

    lastButton6State = currentButton6State;

  }


 

  int currentButton7State = !mcp.digitalRead(Button7);

  if (currentButton7State != lastButton7State) {

    Joystick.setButton(6, currentButton7State);

    lastButton7State = currentButton7State;

  }


 

  int currentButton8State = !mcp.digitalRead(Button8);

  if (currentButton8State != lastButton8State) {

    Joystick.setButton(7, currentButton8State);

    lastButton8State = currentButton8State;

  }

  Joystick.sendState();


 

  delay(100);

}

2024-09-08 03:28:35

RESOLVED - I found out what was happening. Though the Leonardo has what seems to be dedicated SCL and SDA connectors on the top right of the card, they are in fact only extensions of pins d2 and d3. As I had configured toggle switches to use those pins it was causing the issue. I test by attaching a toggle switch to pins D1 and D4, all work as expected.

userHeadPic Sean.Haynes