I use some L298N Ic to drive DC motors ( They are dual H bridge drivers ie, can control 2 DC motors each )
Here is a snippet of code that works for them :, note that I have defined onboard_led_pin as pin 13, and the in_1,in_2 and enable pins as whatever they were connected to ( in_1 and in_2 are digital outputs, and enable is a pin with PWM should I choose to do speed control.
If you want the motor to do something, you call " motor_drive(DIR1,255) " or " motor_drive(1,255) " which will drive tho motor at full power n direction 1
Do you have a schematic of your setup so its easier to debug?
#define DIR1 1 // motor direction 1
#define DIR2 2 // motor direction 2
#define OFF 3 // motor off
void motor_drive (int dir, int power) // Direct hardware interface
{
switch(dir) // Determine motor driving according to passed values
{
case DIR1: // Direction 1
digitalWrite(in_1,HIGH); // Motor turns in Direction 1 at a set power (PWM on enable line)
digitalWrite(in_2,LOW);
analogWrite(enable,power);
digitalWrite(onboard_LED_pin,HIGH);
break;
case DIR2: // Direction 2
digitalWrite(in_1,LOW); // Motor turns in Direction 2 at a set power (PWM on enable line)
digitalWrite(in_2,HIGH);
analogWrite(enable,power);
digitalWrite(onboard_LED_pin,HIGH);
break;
case OFF:
digitalWrite(in_1,LOW); // Motor lines are pulled low, and enable set low
digitalWrite(in_2,LOW);
digitalWrite(enable,LOW);
digitalWrite(onboard_LED_pin,HIGH);
break;
default:
digitalWrite(in_1,LOW); // Motor lines are pulled low, and enable set low
digitalWrite(in_2,LOW);
digitalWrite(enable,LOW);
digitalWrite(onboard_LED_pin,HIGH);
break;
}
digitalWrite(onboard_LED_pin,LOW);
}