Four Way Tracking Car Tutorial
Four Way Tracking Car Tutorial
V2.0.20.12.9
Product List
Preface
Welcome to use the DIY four way tracking and Ultrasonic obstacle
this tutorial will help you make something very cool: DIY four way
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,
components required for these items. At the same time, there are
that your smart car can be assembled and run smoothly, if you
and we will provide you with high-quality technical support services for
The content of this tutorial can ensure that novices without any
Arduino and want to learn how to program and build circuits, please visit
Arduino board
input and output interfaces, etc.The Arduino board can use sensors to
motor rotation, and so on. We just need to assemble the circuit and
boards include
Circuit diagram of the product
Component description:
L298N:
Maximum range 4m
Output echo signal Output TTL level signal, proportional to the range
Size 45*20*15
SG90 module:
The control signal enters the signal modulation chip from the channel of
to the motor driver chip determines the positive and negative rotation
of the motor.When the motor speed is fixed, the potentiometer is driven
about 20ms, and the high level part of this pulse is generally the Angle
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:
Working current 1A
Mounting aperture M3
Size 42*38mm
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 big.
Infrared detection method, that is, the use of infrared light in different
light to the ground, when the infrared light encounter white paper floor
absorbed and the receiver tube on the car does not receive the infrared
to determine the location of the black line and car travel route.
Figure:
LM393 module:
indicator will light up, at the same time signal output interface and
output digital signal (a low level signal), can be adjusted through the
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
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
3. The output port OUT of the sensor module can be directly connected
directly;Connection mode:
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.
#define ENA 5
#define ENB 6
#define IN1 4
#define IN2 2
#define IN3 3
#define IN4 7
int SensorLeft;
int SensorRight;
int rightDistance = 0, leftDistance = 0, middleDistance = 0;
Serial.println("Forward");
}
Serial.println("Left");
}
Serial.println("Right");
}
float GetDistance()
{
float distance;
// Send a low short pulse to Trig to trigger the ranging
digitalWrite(Trig, LOW); //Send a low level to Trig
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
Serial.print("Distance = ");
Serial.println(distance);//The serial output distance is converted into cm
return distance;
}
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();
myservo.write(100); //setservo position according to scaled value
delay(100);
}
void loop()
{
SensorLeft = digitalRead(A5);//The sensor on the left
SensorRight = digitalRead(A2);//The sensor on the Right
middleDistance = GetDistance();//getDistance();
delay(500);
myservo.write(100);
delay(500);
myservo.write(180);
delay(500);
leftDistance = GetDistance();//getDistance();
delay(500);
myservo.write(100);
delay(500);
else
{
forward();
}
}
else
{
forward();
}
if(!SensorLeft)
{
right();
delay(300);
}
else if(!SensorRight)
{
left();
delay(300);
}
else
{
forward();
}
}
int Sensor1;
int Sensor2;
int Sensor3;
int Sensor4;
void setup() {
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode(10, INPUT);
pinMode(11, INPUT);
Serial.begin(9600);
}
void loop()
{
Sensor1 = digitalRead(8);
Sensor2 = digitalRead(9);
Sensor3 = digitalRead(10);
Sensor4 = digitalRead(11);
if ((Sensor4 == LOW || Sensor3 == LOW) && (Sensor2 == HIGH || Sensor1 ==
HIGH))
{
analogWrite(ENA, 150);
analogWrite(ENB, 150);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3,HIGH);
digitalWrite(IN4, LOW);
Serial.println("left");
}
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3,LOW );
digitalWrite(IN4, HIGH);
Serial.println("right");
}
else if(Sensor4 == LOW && Sensor3 == LOW && Sensor2 == LOW && Sensor1
== LOW)
{
analogWrite(ENA, 180);
analogWrite(ENB, 180);
//FORWARD
digitalWrite(IN1, HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
Serial.println("run");
}
else if((Sensor3 == HIGH || Sensor2 == HIGH ) && Sensor4 == LOW &&
Sensor1 == LOW)
{
analogWrite(ENA, 180);
analogWrite(ENB, 180);
//FORWARD
digitalWrite(IN1, HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
Serial.println("run");
}
else
{
analogWrite(ENA, 0);
analogWrite(ENB, 0);
digitalWrite(IN1, LOW);
digitalWrite(IN2,LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
}
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
/*************************************
* TIME:2021.12.31
* Development Team: Zhiyi Technology Co., Ltd.
*
* **************************************/
#include <Servo.h>
int ENA = 5;
int IN1 = 4;
int IN2 = 2;
int ENB = 6;
int IN3 = 3;
int IN4 = 7;
int SensorLeft;
int SensorRight;
void setup()
{
myservo.attach(A0); // attaches the servo on pin 9 to the servo object
//Set the pin mode
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);
myservo.write(100);
}
void loop()
{
cm = GetDistance();
//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 (SensorLeft == HIGH && SensorRight == 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(SensorLeft == LOW && SensorRight == HIGH&& ( cm >5 && cm <10))
{
left();
}
else if(SensorLeft == HIGH && SensorRight == HIGH&&( cm >11 && cm <20))
{
forward();
}
else if(SensorLeft == HIGH && SensorRight == HIGH && ( cm >20 || cm <3))
{
stop();
}
else if((SensorLeft == LOW && SensorRight == LOW)||( cm <5))
{
back();
}
if (SensorLeft == LOW)
{
myservo.write(160);
}
else if (SensorRight == LOW)
{
myservo.write(40);
}
else
{
myservo.write(100);
}
/*
Function: obtain ultrasonic sensor ranging data
Parameters: Trig, Echo
Parameter description: sensor connected to the motherboard pin port A4,A5
Trig -------> pin D12
Echo -------> pin D13
*/
float GetDistance()
{
float distance;
// Send a low short pulse to Trig to trigger the ranging
digitalWrite(Trig, LOW); //Send a low level to Trig
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
Serial.print("Distance = ");
Serial.println(distance);//The serial output distance is converted into cm
return distance;
}
//turn left
void left(){
//Set the speed of the motor
analogWrite(ENA, 60);
analogWrite(ENB, 160);
//The right forward
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
//The left STOP
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
Serial.println("left");
}
//turn right
void right(){
//Set the speed of the motor
analogWrite(ENA, 160);
analogWrite(ENB, 60);
//The right stop
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
//The left forward
digitalWrite(IN3,LOW);
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("forward");
}
//stop
void stop(){
analogWrite(ENA, 0);
analogWrite(ENB, 0);
//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");