Esp32 not talking properly with gpio expander

Hey everyone,

I'm relatively new to playing around with arduinos. I've been trying to use an mcp23017 gpio expander to control a motor board with two different motors, but the board isn't controlling the motors properly.

Sometimes only one of the motors would work, sometimes both of them would work, and sometimes none of them would work.

I suspect the problem's with how I set up the I2C protocol, because if I change the baud rate on serial.begin() alongside monitor_speed then their not-working-properly pattern is different. For instance if I change from 115200 to 40000, they work fine for a second or so until motor A cuts out.

I tried using Wire.setClock() to see if it might fix it, but the motors are still acting weird.

I'd really appreciate some insight as to how I could fix this issue

Source code:

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

// Function declarations:
void MotorLoop();

// MCP pins and variables
MCP23017 mcp;

// ================== Pins: ==================
const int PWMB = 19; // PWM pin for motor 1
const int PWMA = 18; // PWM pin for motor 2

// ================ MCP pins: ================

// Pins for motor 1
const int MAI1 = 5;
const int MAI2 = 4;

// Pins for motor 2
const int MBI1 = 6;
const int MBI2 = 7;

// Pins to talk to MCP23017
void setup() {
  Serial.begin(115200);
  Wire.begin(12, 14);  // begin(SDA pin, SCL pin)
  Wire.setClock(115200);

  // Initialize MCP23017
  mcp.begin();

  // Motor 1 pins
  mcp.pinMode(MAI1, OUTPUT);
  mcp.pinMode(MAI2, OUTPUT);

  // Motor 2 pins
  mcp.pinMode(MBI1, OUTPUT);
  mcp.pinMode(MBI2, OUTPUT);
  pinMode(PWMB, OUTPUT);
}

void loop(){
  MotorLoop();
}

void MotorLoop() {
  // Motor 1 Forward
  mcp.digitalWrite(MAI1, HIGH);
  mcp.digitalWrite(MAI2, LOW);
  analogWrite(PWMA, 255);

  // Motor 2 Forward
  mcp.digitalWrite(MBI1, HIGH);
  mcp.digitalWrite(MBI2, LOW);
  analogWrite(PWMB, 255);

  delay(2000);

  // Motor 1 Reverse
  mcp.digitalWrite(MAI1, LOW);
  mcp.digitalWrite(MAI2, HIGH);
  analogWrite(PWMA, 128);

  // Motor 2 Reverse
  mcp.digitalWrite(MBI1, LOW);
  mcp.digitalWrite(MBI2, HIGH);
  analogWrite(PWMB, 128);

  delay(2000);
}
type or paste code here

Platform.ini:

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps = 
	sstaub/MCP_23017@^1.0.0
monitor_speed = 115200

Can you post an annotated schematic showing exactly how you wired this. Be sure to show all components such as resistors etc. Also post links to technical information on each of the hardware items including the ESP device. Be sure to show power sources.

Ok so I think I found the issue:

mcp23017-pinout-library-pins

I was connecting vdd to 5 volts but the reset pin to 3.3v. Once I put both of them on 3.3 volts the problem seemed to fix itself. I'm using a 10k ohm resistor between reset and 3.3v btw.

One thing that I noticed though was that if I changed the baud rate from 115200 to any other number, the pattern desynchronizes after a while. For instance it might start off with 2 seconds high, 2 seconds low. In like 15 seconds it'll be 3 seconds high 1 second low. Is that normal, is there anything special with the number 115200?

That is a weird nonstandard value. Is there some particular reason for it?