0% found this document useful (0 votes)
24 views5 pages

LINE FOLLOWER Using Arduino - MURTAZA'S WORKSHOP

This document provides instructions to build a line following robot using an Arduino. It includes a parts list, explains the chassis, electronics, and assembly. The Arduino code uses sensors to follow a line and includes PID tuning parameters to control the motor speeds.

Uploaded by

Bogdan Deac
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views5 pages

LINE FOLLOWER Using Arduino - MURTAZA'S WORKSHOP

This document provides instructions to build a line following robot using an Arduino. It includes a parts list, explains the chassis, electronics, and assembly. The Arduino code uses sensors to follow a line and includes PID tuning parameters to control the motor speeds.

Uploaded by

Bogdan Deac
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

  Go to Course Home  LINE FOLLOWER using Arduino  Complete Lesson

How To Make A DIY LINE FOLLOWER using Arduino at Home

Learn How to build a PID Line Follower. With Basic Components such as Arduino, L298N H-bridge and an array of Sensors.

Parts
Arduino UNO: https://amzn.to/2QdKthB
Motor Driver: https://amzn.to/35grl6x
Battery: https://amzn.to/2Fadc0c

Chassis
Electronics
Assembly
Arduino Code

1 // Intsall QTR SENSOR Library Version 3 from Library Manager.


2
3 #include <QTRSensors.h>
4
5 /////////////////////////////////////////////////
6
7 #define speedLimit 170
8 #define rightSpeed 120
9 #define leftSpeed 120
10 #define NUM_SENSORS 8
11 QTRSensorsRC qtrrc((unsigned char[]) {
12 A0, A1, A2, A3, A4, A5, 12, 13 // SENSOR PIN NUMBERS
13 },
14 NUM_SENSORS, 2500, 2);
15 unsigned int sensorValues[NUM_SENSORS];
16 int myPins[6] = {5, 6, 7, 8, 9, 10}; // MOTOR PIN NUMBERS
17
18 //// PID TUNNING PARAMETERS
19 float Kp = 0.07;
20 float Kd = 0.8;
21 float Ki = 0;
22
23 /////////////////////////////////////////////////
24
25 int preError = 0;
26 int integral = 0;
27 int rightSpeedPwm = 0;
28 int leftSpeedPwm = 0;
29 int myFloor = 0;
30
31
32 void setup()
33 {
34 for (int i = 0; i < 6; i++) {
35 pinMode(myPins[i], OUTPUT);
36 }
37 pinMode(11, OUTPUT);
38 digitalWrite(11, HIGH);
39 delay(2000);
40 int i;
41
42 for (int i = 0; i < 200; i++)
43 {
44 if ( 0 <= i &amp;&amp; i < 5 ) rightFunc();
45 if ( 5 <= i &amp;&amp; i < 15 ) leftFunc();
46 if ( 15 <= i &amp;&amp; i < 25 ) rightFunc();
47 if ( 25 <= i &amp;&amp; i < 35 ) leftFunc();
48 if ( 35 <= i &amp;&amp; i < 45 ) rightFunc();
49 if ( 45 <= i &amp;&amp; i < 55 ) leftFunc();
50 if ( 55 <= i &amp;&amp; i < 65 ) rightFunc();
51 if ( 65 <= i &amp;&amp; i < 75 ) leftFunc();
52 if ( 75 <= i &amp;&amp; i < 85 ) rightFunc();
53 if ( 85 <= i &amp;&amp; i < 90 ) leftFunc();
54
55 if ( i >= 90 ) {
56 stopFunc();
57 delay(4);
58 }
59
60 qtrrc.calibrate();
61 delay(4);
62 }
63
64 delay(2000);
65 Serial.begin(9600);
66
67 }
68
69 void loop()
70 {
71
72 unsigned int sensorValues[8];
73 unsigned int position = qtrrc.readLine(sensorValues, 1, myFloor);
74 int error = position - 3500;
75
76 if ( sensorValues[0] < 100 &amp;&amp; sensorValues[7] < 100 ) {
77 myFloor = 0; //white ground - black line
78 }
79 if ( sensorValues[0] > 500 &amp;&amp; sensorValues[7] > 500 ) {
80 myFloor = 1; //black ground - white line
81 }
82
83 integral = integral + error;
84 int myTurn = Kp * error + Kd * (error - preError) + Ki * integral;
85 preError = error;
86
87 rightSpeedPwm = rightSpeed + myTurn ;
88 leftSpeedPwm = leftSpeed - myTurn ;
89 rightSpeedPwm = constrain(rightSpeedPwm, -speedLimit, speedLimit);
90 leftSpeedPwm = constrain(leftSpeedPwm, -speedLimit, speedLimit);
91 moveRobot(rightSpeedPwm, leftSpeedPwm);
92
93 }
94
95 void moveRobot(int myLeftSpeed, int myRightSpeed)
96 {
97 if (myLeftSpeed <= 0) {
98 digitalWrite(myPins[1], 0);
99 digitalWrite(myPins[2], 1);
100 }
101 else {
102 digitalWrite(myPins[1], 1);
103 digitalWrite(myPins[2], 0);
104 }
105
106 if (myRightSpeed <= 0) {
107 digitalWrite(myPins[3], 0);
108 digitalWrite(myPins[4], 1);
109 }
110 else {
111 digitalWrite(myPins[3], 1);
112 digitalWrite(myPins[4], 0);
113 }
114
115 analogWrite(myPins[0], abs(myLeftSpeed));
116 analogWrite(myPins[5], abs(myRightSpeed));
117 }
118
119
120 void stopFunc() {
121 moveRobot(0, 0);
122 }
123
124 void rightFunc() {
125 moveRobot(-90, 90);
126 }
127
128 void leftFunc() {
129 moveRobot(90, -90);
130 }

← Previous Next →

You might also like