A SMART CAR USING THE INVENTONE BOARD AND L298N DRIVER

YEAH! So actually we will be doing something very interesting and like the topic implies it's building of a mini car that can sense obstacles and avoids ,does this makes it smart? ikr! let's get started guys

components

  • Arduino IDE  
  • Arduino car kits
  • L298N driver(The L298N is a dual H-Bridge motor driver which allows speed and direction control of two DC motors at the same time.)
  • invent one board   https://inventone.ng
  • jump wires(you'll be needing quite a lot)
  • multi meter(optional)
  • ultrasonic sensor
  • servo motor
  • Tyre wheels(4)
  • batteries(12v supply)
  • Breadboard(optional)


Note: it is advisable to check the continuity of the jump wires to make debugging easy and if you are new to this you can check the components above online

THEORY


I'm sure most hardware guys are not fans of theory (I'm not too), so don't worry i won't be spending much time on this it bores me a lot, shall we speed through this? alright! let's go.

The idea that triggers this operation is simply easy and i will break this down to the simplest way a baby should understand . The L298N driver i.e the motor driver rotates(moving the whole system) the DC motor(wheel) at a speed, when there is an obstacle in front of the rolling wheels the ultrasonic sensor senses it at a particular programmed distance then triggers the servo motor to move(allowing the ultrasonic sensor look elsewhere)i.e to the left or to the right,making the wheel move in a direction whose distance is greater than the programmed distance and  it continues this until the power source is disconnected, enough of this boring stuff and let's get straight to business!

SYSTEM BUILD UP
First of all, let's get our connections done and then program the system. The picture below can be used as a guideline  and also note that your work can be a little bit untidy so you need be careful.

  • Pair the 4 DC motors (i.e attach them to form 2 pairs) and label them A and B
  • Connect the rubber tyre wheel to the paired motor
  • Connect the DC motors to the L298N driver (The driver module has two screw terminal blocks for the motor A and B, and another screw terminal block for the Ground pin, the 12V for motor and a 5V pin which can either be an input or output.)
  • Connect the positive terminal of the power source to the 12V pin and likewise the negative terminal to the ground pin,then connect the 5V pin to the 5V on the inventone board
  • connect the servo motor pins(i.e pulse, VCC, GND ) to their equivalents on the board.
  • connect the ultrasonic sensor pins(VCC ,trig,echo,GND) to their equvalents on the board. 

Your car should be ready by now but not yet smart so all you need do now is upload your sketch(arduino code) into the micro-controller i.e inventone board , which can be seen below.

There are two sketches to run simultaneously, and this can be achieved by opening a new tab for the second sketch.

The first sketch:

#include <Servo.h>
#include <hcsr04.h>
#include <Drive.h>

#define ECHO 13 //D7
#define TRIGGER 12 //D8
#define SERVO 4 //D6
#define MAX_DISTANCE 150

const int IN1 = 2; //D4/SDA
const int IN2 = 14; //D3/SCK
const int IN3 = 15; //D1
const int IN4 = 0; //D2

Servo myservo;
Drive drive(IN1, IN2, IN3, IN4);
HCSR04 hcsr04(TRIGGER,ECHO);

int rightDistance;
int leftDistance;

void setup() {
Serial.begin(9600);
myservo.attach(SERVO);
}

void loop() {
Serial.println("find distance");

while(findDistance () >= 30){
drive.moveForward(500);
Serial.println("Moving forward");
delay(100);
}

drive.stopMoving();
Serial.println("Stopped moving");

rightDistance = lookRight();
leftDistance = lookLeft();

if (leftDistance > rightDistance){
drive.turnLeft(500);
Serial.println("Turning left");
delay(500);
drive.stopMoving();
}

else if (rightDistance > leftDistance) {
drive.turnRight(500);
Serial.println("Turning right");
delay(500);
drive.stopMoving();
}
else {
drive.moveBackward(500);
delay(500);
drive.stopMoving();
}

delay(1000);
}

 The second sketch:

int lookRight() {

myservo.write(0);

delay(1000);

int distance = findDistance();

return distance;

}

 

int lookLeft() {

myservo.write(180);

delay(1000);

int distance = findDistance();

return distance;

}

 

int lookForward() {

myservo.write(90);

delay(1000);

int distance = findDistance();

return distance;

}

 

int findDistance(){

int distance = hcsr04.distanceInMillimeters() / 10;

if(distance >= MAX_DISTANCE){

return MAX_DISTANCE;

}

else{

return distance;

}

}

With your sketch uploaded, your smart car should be able to perform its programmed operation i.e senses obstacles and changes direction.

That concludes our tutorial. If you have any issues, questions or you want me to know your view about this tutorial you can leave them in the comment section below. Thanks for reading, and please do /* n't *\ try this at home!