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