CAROBOT Motor and Servo Shield (L298N) Example Code

Controlling the Motor Shield is super-easy. If you can blink LEDs, you can make the Motor Shield spin its motors. Here, we’ll provide a simple, expandable example sketch to show how to drive the pair of motors on the Motor Shield.

If you haven’t assembled the motor shield yet, you should do that first. The assembly guide can be found here.

/*
* 30/09/2015
* Jack Lau
* CAROBOT
*
* This is an example code on how to use the CAROBOT Motor
* and Servo Shield.
*
* This code ia an adaptation of the SparkFun Ardumoto code.
*/

const int pwm_a = 3;
const int pwm_b = 11;
const int dir_a = 12;
const int dir_b = 13;

void setup() {

pinMode(pwm_a, OUTPUT);  //Set control pins to be outputs
pinMode(pwm_b, OUTPUT);
pinMode(dir_a, OUTPUT);
pinMode(dir_b, OUTPUT);

analogWrite(pwm_a, 0);  //set both motors to not move
analogWrite(pwm_b, 0);

}

void loop() {
  forw();         //Set Motors to go forward
  //Note : No pwm is defined with the for function,
  //so that fade in and out works
  fadein();       //fade in from 0-255
  delay(1000);
  forward();      //continue full speed forward
  delay(1000);
  fadeout();      //Fade out from 255-0
  delay(1000);    //Wait one second

  stopped();      // stop for 2 seconds
  delay(2000);

  back();         //Set motors to revers.
  //Note : No pwm is defined with the back function,
  // so that fade in and out works
  fadein();       //fade in from 0-255
  delay(1000);
  backward();     //full speed backward
  delay(1000);
  fadeout();      //Fade out from 255-0
  delay(1000);
}

/* Let's take a moment to talk about these functions.
The forw and back functions are simply designating
the direction the motors will turn once they are fed
a PWM signal.
If you only call the forw, or back functions, you
will not see the motors turn. On a similar note the
fade in and out functions will only change PWM, so
you need to consider the direction you were last set
to. In the code above, you might have noticed that I
called forw and fade in the same grouping. You will
want to call a new direction, and then declare your
pwm fade. There is also a stop function.
*/

void forw() { // no pwm defined
  //Reverse motor direction, 1 high, 2 low
  digitalWrite(dir_a, HIGH);
  //Reverse motor direction, 3 low, 4 high
  digitalWrite(dir_b, HIGH);
}

void back() { // no pwm defined
  //Set motor direction, 1 low, 2 high
  digitalWrite(dir_a, LOW);
  //Set motor direction, 3 high, 4 low
  digitalWrite(dir_b, LOW);
}

void forward() { //full speed forward
  //Reverse motor direction, 1 high, 2 low
  digitalWrite(dir_a, HIGH);
  //Reverse motor direction, 3 low, 4 high
  digitalWrite(dir_b, HIGH);
  //set both motors to run at (100/255 = 39)% duty cycle
  analogWrite(pwm_a, 255);
  analogWrite(pwm_b, 255);
}

void backward() { //full speed backward
  //Set motor direction, 1 low, 2 high
  digitalWrite(dir_a, LOW);
  //Set motor direction, 3 high, 4 low
  digitalWrite(dir_b, LOW);
  //set both motors to run at 100% duty cycle (fast)
  analogWrite(pwm_a, 255);
  analogWrite(pwm_b, 255);
}

void stopped() { //stop
  //Set motor direction, 1 low, 2 high
  digitalWrite(dir_a, LOW);
  //Set motor direction, 3 high, 4 low
  digitalWrite(dir_b, LOW);
  //set both motors to run at 0% duty cycle (stop)
  analogWrite(pwm_a, 0);
  analogWrite(pwm_b, 0);
}

void fadein() {
  // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    // sets the value (range from 0 to 255):
    analogWrite(pwm_a, fadeValue);
    analogWrite(pwm_b, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }
}

void fadeout() {
  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    // sets the value (range from 0 to 255):
    analogWrite(pwm_a, fadeValue);
    analogWrite(pwm_b, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }
}

//stop motor A
void astop() {
  //set both motors to run at 0% duty cycle (stop)
  analogWrite(pwm_a, 0);
}

//stop motor B
void bstop() {
  //set both motors to run at 0% duty cycle (stop)
  analogWrite(pwm_b, 0);
}

Leave a Reply

Your email address will not be published. Required fields are marked *