loading

This year i participated in a workshop and needed to submit a project. Because i was short on time, the project was a rc car controlled by an arduino. The car uses an ultrasonic sensor to calculate the distance to the objects in front. If was detected an object, the car stop and changes the direction.

Step 1: What You Need

Step 2: Prepare the RC Car

First i need to remove the rc car controller. This controller was connected to the motors, the batteries and the wireless receiver.

Step 3: Connect the Motors

The L298 has twelve connectors that we need to use. On one side are the connectors (blue connectors) to the motors an the power. Two control one motor, other two control the other motor and the final two power the module.

On the other side (yellow connectors) two enable and disable the motors, two control the rotation of the motor A and the other two control the rotation of the motor B,

Step 4: Connect the Arduino

The L298 is connected to the arduino following way:

Pin enableA (L298) -> 12 (Arduino)

Pin enableB (L298) -> 9 (Arduino)

Pin A1 (L298) -> 11 (Arduino)

Pin A2 (L298) -> 10 (Arduino)

Pin B1 (L298) -> 8 (Arduino)

Pin B2 (L298) -> 7 (Arduino)

Step 5: Connect the HC-SR04

The HC-SR04 uses four pins to connect to the arduino. Two are for the power, one is Trigger, to emit a signal to calculate the distance, and the other is Echo, to receive the signal. The connections to the arduino are the following:

Pin VCC (HC-SR04) -> 5V (Arduino)

Pin Ground (HC-SR04) -> Ground (Arduino)

Pin Echo (HC-SR04) -> 3 (Arduino)

Pin Trigger (HC-SR04) -> 2 (Arduino)

Step 6: Arduino Sketch

This is the Arduino sketch used to control the car.

/**
 Tiago Santos, 2014
 dark_storm@groundzero.com.pt
 http://space.groundzero.com.pt
 Code to control a RC Car
 Thanks to Bruno Santos (feiticeir0@whatgeek.com.pt) for some code
 Free to share
 **/
//Pinouts
//Ultrasound Sensor
#define trigPin 2 // Trigger  2
#define echoPin 3 //echo      3


//Motor A
int enableA = 12;  //12
int pinA1 = 11;    //11
int pinA2 = 10;    //10

///Motor B
int enableB = 9;
int pinB1 = 8;    //8
int pinB2 = 7;  //7


//Variables
long duration;
int distance;
<p>int r;</p>
void setup()
{
 Serial.begin(9600);
 /* Initialize Ultrasound Pin Modes */
 pinMode(trigPin, OUTPUT); // Trigger PIN
 pinMode(echoPin, INPUT); // Echo PIN
  
 /* Initialize Motor A Pin Modes */
  pinMode (enableA, OUTPUT);
  pinMode (pinA1, OUTPUT);
  pinMode (pinA2, OUTPUT);  
  
 /* Initialize Motor B Pin Modes */  
  pinMode (enableB, OUTPUT);
  pinMode (pinB1, OUTPUT);
  pinMode (pinB2, OUTPUT);  
}
void loop()
{
  //check the distance
  distance = checkDistance(); 
  
  Serial.println(distance);
  delay(200);
  //if distance is less that 30 cm
  if(distance < 30)
  {
    Serial.println("menor");
    //stop car, turn wheels right and go back for 500 ms. Then stop
    stopMotorA();
    //stopMotorB();
    r = random(1);
    if( r == 1)
    {
      turnRight();
    }
    else
    {
      turnLeft();
    }
    delay(100);
    goBack();
    delay(500);
    stopMotorB();
    stopMotorA();
    if( r == 1)
    {
      turnLeft();
    }
    else
    {
      turnRight();
    }
    delay(100);
    stopMotorB();
  }
  else
  {
    Serial.println("maior");
    stopMotorA();
    stopMotorB();
    goForward();
  }
  
}
//function to calcutate the distance to an obstacle
long microsecondsToCentimeters (long microseconds) {
 // The speed of sound is 340 m/s or 29 microseconds per centimeter
 // The ping travels forth and back, so, the distance is half the distance traveled
 return microseconds / 29 / 2;
}
//function to check the distance of an obstacle
long checkDistance()
{
 /* The following trigPin/echoPin cycle is used to determine the distance of the nearest
 object by bouncing soundwaves off it */
 //Trigger a HIGH pulse for 2 or more microseconds
 //Give a short LOW pulse before sending a HIGH one
 
 digitalWrite (trigPin, LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite (trigPin, LOW);
 //Now, lets read the read the bounced wave
 duration = pulseIn(echoPin, HIGH);
 //calculate the distance
 distance = microsecondsToCentimeters(duration);
 
 return distance;
}
//function to go forward
void goForward()
{
  //Enable Motor A
  digitalWrite (enableA, HIGH);
  //forward
  digitalWrite (pinA1, LOW);
  digitalWrite (pinA2, HIGH);
}
//function to stop Motor A movement
void stopMotorA()
{
  digitalWrite (enableA, LOW);
}
//function to go back
void goBack()
{
  //Enable Motor A
  digitalWrite (enableA, HIGH);  
  //back
  digitalWrite (pinA1,HIGH);
  digitalWrite (pinA2,LOW);  
}
//function to stop Motor B movement
void stopMotorB()
{
  digitalWrite (enableB, LOW);
}
//function to turn the RC car right
void turnRight()
{
  //Enable Motor B  
  digitalWrite (enableB, HIGH);
  //right  
  digitalWrite (pinB1, LOW);
  digitalWrite (pinB2, HIGH);  
}
//function to turn the RC car left
void turnLeft()
{
  //Enable Motor B  
  digitalWrite (enableB, HIGH);  
  //left
  digitalWrite (pinB1,HIGH);
  digitalWrite (pinB2,LOW);  
}

Step 7: Power

Because the low amperage of the 9v battery, was needed a second battery holder for the motors.The L298 is powered by 5v, but because the motors need a constant and stable power source, i used four 1.5v battery, is a total of 6v.

Step 8: Final Result

<p>Hi Nice project</p><p>We have tried this with L298N Motor Driver Shield but it is not powering the DC motor, what could have gone wrong, can you give us the connection diagram.</p><p>Thanks</p>
<p>Hi. The L298N needs to have is own power source to power the motors. I had a 4x1.5v battery pack connected to 5v and ground pins on the L298N (picture on step 5). I don't have a diagram. </p>
<p>movie pleace and results and some infopleace</p><p>this is not a full IBLE</p><p>but verry cool so i want to make it.</p>
<p>Hello. </p><p>There isn't a movie because some parts were needed for other projects and i need to disassemble the car before have time to make the movie.</p><p>What other info do you need?</p>

About This Instructable

2,812views

30favorites

License:

Bio: https://about.me/tiago.santos
More by TiagoSantos:Prusa I3 Enclosure Marvin Night Light BQ Ciclop Home Made 
Add instructable to: