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:
Ultrasonic Range Finder for Collision Avoidance and Semi-Autonomous Navigation: This feature enables the car to detect obstacles in its path and navigate around them, ensuring smooth and safe movement without manual intervention.
Optical Sensors for Line Following Navigation: These sensors allow the car to follow a predefined path, making it ideal for applications that require precise route adherence, such as warehouse logistics or guided tours.
Bluetooth Connection for Remote Control Operation: Through Bluetooth connectivity, users can remotely control the car using a smartphone or other compatible devices, offering flexibility and ease of operation for various tasks.
Infrared Remote Control: In addition to Bluetooth, the car can be operated using an infrared remote control, providing an alternative method for user interaction and control.
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
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:
Bluetooth Mode
Line Tracking Mode
Obstacle-Avoidance Mode
IR-Remote Control Mode
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:
Bluetooth Mode
Line Tracking Mode
Obstacle-avoidance Mode
IR-Remote Control Mode
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:
Launch the Arduino IDE.
Open the "AUTO_GO.ino" sketch from the extracted files.
5. Connect the Arduino UNO:
Connect the Arduino UNO board to your computer using a USB cable.
Select the appropriate COM port and board type in the Arduino IDE (Tools > Board > Arduino UNO, Tools > Port > Select the correct COM port).
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:
Once the upload is complete, disconnect the USB cable.
Turn on the Smart Car using the rechargeable battery.
The car should now move in all directions according to the "AUTO_GO.ino" sketch.
By following these steps, you can ensure that your Elegoo Smart Robot Car is fully functional and ready for operation in its various modes.
- Move Automatically
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.
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;
default: break;
}
}
}
3. Line Tracking Mode
To utilize the Line Tracking mode of operation for your Smart Car, follow these steps:
1. Prepare the Closed Path:
Use the black tape provided in the kit to create a closed path similar to the image provided. It's recommended to make the path as smooth as possible for optimal performance.
2. Understand the Line Tracking Module:
The Line Tracking mode utilizes a line sensing module equipped with three pairs of IR light sensors: left, center, and right. Each pair consists of a transmitting LED and a receiving optical sensor. These sensors detect dark colors and determine the physical position of the Smart Car along the black line or relative to the center sensor.
3. Upload the Line Tracking Sketch:
Disconnect the Bluetooth module from the Smart Car.
Upload the downloaded Arduino sketch ("Line_tracking_car.ino") to your Smart Car's Arduino board.
After uploading, reinstall the Bluetooth module.
4. Position the Smart Car:
Place the Smart Car on the closed path created with the black tape.
Turn on the power switch of the Smart Car.
5. Use the Elegoo BLE Tool App:
Open the "Elegoo BLE Tool" app on your smartphone, if you haven’t already.
Pair the Smart Car's Bluetooth with the app.
6. Select Line Tracking Mode:
Inside the “Rocker Control Panel” of the app, select the "Line Tracking" button.
7. Observe Navigation:
Watch as the Smart Car navigates along the closed path, guided by the Line Tracking module's sensors.
By following these steps, you can enjoy observing your Smart Car autonomously navigate along the predefined path in 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:
VCC: This pin is connected to the 5-volt positive power source.
TRIG: Known as the "Trigger," this pin serves as an input for the pulse transmitted from the ultrasonic sensor.
ECHO: This pin functions as an output, sending back the received pulse.
GND: The Ground connection, providing the electrical ground connection.
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.
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.
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 provided sketch to the Arduino board, follow these steps:
Disconnect the Cable: Disconnect the programming cable from the Arduino board.
Place the Vehicle: Put the Smart Car on a flat surface.
Power On: Turn on the Smart Car's battery switch.
Observe Movement and Measurement:
The vehicle will move forward while the ultrasonic sensor continuously measures the distance.
If obstacles are detected ahead, the Smart Car will stop and change its direction to bypass the obstacle.
This process will continue until the Smart Car is turned off.
By following these steps, you can observe the Smart Car's autonomous movement and obstacle avoidance functionality in action.
»»» END of Smart Robot Car »»»