You are on page 1of 5

/*

HG7881_Motor_Driver_Example - Arduino sketch

This example shows how to drive a motor with using HG7881 (L9110) Dual
Channel Motor Driver Module. For simplicity, this example shows how to
drive a single motor. Both channels work the same way.

This example is meant to illustrate how to operate the motor driver


and is not intended to be elegant, efficient or useful.

Connections:

Arduino digital output D10 to motor driver input B-IA.


Arduino digital output D11 to motor driver input B-IB.
Motor driver VCC to operating voltage 5V.
Motor driver GND to common ground.
Motor driver MOTOR B screw terminals to a small motor.

Related Banana Robotics items:

BR010038 HG7881 (L9110) Dual Channel Motor Driver Module

https://www.BananaRobotics.com/shop/HG7881-(L9110)-Dual-Channel-Motor-Driver-Modul
e

https://www.BananaRobotics.com
*/

// wired connections
#define HG7881_B_IA 10 // D10 --> Motor B Input A --> MOTOR B +
#define HG7881_B_IB 11 // D11 --> Motor B Input B --> MOTOR B -
#define HG7881_A_IA 6
#define HG7881_A_IB 7
// functional connections
#define MOTOR_B_PWM HG7881_B_IA // Motor B PWM Speed
#define MOTOR_B_DIR HG7881_B_IB // Motor B Direction
#define MOTOR_A_PWM HG7881_A_IA
#define MOTOR_A_DIR HG7881_A_IB

// the actual values for "fast" and "slow" depend on the motor
#define PWM_SLOW 150 // arbitrary slow speed PWM duty cycle
#define PWM_FAST 200 // arbitrary fast speed PWM duty cycle
#define DIR_DELAY 1000 // brief delay for abrupt motor changes

