Robot Car using L298N & InventOne

Introduction

Hello there! Are you in for another awesome DIY project? Here we would be building a robot car using our very own inventone dev board and L298N motor driver. This tutorial takes you through the hardware and software intricacies of controlling two brushed dc motors using the inventone board.

 

List of Components

Battery

L298N-Bridge breakout board

Two brushed DC motors

InventOne dev board

Jumper wires

 

You can order all your components online from aliexpress, hub360, or ebay . To get the inventone board, click this order link.

 

Tutorial

L298N bridge breakout board allows us to control two dc motors at the same time, it also gives us the ability to choose what direction we want our motors to turn. For more info about the L298N bridge see this tutorial: Arduino DC Motor Control Tutorial

We’ve added a picture of the circuit connection which we designed using fritzing, while implementing this project we used three lithium 3.7V batteries to provide longer power. The connection below would suffice but for better performance we suggest you get lithium batteries and connect them in series.

Note also that the L298N Bridge requires a minimum of 12V as power, and you need to connect the enable pin to about 5V for the L298N to respond to the signals sent from the inventone board. Just make sure you go through the tutorial above to get more understanding of the L298N bridge.

 

Circuit diagram for controlling 2 dc motors using the inventone dev board.
Circuit diagram for L298N motor driver & InventOne board

 

Implement the circuit above taking note of the pin numbers, you can also get the 5V required to power your inventone board from the L298N bridge board. Don't forget to connect the ground from the bridge to the inventone board.

 

Code

Basically, we have four output pins which we would be using to drive the dc motors. We would be creating two demos; DemoOne drives the two rotors for 2 seconds by writing a high voltage level on one of the legs of each rotor and then a low to the other leg. Next we turn of the motors for half a second, finally we drive the motors in the other direction by changing the polarities of the voltage in which we write to the motors.

Demotwo does about the same thing except that we have a for loop which we would be using to increase or decrease the speed of the motors gradually from zero to its maximum speed and from its max speed back to zero. Finally, in the loop we would run each of the demos for about 1 second.

 

/*This code provides a demo for L298N motor driver using the Inventone dev. board*/
//Pin connections for motor A
int in1 = 12;
int in2 = 16;
//for motor B
int in3 = 2;// SDA is pin 2 , SCK is pin 14
int in4 = 14;

void setup() {
  // set all the motor control pins to outputs
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
}

void demoOne() {
  // this function will run the motors in both directions    //at a fixed speed
  // turn on motor A
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  // turn on motor B
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  delay(2000);
  / now turn off motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  delay(500);
  // now change motor directions
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  delay(2000);
}
void demoTwo() {
  // this function will run the motors across the range      //of possible speeds
  // turn on motors
  // accelerate from zero to maximum speed
  digitalWrite(in1, LOW);
  digitalWrite(in3, LOW);
  for (int i = 0; i < 1023; i++)
  {
  analogWrite(in2, i);
  analogWrite(in4, i);
  delay(20);
  }
  // decelerate from maximum speed to zero
  for (int i = 1023; i >= 0; --i)
  {
  analogWrite(in2, i);
  analogWrite(in4, i);
  delay(20);
  }
  // now turn off motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  }
void loop()
{
  demoOne();
  delay(1000);
  demoTwo();
  delay(1000);
}