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.
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 TestThe 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
TIPS: 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 ModeOnce 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.
Then, upload the "Bluetooth Car" Sketch to your Smart Car Arduino board. A copy of the "Bluetooth Car" Sketch is also provided here.
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 ModeIn 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.
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.
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 ModeElegoo’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 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.
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(){ 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.
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 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 »»» |
|