Arduino -based four way tracking car kit tutorial V2.
V2.0.20.12.9
Product List
Preface
Welcome to use the DIY four way tracking and Ultrasonic obstacle
avoidance car kit tutorial launched by ZHIYI! Whether you are an
experienced maker or a novice with a small amount of professional skills,
this tutorial will help you make something very cool: DIY four way
tracking and Ultrasonic obstacle avoidance car kit tutorial mode.
The kit is based on the popular open source electronic platform Arduino.
You can share and exchange your experiences and design ideas with fans
all over the world. All components in the kit are individually packaged,
including all the electronic components, modules and mechanical
components required for these items. At the same time, there are
detailed assembly and debugging instructions in this tutorial. To ensure
that your smart car can be assembled and run smoothly, if you
encounter difficult technical problems, you can contact us at any time,
and we will provide you with high-quality technical support services for
free as soon as possible.
The content of this tutorial can ensure that novices without any
professional skills can accomplish the goal. If you are interested in
Arduino and want to learn how to program and build circuits, please visit
our shop or contact us to purchase an Arduino learning starter kit
specially prepared for beginners.
Arduino
Arduino is an open source electronic platform based on easy-to-use
hardware and software.Suitable for anyone working on interactive
projects. Usually, an Arduino project consists of circuits and codes.
Arduino board
The Arduino board is a circuit board that integrates a microcontroller,
input and output interfaces, etc.The Arduino board can use sensors to
sense the environment and receive user operations to control LEDs,
motor rotation, and so on. We just need to assemble the circuit and
write the code.Currently, there are several models of Arduino
development boards, and the codes between different types of
development boards are common (due to different hardware, some
development boards may not be fully compatible). Popular main control
boards include
Circuit diagram of the product
Component description:
L298N:
1. Input voltage of logic part: 6 ~ 7V
2. input voltage of drive part vs: 4.8 ~ 46v
3. working current of logic part is: ≤ 36ma
4. working current of driving part IO: ≤ 2A
5. maximum dissipation power: 25W (t = 75 ℃)
6. Control signal input level: high level: 2.3V ≤ VIN ≤ vs
low level:- 0.3V ≤ VIN ≤ 1.5V
7. working temperature: - 25 ℃ ~ + 130 ℃
8.driving mode: double channel high power H-bridge drive
How does L298N module work?
1.ENA, ENB control enable
2.Out1,out2 and out3,out4 controls the motor,
3.IN1, IN2, IN3, IN4 connect control level, control positive and negative
rotation.
State ENA ENB IN1 IN2 IN3 IN4
Stop 0 0 X X X X
Brake 1 1 0 0 0 0
Forward 1 1 1 0 1 0
Back 1 1 0 1 0 1
Turn Left 1 1 0 0 1 0
Turn Right 1 1 1 0 0 0
Figure:
HC-SR04 module:
Electrical parameters HC-SR04 Ultrasonic module
Working voltage DC-5V
Working current 15mA
Working frequency 40KHz
Maximum range 4m
Minimum range 2cm
Measuring angle 15°
Input trigger signal 10 US TTL pulse
Output echo signal Output TTL level signal, proportional to the range
Size 45*20*15
How does ultrasonic obstacle avoidance work?
1. Transmitter (trigger pin) sending signal: high-frequency sound.
2. When a signal finds an object, it is reflected.
3. Receiver (echo pin) : Receives the signal reflected back from it
Figure:
The HC-SR04 ultrasonic sensor detects how far ahead an obstacle has
appeared.If there is an obstacle in your specified detection range, the
car can make corresponding actions.For example, forward, back, turn
left or turn right.
SG90 module:
The control signal enters the signal modulation chip from the channel of
the receiver to obtain the DC bias voltage.It has an internal reference
circuit, which generates a reference signal with a cycle of 20ms and a
width of 1.5ms. The dc bias voltage obtained is compared with the
voltage of the potentiometer to obtain the voltage difference
output.Finally, the positive and negative output of the voltage difference
to the motor driver chip determines the positive and negative rotation
of the motor.When the motor speed is fixed, the potentiometer is driven
to rotate by the cascade reduction gear, so that the voltage difference is
0 and the motor stops rotating.
How does SG90 module work?
Control of steering gear:
The control of steering gear generally requires a time base pulse of
about 20ms, and the high level part of this pulse is generally the Angle
control pulse part within the range of 0.5ms~2.5ms.Taking the servo at
an Angle of 180 degrees as an example, the corresponding control
relationship is as follows:
0.5 ms -- -- -- -- -- -- -- -- -- -- -- -- -- - 0°
1.0 ms -- -- -- -- -- -- -- -- -- -- -- -- 45°
1.5 ms -- -- -- -- -- -- -- -- -- -- -- -- 90°
2.0 ms -- -- -- -- -- -- -- -- -- -- - 135°
Figure:
Four channel tracking module:
Electrical parameters Four channel tracking module
Working voltage DC 3.3V-5V
Working current 1A
working temperature -10℃ to 50℃
Mounting aperture M3
detect distance 1mm to 60cm
Size 42*38mm
Output signal TTL level
How does Four channel tracking module work?
The sensor belongs to infrared reflection detection, so the reflectivity
and shape of the target is the key to the detection range.Among them,
the black detection distance is the smallest and the white detection
distance is the largest.Small area object distance is small, large area
distance is big.
Infrared detection method, that is, the use of infrared light in different
colors of the object surface has different reflection intensity
characteristics, in the process of the car constantly emitting infrared
light to the ground, when the infrared light encounter white paper floor
diffuse reflection, reflected light is installed in the car received by the
receiving tube;If a black line is encountered, the infrared light is
absorbed and the receiver tube on the car does not receive the infrared
light.MCU on whether to receive the reflected infrared light as the basis
to determine the location of the black line and car travel route.
Figure:
LM393 module:
The sensor module light is adaptable to the environment, it has a pair of
infrared transmitting and receiving tube, tubes, the frequency of ir when
detecting direction meet with obstacles (reflecting surface), reflected
infrared receiving tube, after the comparator circuit processing, green
indicator will light up, at the same time signal output interface and
output digital signal (a low level signal), can be adjusted through the
potentiometer knob detection distance, effective distance range 2 ~ 30
cm, the working voltage of 3.3 V to 5 V.The detection distance of the
sensor can be adjusted by potentiometer, which has the characteristics
of small interference, easy assembly and easy use.
How does Infrared detection module work?
1. When the module detects the obstacle signal in front, the green
indicator light on the circuit board lights up the level, and the OUT port
continuously outputs the low-level signal. The detection distance of the
module is 2 ~ 30cm, and the detection Angle is 35 °.Counterclockwise
potentiometer to reduce detection distance.
2. Active infrared reflection detection of the sensor, so the reflectivity
and shape of the target are the key to the detection range.Among them,
the black detection distance is small, while the white is large.Small area
object distance is small, large area distance is big.
3. The output port OUT of the sensor module can be directly connected
with IO port of single-chip microcomputer, or it can drive a 5V relay
directly;Connection mode:
VCC-VCC; GND-GND; OUT-IO
4. The comparator adopts LM393 and works stably;
5. 3-5V DC power supply can be used to supply power to the module.When the
power is switched on, the red power indicator lights up.
6. Screw hole of 3mm for easy fixing and installation;
7. Circuit board size: 3.2cm * 1.4cm
Figure:
Code description:
Part 1: Ultrasonic obstacle avoidance program
/*************************************
* TIME:2020.12.9
* Development Team: Zhiyi Technology Co., Ltd.
*
* **************************************/
/*************************************
* TIME:2020.12.9
* Development Team: Zhiyi Technology Co., Ltd.
*
* **************************************/
#include <Servo.h> //servo library
Servo myservo; // create servo object to control servo
int Echo = 13;//Pin to D13
int Trig = 12;//Pin to D12
int sound = 15;//Pin to D15
#define ENA 5
#define ENB 6
#define IN1 3
#define IN2 4
#define IN3 2
#define IN4 7
#define carSpeed 150//Set the carSpeed to 150
int rightDistance = 0, leftDistance = 0, middleDistance = 0;
void forward(){//forward function
analogWrite(ENA, carSpeed);//Set the speed of ENA
analogWrite(ENB, carSpeed);//Set the speed of ENB
//The right forward
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
//The left forward
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
Serial.println("Forward");
}
void back() {//back function
analogWrite(ENA, carSpeed);//Set the speed of ENA
analogWrite(ENB, carSpeed);//Set the speed of ENB
//The rihgt back
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
//The left back
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
Serial.println("Back");
}
void left() {//left function
analogWrite(ENA, carSpeed);//Set the speed of ENA
analogWrite(ENB, carSpeed);//Set the speed of ENB
//The right forward
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
//The left back
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
Serial.println("Left");
}
void right() {//right function
analogWrite(ENA, carSpeed); //Set the speed of ENA
analogWrite(ENB, carSpeed); //Set the speed of ENB
//The rihgt back
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
//The left forward
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
Serial.println("Right");
}
void stop() {//stop function
digitalWrite(ENA, LOW); //Set the speed of ENA to low
digitalWrite(ENB, LOW); //Set the speed of ENB to low
Serial.println("Stop!");
}
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(A0,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();//getDistance();
if(middleDistance <= 40) {
stop();
delay(500);
myservo.write(10);
delay(500);
rightDistance = Distance_test();//getDistance();
delay(500);
myservo.write(90);
delay(500);
myservo.write(180);
delay(500);
leftDistance = Distance_test();//getDistance();
delay(500);
myservo.write(90);
delay(500);
if(rightDistance > leftDistance) {
right();
delay(360);
}
else if(rightDistance < leftDistance) {
left();
delay(360);
}
else if((rightDistance <= 40) || (leftDistance <= 40)) {
back();
}
else {
forward();
}
}
else {
forward();
}
}
explanation for the program:
When the front side detects the obstacle, the trolley will stop, and the
steering engine will rotate left and right to detect the distance between
the two sides of the obstacle, judge the distance between the two sides
is further, and the trolley will rotate to which side.
Part 2: Four road tracking program
int ENA = 5;
int IN1 = 3;
int IN2 = 4;
int ENB = 6;
int IN3 = 2;
int IN4 = 7;
#define ENASpeed 100 //set the speed for ENA
#define ENBSpeed 100//set the speed for ENB
int Sensor1 = 0;
int Sensor2 = 0;
int Sensor3 = 0;
int Sensor4 = 0;
void setup() {
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(Sensor1, INPUT);
pinMode(Sensor2, INPUT);
pinMode(Sensor3, INPUT);
pinMode(Sensor4, INPUT);
Serial.begin(9600);
}
void loop(){
digitalWrite(ENA, HIGH);
digitalWrite(ENB, HIGH);
//Count from left to right
Sensor1 = digitalRead(8);//The first sensor
Sensor2 = digitalRead(9);//TheSecond sensor
Sensor3 = digitalRead(10);//Third sensor
Sensor4 = digitalRead(11);//Fourth sensor
//When a black line is detected, the sensor is low
//When no black line is detected, the sensor is high
//If the third and fourth sensors detect a black line, turn left.
if (Sensor4 == LOW && Sensor3 == LOW && Sensor2 == HIGH && Sensor1 == HIGH)
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
Serial.println("left");
}
//If the first s and Second sensors detect a black line, turn right.
else if(Sensor4 == HIGH && Sensor3 == HIGH && Sensor2 == LOW && Sensor1 ==
LOW)
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
Serial.println("right");
}
//If the third and Second sensors detect a black line, turn run.
else if(Sensor4 == LOW && Sensor3 == HIGH && Sensor2 == HIGH && Sensor1 ==
LOW)
{
//FORWARD
digitalWrite(IN1, HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
Serial.println("run");
}
//When all the sensors fail to detect the signal, the car backs up
else if(Sensor4 == LOW && Sensor3 == LOW && Sensor2 == LOW && Sensor1 ==
LOW)
{
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
Serial.println("back");
}
explanation for the program:
When Sensor1 and Sensor2 infrared sensors detect the black line, and
Sensor3 and Sensor4 do not detect the black line, then MCU controls the
L298N module to make the car turn right.
When Sensor3 and Sensor4 infrared sensors detect the black line, and
Sensor1 and Sensor2 do not detect the black line, then MCU controls the
L298N module to make the car turn left.
When the black line is detected by Sensor3 and Sensor2 infrared sensors
and no black line is detected by Sensor1 and Sensor4, the MCU controls
the L298N module to make the car move forward.
When the black line is not detected by Sensor1, Sensor2, Sensor3 and
Sensor4 infrared sensors, the MCU controls the L298N module to make
the car move back.
Part 3: the Car follow program
#include <AFMotor.h>
#define Trig 12 //Pin Tring connects to D12
#define Echo 13 //Pin Echo connects to D13
float cm; //Distance variable
float temp; //
int ENA = 5;
int IN1 = 3;
int IN2 = 4;
int ENB = 6;
int IN3 = 2;
int IN4 = 7;
int Sensor1 =A5;//pin A5
int Sensor2 =A2;//pin A2
int val1;
int val2;
void setup() {
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(Sensor1, INPUT);
pinMode(Sensor2, INPUT);
pinMode(Trig, OUTPUT);
pinMode(Echo, INPUT);
Serial.begin(9600);
}
void loop(){
val1 = digitalRead(A5);//The sensor on the left
val2 = digitalRead(A2);//The sensor on the Right
// Send a low short pulse to Trig to trigger the ranging
digitalWrite(Trig, LOW); //Send a low level to Trig
delayMicroseconds(2); //delay 2us
digitalWrite(Trig,HIGH); //Send a HIGH level to Trig
delayMicroseconds(10); //delay 10us
digitalWrite(Trig, LOW); //Send a low level to Trig
temp = float(pulseIn(Echo, HIGH)); // Store echo wait time,
// The pulseIn function waits for the pin to change to HIGH, begins to calculate the time,
//waits for it to change to LOW, and stops the timing
// Return the length of the pulse
// the speed of sound is 340m/1s, which is converted to 34000cm / 1000000 s =>
//34/1000
// Since the actual distance from send to receive is 2 times, we have to divide by 2
// Distance (cm) = (echo time * (34/1000)) / 2
// The simplified calculation formula is (echo time * 17)/ 1000
cm = (temp * 17 )/1000; //Convert the echo time to cm
//If the sensor on the right detects the signal and the sensor on the left does not
//and the distance of the obstacle detected by the ultrasonic is less than 10cm and
greater than 5cm
//turn right
if (val1 == HIGH && val2 == LOW&& ( cm >5&& cm <10))
{
right();
}
//If the sensor on the left detects the signal and the sensor on the right does not
//and the distance of the obstacle detected by the ultrasonic is less than 10cm and
greater than 5cm
//turn left
else if(val1 == LOW && val2 == HIGH&& ( cm >5&& cm <10))
{
left();
}
else if(val1 == HIGH && val2 == HIGH&&( cm >15&& cm <25))
{
forward();
}
else if(val1 == HIGH && val2 == HIGH &&( cm >30||cm <3))
{
stop();
}
else if((val1 == LOW && val2 == LOW)||( cm <3))
{
back();
}
Serial.print("Echo =");
Serial.print(temp);//The serial port outputs the raw data of the wait time
Serial.print(" | | Distance = ");
Serial.print(cm);//The serial output distance is converted into cm
Serial.println("cm");
delay(100);
}
//turn left
void left(){
//Set the speed of the motor
analogWrite(ENA, 160);
analogWrite(ENB, 60);
//The right forward
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
//The left stop
digitalWrite(IN3,LOW );
digitalWrite(IN4,LOW );
Serial.println("left");
}
//turn right
void right(){
//Set the speed of the motor
analogWrite(ENA, 60);
analogWrite(ENB, 160);
//The right STOP
digitalWrite(IN1, LOW);
digitalWrite(IN2,LOW );
//The left forward
digitalWrite(IN3, HIGH );
digitalWrite(IN4,LOW);
Serial.println("right");
}
//turn forward
void forward(){
//FORWARD
//Set the speed of the motor
analogWrite(ENA, 130);
analogWrite(ENB, 130);
//The right forward
digitalWrite(IN1, HIGH);
digitalWrite(IN2,LOW);
//The left forward
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
Serial.println("run");
}
//stop
void stop(){
//set IN1,IN2,IN3,IN4 to LOW
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
Serial.println("stop");
}
//back up
void back(){
//Set the speed of the motor
analogWrite(ENA, 110);
analogWrite(ENB, 110);
//The right back
digitalWrite(IN1, LOW);
digitalWrite(IN2,HIGH);
//The left back
digitalWrite(IN3,LOW);
digitalWrite(IN4, HIGH);
Serial.println("back");
explanation for the program:
The realization of the function of car following module requires the
combination of two infrared detection modules and ultrasonic module.
When the sensor on the left detects the signal, while the sensor on the
right does not, and the ultrasonic detects the distance of the obstacle in
front is less than 10cm and greater than 5cm, the car turns left.
When the sensor on the right detects the signal while the sensor on the
left does not, and the ultrasonic detects the distance of the obstacle in
front is less than 10cm and greater than 5cm, the car turns right.
When the ultrasonic detects the distance of the obstacle in front is less
than 25cm and greater than 15cm, the trolley will move forward.
When the signal is detected by both sensors and the distance of the
obstacle in front is less than 3cm detected by the ultrasonic wave, the
car backs away.
When no signal is detected by sensors on both sides and the distance of
the obstacle in front is greater than 30cm or less than 3cm, the car
stops.