Core Electronics Forum

VNH5019 Motor Driver Shield Coding Current Range under 1Amp

Using sample code that came with the VNH5019 Motor Driver Shield for Arduino, it keeps executing the error code on the first loop count. I am thinking its because I am driving a 12V fan that only requires a max of 0.29A. So the question is, how do I change the current range? I think, the sample code changes the current via motor speed setting whilst leaving the voltage constant. Am I to assume the setting of 400 in the code equates to 12A per motor? If this isn’t the reason why code keeps going to the error function, please let me know what I else I can try to debug.

Hey Eddie,

Welcome to the forum! :partying_face:

Can you please send through the script and the error messages that you’re receiving in this case, it’d be helpful to get some fresh eyes on this one to debug with you.

Bryce
Core Electronics | Support

I figured it out. Turns out I had incorrect pinouts when I connected it to my Particle Photon. I used the Particle Shield Shield so pinouts don’t exactly match and I needed to change it code. Also with Photon, you can dynamically allocate a pin to be Analog or Digital or PWM. I didn’t do that either. Once I did all that, it worked perfectly.

I do however have another issue. For some reason, if I mapped pins directly to the Particle Photon it works fine but once I connect it to the Particle Shield Shield, it seems to fail at it runs the function stopIfFault(). Now I would have thought that it may still be a pin issue but if that was the case then I wouldn’t be able to run the shield and I wouldn’t be able to get the current reading

My script as follows:
//------------------------------------------------------------------------------
//Module MotorShield
#include <DualVNH5019MotorShield.h>

DualVNH5019MotorShield md;

void stopIfFault()
{
if (md.getM1Fault())
{
Particle.publish(“Error”, “M1 fault”, PRIVATE);
while(1);
}

if (md.getM2Fault())
{
Particle.publish(“Error”, “M1 fault”, PRIVATE);
while(1);
}

}

int M1EN = D6;
int M2INA = D7;
int M2INB = D5;
int M1PWM = A4;
int M2PWM = A5;
int M1INA = D2;
int M2EN = D3;
int M1INB = D4;
int M1CS = A0;
int M2CS = A1;

// Variables used for Particle Cloud Function
int fanCommand = 0;
int tempCommand = 0;
String fCommand = “”;
String tCommand = “”;

//Library and Variable for Checking Wifi
#include “Particle.h”
uint32_t wLastTime;
double MCurrent = 0;

//-----------------------------------------------------------------------------------------------------------------------------
// setup() runs once, when the device is first turned on.
void setup()
// Put initialization like pinMode and begin functions here.
{

//------------------------------------------------------------------------------
//Module Motor Shield

Particle.variable(“MCurrent”, MCurrent);
pinMode(M1EN, INPUT);
pinMode(M2INA, OUTPUT);
pinMode(M2INB, OUTPUT);
pinMode(M1PWM, OUTPUT);
pinMode(M2PWM, OUTPUT);
pinMode(M1INA, OUTPUT);
pinMode(M2EN, INPUT);
pinMode(M1INB, OUTPUT);
pinMode(M1CS, INPUT);
pinMode(M2CS, INPUT);

md.init();

Particle.publish(“MxEN:”, String(md.getM1Fault()) , PRIVATE);

//Function to connect to Particle Cloud
Particle.function(“SetFanSpeed”, setFanSpeed);
Particle.function(“SetTemperature”, setTemp);

}
//-----------------------------------------------------------------------------------------------------------------------------

// loop() runs over and over again, as quickly as it can execute.
void loop()
{
//Module Motor Shield

//Code to check WiFi
if (!Particle.connected()) {
//try reconnect forever
wLastTime = millis();
while(!Particle.connected()) {Particle.process();}
}//if (!Particle.connected())

wLastTime = millis();
while(millis() - wLastTime < 1000) {Particle.process();}

//Activate Motor
if (Particle.connected())
{
/*
if(fanCommand > 0)
{
setFanSpeed(fCommand);
}
else
{
setFanSpeed(“0”); // stop the fan
}
if(tempCommand > 0)
{
setTemp(tCommand);
}
else
{
setTemp(“0”)";
}
*/

}//if (Particle.connected())
delay(5000);
} //loop

//------------------------------------------------------------------------------
// Module Motor Shield
//set fan speed function
int setFanSpeed(String fCommand)
{
//Forward Increase M1
//for (int i = 324; i <= 393; i++) //Fan Speed between 350 and 394. It will suddenly go max as soon as you put it to 394. Can get to 250 but must first ramp up to 393 first to get it going
//{
fanCommand = fCommand.toInt();
md.setM1Speed(fanCommand);
Particle.publish(“M1 Speed”, String(fanCommand) , PRIVATE);
delay(1000);
Particle.publish(“M1EN:”, String(digitalRead(M1EN)) , PRIVATE);
delay(1000);
stopIfFault();
//if (fanCommand%200 == 100)
//{
MCurrent = md.getM1CurrentMilliamps();
Particle.publish(“M1 Current(mA)”, String(MCurrent,2), PRIVATE);
//}
delay(1000);
//}
return 1;
}

//set temperature function
int setTemp(String tCommand)
{
//Forward Increase M2
//for (int j = 0; j <= 400; j++)
//{
tempCommand = tCommand.toInt();
md.setM2Speed(tempCommand);
Particle.publish(“M2 Speed”, String(tempCommand) , PRIVATE);
delay(1000);
Particle.publish(“M2EN:”, String(digitalRead(M2EN)) , PRIVATE);
delay(1000);
stopIfFault();
//if (tempCommand%200 == 100)
//{
MCurrent = md.getM2CurrentMilliamps();
Particle.publish(“M2 Current(mA)”, String(MCurrent,2), PRIVATE);
//}
delay(1000);
//}
return 1;
}

Hey Eddie,

Out of curiosity, if you replace

{
Particle.publish(“Error”, “M1 fault”, PRIVATE);
while(1);
}

with

{
Particle.publish(“Error”, “M1 fault”, PRIVATE);
}

Does the error occur repeatedly?

Bryce
Core Electronics | Support