FWD Skill Zone
  • home
  • Power Electronics
    • Breadboard Power Supply
    • Variable Power Supply
    • DC to DC Boost Converter
  • Robotics
    • Braccio Robotic Arm
    • Voice-activated robotic arm
    • Smart Robot Car
    • Short Range Radar System
  • Machine Learning
    • Deep Learning
    • Transfer Learning
  • Arduino
    • Arduino 101
    • Arduino 102
    • Arduino 103
  • VHDL
    • Intro to VHDL
    • 2 to 4 Binary Decoder
    • 3 to 8 Binary Decoder
    • Universal Shift Register
  • Verilog
    • Intro to Verilog
    • Verilog Construction
    • Verilog Examples
  • Educational Links
  • Contact us
  • home
  • Power Electronics
    • Breadboard Power Supply
    • Variable Power Supply
    • DC to DC Boost Converter
  • Robotics
    • Braccio Robotic Arm
    • Voice-activated robotic arm
    • Smart Robot Car
    • Short Range Radar System
  • Machine Learning
    • Deep Learning
    • Transfer Learning
  • Arduino
    • Arduino 101
    • Arduino 102
    • Arduino 103
  • VHDL
    • Intro to VHDL
    • 2 to 4 Binary Decoder
    • 3 to 8 Binary Decoder
    • Universal Shift Register
  • Verilog
    • Intro to Verilog
    • Verilog Construction
    • Verilog Examples
  • Educational Links
  • Contact us
Picture

Smart Robot Car

An impressive work combining different solutions to the real world problems! The Elegoo Smart Car has features like Ultrasonic Range finder for collision avoidance and semi-autonomus navigation; Optical sensors for line following navigation; Bluetooth connection for remote control operation; and an Infrared Remote Control.​
Picture
Image courtesy of Elegoo
Assembly instruction of the Elegoo Smart Robot Car is quite simple and the assembly procedure can be obtained from Elegoo website https://www.elegoo.com/download/ and select "Elegoo Smart Robot Car Kit V3.0 Plus." The Elegoo's assembly instruction is also provided here. 
When you open the Smart Car kit, you will find all the hardware in designated plastic bags. Follow the instruction above and enjoy the assembly process. Just one advice, DO NOT assemble the top and bottom "Base Plates" together on page 16 of the assembly manual. The illustration on page 16 is deceiving that it looks like those Plates are assembled on step 16; however, these Plates should be assembled on step 19 after all the electrical wires are connected to the motor driver that is mounted onto the bottom Plate. Other than this, the assembly process is quite easy and intuitive. 
Parts
- 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
Picture
Elegoo's Smart Car Parts
Once the Smart Car is fully assembled, the next step is testing all the functions and operations of the car. The Smart Car comes fully loaded with the "SmartCar Multi functions" software that allows the car to be operated in four mode of operations: Bluetooth Mode,  Line Tracking Mode,  Obstacle-avoidance Mode, and  IR-Remote Control Mode. However,  all these basic codes for the Smart Car are also provided by Elegoo and these codes can be downloaded from Elegoo's website.  The downloaded Zip file has six sections for tutorial purpose. We will start form the  AUTO_GO.ino sketch that moves the car in all directions. We will also need Arduino IDE to compile and run these codes.

Smart Robot Car – Initial Test

The Smart Robot Car has the following functions:
1. Bluetooth Mode
2. Line Tracking Mode
3. Obstacle-avoidance Mode
​4. IR-Remote Control Mode

Make the Car Move

  • Download the Arduino sketch from Elegoo’s website to your computer.
  • Unzip the downloaeded files.
  •  Download the Arduino development software (skip this step if you already have the Arduino IDE).
  • Connect the Smart Car to the computer using the provided USB cable.
  • Uninstall the Bluetooth module from the Smart Car
  •  Open the Arduino IDE and “AUTO_GO.ino".
  • Inside the Arduino IDE. Select “Tool” => “Board” => select “Arduino/Genuino Uno
  • Select => “Tool” => “Port” => “COMXX (Arduino/Genuino Uno)” (select the appropriate COM port).
  • Upload the Arduino sketch, “AUTO_GO.ino” to your Smart Car.
  • Disconnect the programming cable.
  • Reinstall the Bluetooth module.
TIPS:
When uploading codes, please remove the Bluetooth module from the IO expansion board. You can mount the Bluetooth module after the upload.

Move Automatically
We will try to make the car move automatically: go forward 0.4s - back up 0.4s - turn left 0.4s - turn right 0.4s. First of all, let's see the connection of the motor and the L298N board. We will use Arduino pins 5, 6, 7, 8, 9, and 11 to control the car using the motor controller.  Pin 9 and 11 control the right wheel and pin 7 and 8 control the left wheel. The remaining two pins, 5 and 6 are used to control ENA and ENB respectively.
After the "Auto Go" sketch is uploaded to the Arduino UNO, place the Smart Car on a level surface. Turn on the battery switch and observe the test. The Smart Car moves forward for one second and then reverses for one second. It then turns left for one second and then right for one second. This sequence repeats forever. The "Auto Go" test verifies the motors operation 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);
}

Bluetooth Mode