void setup()
{
Serial.begin( 9600 );
pinMode( MOTOR_B_DIR, OUTPUT );
pinMode( MOTOR_B_PWM, OUTPUT );
pinMode( MOTOR_A_DIR, OUTPUT );
pinMode( MOTOR_A_PWM, OUTPUT );
digitalWrite( MOTOR_B_DIR, LOW );
digitalWrite( MOTOR_B_PWM, LOW );
digitalWrite( MOTOR_A_DIR, LOW );
digitalWrite( MOTOR_A_PWM, LOW );
Serial.println( "Fast fcccccccorward..." );
}
void loop()
{
boolean isValidInput;
//check whether arduino is reciving signal or not
while(Serial.available() == 0);
char val = Serial.read() ;//reads the signal
//Serial.println(val);
switch(val)

{
case 1: // 1) Fast forward
// Serial.println( "Fast forward..." );
// always stop motors briefly before abrupt changes
digitalWrite( MOTOR_B_DIR, LOW );
digitalWrite( MOTOR_B_PWM, LOW );
digitalWrite( MOTOR_A_DIR, LOW);
digitalWrite( MOTOR_A_PWM, LOW);
//delay( DIR_DELAY );
// set the motor speed and direction
digitalWrite( MOTOR_B_DIR, HIGH ); // direction = forward
analogWrite( MOTOR_B_PWM, 255-PWM_FAST ); // PWM speed = fast
digitalWrite( MOTOR_A_DIR, HIGH );
analogWrite( MOTOR_A_PWM, 255-PWM_FAST );
isValidInput = true;
break;

case 3: // 2) Gir esquerra


//Serial.println( "Forward..." );
// always stop motors briefly before abrupt changes
digitalWrite( MOTOR_B_DIR, LOW );
digitalWrite( MOTOR_B_PWM, LOW );
digitalWrite( MOTOR_A_DIR, HIGH);
digitalWrite( MOTOR_A_PWM, HIGH);
// delay( DIR_DELAY );
// set the motor speed and direction
digitalWrite( MOTOR_A_DIR, HIGH );
analogWrite( MOTOR_A_PWM, 255-PWM_SLOW );
isValidInput = true;
break;

case 2: // 2) Gir 2 dreta


//Serial.println( "Forward..." );
// always stop motors briefly before abrupt changes
digitalWrite( MOTOR_B_DIR, HIGH );
digitalWrite( MOTOR_B_PWM, HIGH );
digitalWrite( MOTOR_A_DIR, LOW);
digitalWrite( MOTOR_A_PWM, LOW);
// delay( DIR_DELAY );
// set the motor speed and direction
digitalWrite( MOTOR_B_DIR, HIGH ); // direction = forward
analogWrite( MOTOR_B_PWM, 255-PWM_SLOW ); // PWM speed = slow
isValidInput = true;
break;

case 10: // 1) gir amunt


// Serial.println( "Fast forward..." );
// always stop motors briefly before abrupt changes
digitalWrite( MOTOR_B_DIR, LOW );
digitalWrite( MOTOR_B_PWM, LOW );
digitalWrite( MOTOR_A_DIR, LOW);
digitalWrite( MOTOR_A_PWM, LOW);
//delay( DIR_DELAY );
// set the motor speed and direction
digitalWrite( MOTOR_B_DIR, HIGH ); // direction = forward
analogWrite( MOTOR_B_PWM, 140); // PWM speed = fast
digitalWrite( MOTOR_A_DIR, HIGH );
analogWrite( MOTOR_A_PWM, 70 );
isValidInput = true;
break;

case 11: // 1) gir amunt 2


// Serial.println( "Fast forward..." );
// always stop motors briefly before abrupt changes
digitalWrite( MOTOR_B_DIR, LOW );
digitalWrite( MOTOR_B_PWM, LOW );
digitalWrite( MOTOR_A_DIR, LOW);
digitalWrite( MOTOR_A_PWM, LOW);
//delay( DIR_DELAY );
// set the motor speed and direction
digitalWrite( MOTOR_B_DIR, HIGH ); // direction = forward
analogWrite( MOTOR_B_PWM, 70 ); // PWM speed = fast
digitalWrite( MOTOR_A_DIR, HIGH );
analogWrite( MOTOR_A_PWM, 140 );
isValidInput = true;
break;

case 12:
// 1) gir contrari
// Serial.println( "Fast forward..." );
// always stop motors briefly before abrupt changes
digitalWrite( MOTOR_B_DIR, HIGH );
digitalWrite( MOTOR_B_PWM, HIGH );
digitalWrite( MOTOR_A_DIR, HIGH);
digitalWrite( MOTOR_A_PWM, HIGH);
//delay( DIR_DELAY );
// set the motor speed and direction
digitalWrite( MOTOR_B_DIR, LOW ); // direction = forward
analogWrite( MOTOR_B_PWM, 115 ); // PWM speed = fast
digitalWrite( MOTOR_A_DIR, LOW );
analogWrite( MOTOR_A_PWM, 185 );
isValidInput = true;
break;

case 13: // 1) gir contrari 2


// Serial.println( "Fast forward..." );
// always stop motors briefly before abrupt changes
digitalWrite( MOTOR_B_DIR, HIGH );
digitalWrite( MOTOR_B_PWM, HIGH );
digitalWrite( MOTOR_A_DIR, HIGH);
digitalWrite( MOTOR_A_PWM, HIGH);
//delay( DIR_DELAY );
// set the motor speed and direction
digitalWrite( MOTOR_B_DIR, LOW ); // direction = forward
analogWrite( MOTOR_B_PWM, 185 ); // PWM speed = fast
digitalWrite( MOTOR_A_DIR, LOW );
analogWrite( MOTOR_A_PWM, 115 );
isValidInput = true;
break;

case 4: // 4) Reverse
//Serial.println( "Fast forward..." );
// always stop motors briefly before abrupt changes
digitalWrite( MOTOR_B_DIR, LOW );
digitalWrite( MOTOR_B_PWM, LOW );
digitalWrite( MOTOR_A_DIR, LOW);
digitalWrite( MOTOR_A_PWM, LOW);
//delay( DIR_DELAY );
// set the motor speed and direction
digitalWrite( MOTOR_B_DIR, LOW ); // direction = reverse
analogWrite( MOTOR_B_PWM, PWM_SLOW ); // PWM speed = slow
digitalWrite( MOTOR_A_DIR, LOW );
analogWrite( MOTOR_A_PWM, PWM_SLOW );
isValidInput = true;
break;

case 6: // 6) Hard stop (use with caution)


//Serial.println( "Hard stop (brake)..." );
digitalWrite( MOTOR_B_DIR, HIGH );
digitalWrite( MOTOR_B_PWM, HIGH );
digitalWrite( MOTOR_A_DIR, HIGH );
digitalWrite( MOTOR_A_PWM, HIGH );
break;

default:
// wrong character! display the menu again!

break;
}

// repeat the main loop and redraw the menu...


}
/*EOF*/

You might also like