VNH2SP30 H-Bridge DC motor driver library for SparkFun MonsterMoto Shields and el-cheapo Chinese driver modules
The original example sketch by Jim Lindblom is now made object-oriented programming friendly. A few extra functions have also been added to the library. See below for details.
This library is written in object-oriented C. I could have written a C++ Class instead but would like to get a hang of C-style OOP.
When activating half bridge mode on the SparkFun MonsterMoto Shield:
- remove R2 and short pads,
- short InA and InB,
- short OutA and OutB;
- Remove any header pins on InB and Out B.
-
Make sure the files in this repository are located in your Arduino library folder or your Arduino sketch folder.
-
Define your pins in the Arduino Sketch. For example:
// Sparkfun MonsterMoto motor driver pins
#define MD_A_EN A0
#define MD_A_CS A2
#define MD_A_INA 7
#define MD_A_INB 8 // put a placeholder value i.e. 255 when using halfbridge mode
#define MD_A_PWM 5
// and if you are also using the other VNH2SP30 driver chip
#define MD_B_EN A1
#define MD_B_CS A3
#define MD_B_INA 4
#define MD_B_INB 9 // put a placeholder value i.e. 255 when using halfbridge mode
#define MD_B_PWM 6
- Create instances of the motorDriver "class" in your Arduino file.
MotorDriver* motorDriver_A = MotorDriver__create( MD_A_EN, MD_A_CS, MD_A_INA,
MD_A_INB, MD_A_PWM, FULLBRIDGE );
MotorDriver* motorDriver_B = MotorDriver__create( MD_B_EN, MD_B_CS, MD_B_INA,
MD_B_INB, MD_B_PWM, FULLBRIDGE );
Check the C implementation file for more details. But in summary:
- Change bridge mode - YOU HAVE TO FIGURE OUT SOME WAY TO UNDO THE HALF-BRIDGE MODIFICATIONS IN SITU
void MotorDriver__changeBridgeMode( MotorDriver* self, DriverConfiguration mode );
- Check if motor driver is faulty
bool MotorDriver__motorDriverIsFaulty( MotorDriver* self );
- Check motor current draw
unsigned int MotorDriver__checkMotorCurrentDraw( MotorDriver* self );
- Turn off motor
void MotorDriver__turnOffMotor( MotorDriver* self );
- Turn on motor
bool MotorDriver__turnOnMotor( MotorDriver* self, MotorState state );
- Get motor state
MotorState MotorDriver__getMotorState( MotorDriver* self );