Once you verified the assembly and motor operation of the Smart Car using the "Auto Go" sketch, you can use the "Bluetooth Car" Sketch form the downloaded files to perform the Bluetooth operational test. First, install the Elegoo Bluetooth App, "Elegoo BLE Tool", on your smartphone from Google Play Store for Android or from App Store for an iPhone. 
Picture
Elegoo BLE Tool
Then, upload the "Bluetooth Car" Sketch to your Smart Car Arduino board. A copy of the "Bluetooth Car" Sketch is also provided here.
  • Connect the Smart Car to the computer.
  • Disconnect the Bluetooth module
  • Upload the downloaded Arduino sketch, “bluetooth_car.ino” to your Smart Car.
  • Disconnect the programming cable.
  • Reinstall the Bluetooth module.
  • Position the Smart Car on a flat surface and turn the power switch to ON
  • Open the “Elegoo BLE Tool” App on your smartphone.
  • Pair the Smart Car Bluetooth with the App.
  • Inside the “Rocker Control Panel” of the App, use the Bluetooth mode joystick control to move the car forward, back, left, right, and Stop to test the Bluetooth operation. 
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. ​
Picture
​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;
      default:  break;
    }
  }
}

Line Tracking Mode

In this mode of operation, you will need a black tape (provided in the kit). Make your own closed path similar to the image provided here.  It is recommended that you make the closed path as smooth as possible. 
Picture
The Line Tracking mode uses a line sensing module that has three pairs of IR light sensors: left, center, and right transmiting and receiving sensors. Each sensor has a transmitting LED and a receiving optical sensor. These light sensors are designed to respond to a dark color and determine the physical position of the Smart Car . The purpose of this module is keeping the Smart Car aligned with the dark line or the center sensor. 
Picture
In order to test the Line Tracking Module, download the Arduino sketch, "Line_tracking_car.ino" to your Smart Car and position the Smart Car on the closed path. Turn on the power switch and then the Smart Car will start to navigate along the black line until you stop the car by turning the power switch off. The Arduino sketch is also provided here for convenience.  ​
  • Disconnect the Bluetooth module
  • Upload the downloaded Arduino sketch, “Line_tracking_car.ino” to your Smart Car.
  • Disconnect the programming cable.
  • Reinstall the Bluetooth module.
  • Position the Smart Car on the closed path (black tape), turn the power switch to ON
  • Open the “Elegoo BLE Tool” App on your smartphone, if you haven’t done it so.
  • Pair the Smart Car Bluetooth with the APP.
  • Inside the “Rocker Control Panel” of the App, select the "Line Tracking" button and observe the Smart Car navigating along the closed path. Enjoy.
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);  
  }
}

Obstacle-Avoidance Mode

Elegoo’s collision avoidance mode uses HCSR04 ultrasonic sensor which is mounted at the front of the smart car. The ultrasonic sensor is mounted on the servo motor allowing to sweep for about 180 degrees. The ultrasonic sensor uses sonar to determine distance to an object. It offers excellent range detection with high accuracy from 2cm to 400 cm or 1” to 13 feet.
The ultrasonic sensor has four pins:
VCC – This is the 5-volt positive power connection. 
TRIG – This is the “Trigger”, an input for the pulse we will be sending from the ultrasonic transmitter.
ECHO – This is an output that sends back the received pulse.
GND – The Ground connection.
Picture
Ultrasonic sensor
The timing diagram of HCSR04 is shown the figure below. To start the measurement, Trig of SR04 must receive a pulse of high (5V) for at least 10us, this will initiate the sensor to transmit 8 pulses of ultrasonic burst at 40kHz and wait for the reflected ultrasonic burst. When the sensor detected ultrasonic from the receiver, it will set the Echo pin to high (5V) and delay for a period which is proportion to a distance based on the object detection.
Picture
Ultrasonic Module Timing Diagram
​To obtain the distance, measure the width (Ton) of Echo pin.
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.
  • Ultrasonic sensor measures the distance to the nearest object until obstacle detected.
  • Stop the car.
  • Measure the distance to the right and left of the Smart Car.
  • Turn the car in the direction that you measure the longest distance.
  •  Move forward.
  • Repeat the same process when another obstacle is detected.
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.
  • Connect the Smart Car to the computer.
  • Disconnect the Bluetooth module
  • Upload the downloaded Arduino sketch, “Obstacle_Avoidance_Car.ino” to your Smart Car.
  • Disconnect the programming cable.
  • Reinstall the Bluetooth module.
  • Position the Smart Car on a flat surface, turn the power switch to ON
  • Open the “Elegoo BLE Tool” App to your smartphone, if you haven’t done it on the previous section.
  • Pair the Smart Car Bluetooth with the App.
  • Inside the “Rocker Control Panel” of the App, select obstacle avoidance and enjoy the autonomous navigation in action.
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 above sketch to the Arduino board, disconnect the cable, put the vehicle on a flat surface and power on the Smart Car battery switch. Then, you will see that the vehicle will move forward while the ultrasonic sensor measures the distance continuously. If there are obstacles ahead, the Smart Car will stop, and it will change its direction to bypass the obstacle ahead. This process will continue until the Smart Car is turned off.
»»» END of  Elegoo Smart Car »»»
Have a question or comments?
    - click here to ping me
  • home
  • Power Electronics
    • Breadboard Power Supply
    • Variable Power Supply
    • DC to DC Boost Converter
  • Robotics
    • Braccio Robotic Arm
    • Voice-activated robotic arm
    • Smart Robot Car
    • Short Range Radar System
  • Machine Learning
    • Deep Learning
    • Transfer Learning
  • Arduino
    • Arduino 101
    • Arduino 102
    • Arduino 103
  • VHDL
    • Intro to VHDL
    • 2 to 4 Binary Decoder
    • 3 to 8 Binary Decoder
    • Universal Shift Register
  • Verilog
    • Intro to Verilog
    • Verilog Construction
    • Verilog Examples
  • Educational Links
  • Contact us