I'm trying to use a XY-160D motor driver to control a DC motor with encoder. I tested the encoders first and I know they work, but I can't get the Arduino to control the motor through the motor driver. I'm using a Nano 33 IoT. The motor is a JGY-370 12VDC.
For your viewing pleasure, here is a schematic I made of my set up:

And the code I'm using:
#include "thingProperties.h"
#define ENCA 2
#define ENCB 3
#define PWM 5
#define IN1 6
#define IN2 10
int pos = 0;
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1000);
pinMode(ENCA, INPUT);
pinMode(ENCB, INPUT);
attachInterrupt(digitalPinToInterrupt(ENCA), readEncoder, RISING);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
// Your code here
setMotor(1,40,PWM,IN1,IN2);
delay(1000);
Serial.println(pos);
Serial.println("TEST1");
setMotor(-1,40,PWM,IN1,IN2);
delay(1000);
Serial.println(pos);
Serial.println("TEST2");
setMotor(0,40,PWM,IN1,IN2);
delay(1000);
Serial.println(pos);
Serial.println("TEST3");
}
void setMotor(int dir, int pwmVal, int pwm, int in1, int in2){
analogWrite(pwm,pwmVal);
if(dir == 1){
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
}
else if(dir == -1){
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
}
else{
digitalWrite(in1,LOW);
digitalWrite(in2,LOW);
}
}
void readEncoder() {
int b = digitalRead(ENCB);
if (b > 0) {
pos++;
}
else {
pos--;
}
}
/*
Since DoorMotor is READ_WRITE variable, onDoorMotorChange() is
executed every time a new value is received from IoT Cloud.
*/
void onDoorMotorChange() {
// Add your code here to act upon DoorMotor change
}
I found this website that gives a specific example with this motor driver and an Arduino, and I can't see anything obviously wrong with what I'm doing and what they're doing, save for my motor has an encoder and theirs doesn't. That website also shows the complete layout of the driver board. The motor driver is capable of controlling 2 motors, I only have one. I've tried moving both the motor outputs between Motor1 and Motor2 and same with the signal cables, and nothing happens. The motor doesn't receive power at all. I've tried different digital pins (5, 6, and 10 are just the most recent troubleshooting effort) to no avail.
I think it must be an issue with how I've connected things to the driver, but I've triple checked everything and I can't just figure it out. I know my code is running and the function to control the motor is activating, but getting lost somewhere along the line. Any help is appreciated and thanks in advance.
