Smart Robot Car

The Elegoo Smart Robot Car is an impressive creation that combines various solutions to address real-world problems effectively. The Elegoo Smart Car stands out with its array of advanced features, including:

These features collectively make the Elegoo Smart Car a versatile and powerful tool, suitable for educational purposes, hobby projects, and practical applications where automation and intelligent navigation are essential.

Assembling the Smart Car

Assembling the Elegoo Smart Robot Car is straightforward, and the detailed assembly procedure can be accessed on the Elegoo website. Simply visit (https://www.elegoo.com) and select "Elegoo Smart Robot Car Kit V3.0 Plus." Additionally, the assembly instructions from Elegoo are provided here for your convenience.

When you open the Smart Car kit, you'll find all the hardware organized in designated plastic bags. Follow the instructions provided on the Elegoo website and enjoy the assembly process. However, here is one important piece of advice: DO NOT assemble the top and bottom "Base Plates" together as shown on page 16 of the assembly manual. The illustration on page 16 can be misleading, making it appear that the Plates should be assembled at step 16. In reality, these Plates should be assembled at step 19, after all the electrical wires are connected to the motor driver, which is mounted onto the bottom Plate. Aside from this detail, the assembly process is quite easy and intuitive.

Parts List

- DC motors

- Acrylic Chassis

- L298N motor driver

- Arduino UNO

- Sensor shield

- SG90 Micro Servo

- Ultrasonic sensor & holder

- Line tracking module

- Bluetooth module

- Infrared receiver

- Rechargeable Battery

- Wheels

Elegoo's Smart Car Parts

Post-Assembly Instructions

Once the Smart Car is fully assembled, the next step is to test all its functions and operations. The Smart Car comes equipped with the "SmartCar Multi functions" software, allowing it to operate in four modes:

All the necessary codes for these basic functions are provided by Elegoo and can be downloaded from their website. The downloaded zip file includes six sections for tutorial purposes. We will start with the  "AUTO_GO.ino" sketch, which moves the car in all directions. 


Smart Robot Car – Initial Test

The Smart Robot Car has the following functions:


Tips for Uploading Codes

When uploading codes to the Elegoo Smart Robot Car, follow this crucial tip to avoid potential issues:


Remove the Bluetooth Module: Before uploading any codes, remove the Bluetooth module from the Arduino or the IO expansion board (if there is one). You can remount the Bluetooth module after the upload process is complete.

 Steps for Testing and Operation

1. Download the Code: Go to the [Elegoo download page](https://www.elegoo.com/download/) and select the "Elegoo Smart Robot Car Kit V3.0 Plus" to download the necessary files.

2. Extract the Zip File: Once downloaded, extract the zip file to a convenient location on your computer. This file contains all the necessary codes and tutorials.

3. Install Arduino IDE: If you haven't already, download and install the Arduino IDE from the [Arduino website](https://www.arduino.cc/en/software).

4. Open AUTO_GO.ino Sketch:

5. Connect the Arduino UNO:

6. Upload the Code:

    - Click the upload button in the Arduino IDE to compile and upload the "AUTO_GO.ino" sketch to the Arduino UNO.

7. Test the Car:

By following these steps, you can ensure that your Elegoo Smart Robot Car is fully functional and ready for operation in its various modes.

We will make the car move automatically by following this sequence: move forward for 0.4 seconds, back up for 0.4 seconds, turn left for 0.4 seconds, and turn right for 0.4 seconds. First, let's review the motor and L298N board connections. We'll use Arduino pins 5, 6, 7, 8, 9, and 11 to control the car through the motor controller. Pins 9 and 11 control the right wheel, while pins 7 and 8 control the left wheel. Pins 5 and 6 are used to control ENA and ENB, respectively.

After uploading the "Auto Go" sketch to the Arduino UNO, place the Smart Car on a level surface. Turn on the battery switch and observe the test. The Smart Car will move forward for one second, reverse for one second, turn left for one second, and then turn right for one second. This sequence repeats indefinitely. The "Auto Go" test verifies the operation of the motors and the overall performance of the Smart Car.

AUTO_GO.ino 

//www.elegoo.com


//    The direction of the car's movement

//  ENA   ENB   IN1   IN2   IN3   IN4   Description  

//  HIGH  HIGH  HIGH  LOW   LOW   HIGH  Car is runing forward

//  HIGH  HIGH  LOW   HIGH  HIGH  LOW   Car is runing back

//  HIGH  HIGH  LOW   HIGH  LOW   HIGH  Car is turning left

//  HIGH  HIGH  HIGH  LOW   HIGH  LOW   Car is turning right

//  HIGH  HIGH  LOW   LOW   LOW   LOW   Car is stoped

//  HIGH  HIGH  HIGH  HIGH  HIGH  HIGH  Car is stoped

//  LOW   LOW   N/A   N/A   N/A   N/A   Car is stoped



//define L298n module IO Pin

#define ENA 5

#define ENB 6

#define IN1 7

#define IN2 8

#define IN3 9

#define IN4 11


void forward(){ 

  digitalWrite(ENA,HIGH); //enable L298n A channel

  digitalWrite(ENB,HIGH); //enable L298n B channel

  digitalWrite(IN1,HIGH); //set IN1 hight level

  digitalWrite(IN2,LOW);  //set IN2 low level

  digitalWrite(IN3,LOW);  //set IN3 low level

  digitalWrite(IN4,HIGH); //set IN4 hight level

  Serial.println("Forward");//send message to serial monitor

}


void back(){

  digitalWrite(ENA,HIGH);

  digitalWrite(ENB,HIGH);

  digitalWrite(IN1,LOW);

  digitalWrite(IN2,HIGH);

  digitalWrite(IN3,HIGH);

  digitalWrite(IN4,LOW);

  Serial.println("Back");

}


void left(){

  digitalWrite(ENA,HIGH);

  digitalWrite(ENB,HIGH);

  digitalWrite(IN1,LOW);

  digitalWrite(IN2,HIGH);

  digitalWrite(IN3,LOW);

  digitalWrite(IN4,HIGH); 

  Serial.println("Left");

}


void right(){

  digitalWrite(ENA,HIGH);

  digitalWrite(ENB,HIGH);

  digitalWrite(IN1,HIGH);

  digitalWrite(IN2,LOW);

  digitalWrite(IN3,HIGH);

  digitalWrite(IN4,LOW);

  Serial.println("Right");

}


//before execute loop() function, 

//setup() function will execute first and only execute once

void setup() {

  Serial.begin(9600);//open serial and set the baudrate

  pinMode(IN1,OUTPUT);//before useing io pin, pin mode must be set first 

  pinMode(IN2,OUTPUT);

  pinMode(IN3,OUTPUT);

  pinMode(IN4,OUTPUT);

  pinMode(ENA,OUTPUT);

  pinMode(ENB,OUTPUT);

}


//Repeat execution

void loop() {

  forward();  //go forward

  delay(1000);//delay 1000 ms

  back();     //go back

  delay(1000);

  left();     //turning left

  delay(1000);

  right();    //turning right

  delay(1000);

}


2. Bluetooth Mode

After verifying the assembly and motor operation of the Smart Car using the "Auto Go" sketch, you can proceed to test the Bluetooth functionality using the "Bluetooth Car" sketch from the downloaded files. Follow these steps:

1. Install the Elegoo Bluetooth App: Download and install the "Elegoo BLE Tool" app on your smartphone from the Google Play Store for Android or the App Store for iPhone.

2. Upload the Bluetooth Car Sketch: 

   - Open the "Bluetooth Car" sketch in the Arduino IDE.

   - Remove the Bluetooth module from the IO expansion board before uploading the code.

   - Connect the Arduino UNO to your computer and upload the sketch.

   - Once the upload is complete, remount the Bluetooth module.

3. Pair Your Smartphone:

   - Turn on the Smart Car and ensure the Bluetooth module is powered.

   - Open the "Elegoo BLE Tool" app on your smartphone and pair it with the Smart Car's Bluetooth module.

4. Control the Car:

   - Use the app to control the Smart Car. You should be able to move it forward, backward, left, and right using the controls on your smartphone.

Elegoo BLE Tool 

NOTE: Do not forget to remove the Bluetooth Module from the Smart Car before uploading the code. Once you are done with the upload, put the Bluetooth module back to the Smart Car. This is because the Arduino UNO uses the same serial peripheral port for the USB and the Bluetooth Module. These two serial connections can not be used simultaneously.

This precaution is necessary because the Arduino UNO utilizes the same serial peripheral port for both the USB connection and the Bluetooth Module. Simultaneous use of these two serial connections can lead to conflicts and errors. By following these steps, you can ensure smooth operation of the Smart Car's Bluetooth functionality without encountering any issues during the upload process.

​bluetooth_car.ino 

//www.elegoo.com


#define ENA 5

#define ENB 6

#define IN1 7

#define IN2 8

#define IN3 9

#define IN4 11

#define LED 13


unsigned char carSpeed = 250;

bool state = LOW;


void forward(){ 

  digitalWrite(ENA,HIGH);

  digitalWrite(ENB,HIGH);

  digitalWrite(IN1,HIGH);

  digitalWrite(IN2,LOW);

  digitalWrite(IN3,LOW);

  digitalWrite(IN4,HIGH);

  Serial.println("Forward");

}


void back(){

  digitalWrite(ENA,HIGH);

  digitalWrite(ENB,HIGH);

  digitalWrite(IN1,LOW);

  digitalWrite(IN2,HIGH);

  digitalWrite(IN3,HIGH);

  digitalWrite(IN4,LOW);

  Serial.println("Back");

}


void left(){

  analogWrite(ENA,carSpeed);

  analogWrite(ENB,carSpeed);

  digitalWrite(IN1,LOW);

  digitalWrite(IN2,HIGH);

  digitalWrite(IN3,LOW);

  digitalWrite(IN4,HIGH); 

  Serial.println("Left");

}


void right(){

  analogWrite(ENA,carSpeed);

  analogWrite(ENB,carSpeed);

  digitalWrite(IN1,HIGH);

  digitalWrite(IN2,LOW);

  digitalWrite(IN3,HIGH);

  digitalWrite(IN4,LOW);

  Serial.println("Right");

}


void stop(){

  digitalWrite(ENA,LOW);

  digitalWrite(ENB,LOW);

  Serial.println("Stop!");

}


void stateChange(){

  state = !state;

  digitalWrite(LED, state);

  Serial.println("Light");  

}


void setup() { 

  Serial.begin(9600);

  pinMode(LED, OUTPUT); 

  pinMode(IN1,OUTPUT);

  pinMode(IN2,OUTPUT);

  pinMode(IN3,OUTPUT);

  pinMode(IN4,OUTPUT);

  pinMode(ENA,OUTPUT);

  pinMode(ENB,OUTPUT);

  stop();

}


void loop() { 

  if(Serial.available())

  {

    char getstr = Serial.read();

    switch(getstr){

      case 'f': forward(); break;

      case 'b': back();   break;

      case 'l': left();   break;

      case 'r': right();  break;

      case 's': stop();   break;

      case 'a': stateChange(); break;

      defaultbreak;

    }

  }

}

3. Line Tracking Mode

To utilize the Line Tracking mode of operation for your Smart Car, follow these steps:

1. Prepare the Closed Path:

2. Understand the Line Tracking Module:

3. Upload the Line Tracking Sketch:

4. Position the Smart Car:

5. Use the Elegoo BLE Tool App:

6. Select Line Tracking Mode:

7. Observe Navigation:

By following these steps, you can enjoy observing your Smart Car autonomously navigate along the predefined path in Line Tracking mode.

Line Tracking Mode

Line_tracking_car.ino 

//www.elegoo.com


//Line Tracking IO define

#define LT_R !digitalRead(10)

#define LT_M !digitalRead(4)

#define LT_L !digitalRead(2)


#define ENA 5

#define ENB 6

#define IN1 7

#define IN2 8

#define IN3 9

#define IN4 11


#define carSpeed 250


void forward(){

  analogWrite(ENA, carSpeed);

  analogWrite(ENB, carSpeed);

  digitalWrite(IN1, HIGH);

  digitalWrite(IN2, LOW);

  digitalWrite(IN3, LOW);

  digitalWrite(IN4, HIGH);

  Serial.println("go forward!");

}


void back(){

  analogWrite(ENA, carSpeed);

  analogWrite(ENB, carSpeed);

  digitalWrite(IN1, LOW);

  digitalWrite(IN2, HIGH);

  digitalWrite(IN3, HIGH);

  digitalWrite(IN4, LOW);

  Serial.println("go back!");

}


void left(){

  analogWrite(ENA, carSpeed);

  analogWrite(ENB, carSpeed);

  digitalWrite(IN1, LOW);

  digitalWrite(IN2, HIGH);

  digitalWrite(IN3, LOW);

  digitalWrite(IN4, HIGH);

  Serial.println("go left!");

}


void right(){

  analogWrite(ENA, carSpeed);

  analogWrite(ENB, carSpeed);

  digitalWrite(IN1, HIGH);

  digitalWrite(IN2, LOW);

  digitalWrite(IN3, HIGH);

  digitalWrite(IN4, LOW); 

  Serial.println("go right!");


void stop(){

   digitalWrite(ENA, LOW);

   digitalWrite(ENB, LOW);

   Serial.println("Stop!");


void setup(){

  Serial.begin(9600);

  pinMode(10,INPUT);

  pinMode(4,INPUT);

  pinMode(2,INPUT);

}


void loop() {

  if(LT_M){

    forward();

  }

  else if(LT_R) { 

    right();

    while(LT_R);                             

  }   

  else if(LT_L) {

    left();

    while(LT_L);  

  }

}


4. Obstacle-Avoidance Mode

Elegoo's collision avoidance mode employs the HCSR04 ultrasonic sensor, strategically mounted at the front of the smart car. This ultrasonic sensor is affixed to a servo motor, enabling it to sweep an area of approximately 180 degrees. Utilizing sonar technology, the ultrasonic sensor accurately determines the distance to an object in its path. It boasts an impressive range detection capability, accurately measuring distances from 2cm to 400cm or 1 inch to 13 feet.

The ultrasonic sensor features four pins:

Ultrasonic Sensor

The timing diagram of the HCSR04 ultrasonic sensor is depicted in the figure below. To initiate a measurement, the Trig pin of the SR04 must receive a pulse of high voltage (5V) for a duration of at least 10 microseconds. This pulse triggers the sensor to transmit 8 pulses of ultrasonic burst at a frequency of 40kHz and then waits for the reflected ultrasonic burst. When the sensor detects the ultrasonic signal from the receiver, it sets the Echo pin to high voltage (5V) and delays for a period proportional to the distance based on the object detection.

Ultrasonic Module Timing Diagram 

To measure the distance using the HCSR04 ultrasonic sensor, you can follow these steps:


Time = Width of Echo pulse, in uS (micro second)

- Distance in centimeters = Time / 58

- Distance in inches = Time / 148

- Or you can utilize the speed of sound, which is 340m/s


The ultrasonic module can be tested using Arduino microcontroller and the following sketch function.

int ultrasonic_test(){

  digitalWrite(Trig, LOW);

  delayMicroseconds(2);

  digitalWrite(Trig, HIGH);

  delayMicroseconds(10);

  digitalWrite(Trig, LOW);

  float distance = pulseIn(Echo, HIGH);

  distance = distance / 58;

}

Obstacle Avoidance – Operation

The principle of obstacle or collision avoidance is as simple as “if – else if – else” statement in C++ or any other programing languages. The ultrasonic sensor module will detect the distance between the car and an obstacle in front of it and sending the data to the microcontroller. Then, the microcontroller sends a corrective action to the smart car and this process continues repeatedly.

The algorithm follows the following sequence.

This algorithm works fine as a starting point, but it needs more improvement as you become more familiar with the Smart Car.

Now, we have the basic understanding of the obstacle avoidance system, let us start the fun part.

Obstacle_Avoidance_Car.ino 

#include <Servo.h>  //servo library

Servo myservo;      // create servo object to control servo


int Echo = A4;  

int Trig = A5; 


#define ENA 5

#define ENB 6

#define IN1 7

#define IN2 8

#define IN3 9

#define IN4 11

#define carSpeed 250

int rightDistance = 0, leftDistance = 0, middleDistance = 0;


void forward(){ 

  analogWrite(ENA, carSpeed);

  analogWrite(ENB, carSpeed);

  digitalWrite(IN1, HIGH);

  digitalWrite(IN2, LOW);

  digitalWrite(IN3, LOW);

  digitalWrite(IN4, HIGH);

  Serial.println("Forward");

}


void back() {

  analogWrite(ENA, carSpeed);

  analogWrite(ENB, carSpeed);

  digitalWrite(IN1, LOW);

  digitalWrite(IN2, HIGH);

  digitalWrite(IN3, HIGH);

  digitalWrite(IN4, LOW);

  Serial.println("Back");

}


void left() {

  analogWrite(ENA, carSpeed);

  analogWrite(ENB, carSpeed);

  digitalWrite(IN1, LOW);

  digitalWrite(IN2, HIGH);

  digitalWrite(IN3, LOW);

  digitalWrite(IN4, HIGH); 

  Serial.println("Left");

}


void right() {

  analogWrite(ENA, carSpeed);

  analogWrite(ENB, carSpeed);

  digitalWrite(IN1, HIGH);

  digitalWrite(IN2, LOW);

  digitalWrite(IN3, HIGH);

  digitalWrite(IN4, LOW);

  Serial.println("Right");

}


void stop() {

  digitalWrite(ENA, LOW);

  digitalWrite(ENB, LOW);

  Serial.println("Stop!");


//Ultrasonic distance measurement Sub function

int Distance_test() {

  digitalWrite(Trig, LOW);   

  delayMicroseconds(2);

  digitalWrite(Trig, HIGH);  

  delayMicroseconds(20);

  digitalWrite(Trig, LOW);   

  float Fdistance = pulseIn(Echo, HIGH);  

  Fdistance= Fdistance / 58;       

  return (int)Fdistance;

}  


void setup() { 

  myservo.attach(3,700,2400);  // attach servo on pin 3 to servo object

  Serial.begin(9600);     

  pinMode(Echo, INPUT);    

  pinMode(Trig, OUTPUT);  

  pinMode(IN1, OUTPUT);

  pinMode(IN2, OUTPUT);

  pinMode(IN3, OUTPUT);

  pinMode(IN4, OUTPUT);

  pinMode(ENA, OUTPUT);

  pinMode(ENB, OUTPUT);

  stop();


void loop() { 

    myservo.write(90);  //setservo position according to scaled value

    delay(500); 

    middleDistance = Distance_test();


    if(middleDistance <= 40) {     

      stop();

      delay(500);                         

      myservo.write(10);          

      delay(1000);      

      rightDistance = Distance_test();

      

      delay(500);

      myservo.write(90);              

      delay(1000);                                                  

      myservo.write(180);              

      delay(1000); 

      leftDistance = Distance_test();

      

      delay(500);

      myservo.write(90);              

      delay(1000);

      if(rightDistance > leftDistance) {

        right();

        delay(360);

      }

      else if(rightDistance < leftDistance) {

        left();

        delay(360);

      }

      else if((rightDistance <= 40) || (leftDistance <= 40)) {

        back();

        delay(180);

      }

      else {

        forward();

      }

    }  

    else {

        forward();

    }                     

}


After uploading the provided sketch to the Arduino board, follow these steps:

By following these steps, you can observe the Smart Car's autonomous movement and obstacle avoidance functionality in action.

»»» END of  Smart Robot Car »»»