├── Line-Follower-Robot-QTR-8RC.ino └── README.md /Line-Follower-Robot-QTR-8RC.ino: -------------------------------------------------------------------------------- 1 | #include //Adafruit Motor Shield Library. First you must download and install AFMotor library 2 | #include //Pololu QTR Sensor Library. First you must download and install QTRSensors library 3 | 4 | AF_DCMotor motor1(1, MOTOR12_1KHZ ); //create motor #1 using M1 output on Motor Drive Shield, set to 1kHz PWM frequency 5 | AF_DCMotor motor2(2, MOTOR12_1KHZ ); //create motor #2 using M2 output on Motor Drive Shield, set to 1kHz PWM frequency 6 | 7 | #define KP 2 //experiment to determine this, start by something small that just makes your bot follow the line at a slow speed 8 | #define KD 5 //experiment to determine this, slowly increase the speeds and adjust this value. ( Note: Kp < Kd) 9 | #define M1_minumum_speed 150 //minimum speed of the Motor1 10 | #define M2_minumum_speed 150 //minimum speed of the Motor2 11 | #define M1_maksimum_speed 250 //max. speed of the Motor1 12 | #define M2_maksimum_speed 250 //max. speed of the Motor2 13 | #define MIDDLE_SENSOR 4 //number of middle sensor used 14 | #define NUM_SENSORS 5 //number of sensors used 15 | #define TIMEOUT 2500 //waits for 2500 us for sensor outputs to go low 16 | #define EMITTER_PIN 2 //emitterPin is the Arduino digital pin that controls whether the IR LEDs are on or off. Emitter is controlled by digital pin 2 17 | #define DEBUG 0 18 | 19 | //sensors 0 through 5 are connected to analog inputs 0 through 5, respectively 20 | QTRSensorsRC qtrrc((unsigned char[]) { A4,A3,A2,A1,A0} ,NUM_SENSORS, TIMEOUT, EMITTER_PIN); 21 | 22 | unsigned int sensorValues[NUM_SENSORS]; 23 | 24 | void setup() 25 | { 26 | delay(1500); 27 | manual_calibration(); 28 | set_motors(0,0); 29 | } 30 | 31 | int lastError = 0; 32 | int last_proportional = 0; 33 | int integral = 0; 34 | 35 | void loop() 36 | { 37 | unsigned int sensors[5]; 38 | int position = qtrrc.readLine(sensors); //get calibrated readings along with the line position, refer to the QTR Sensors Arduino Library for more details on line position. 39 | int error = position - 2000; 40 | 41 | int motorSpeed = KP * error + KD * (error - lastError); 42 | lastError = error; 43 | 44 | int leftMotorSpeed = M1_minumum_speed + motorSpeed; 45 | int rightMotorSpeed = M2_minumum_speed - motorSpeed; 46 | 47 | // set motor speeds using the two motor speed variables above 48 | set_motors(leftMotorSpeed, rightMotorSpeed); 49 | } 50 | 51 | void set_motors(int motor1speed, int motor2speed) 52 | { 53 | if (motor1speed > M1_maksimum_speed ) motor1speed = M1_maksimum_speed; 54 | if (motor2speed > M2_maksimum_speed ) motor2speed = M2_maksimum_speed; 55 | if (motor1speed < 0) motor1speed = 0; 56 | if (motor2speed < 0) motor2speed = 0; 57 | motor1.setSpeed(motor1speed); 58 | motor2.setSpeed(motor2speed); 59 | motor1.run(FORWARD); 60 | motor2.run(FORWARD); 61 | } 62 | 63 | //calibrate for sometime by sliding the sensors across the line, or you may use auto-calibration instead 64 | void manual_calibration() { 65 | 66 | int i; 67 | for (i = 0; i < 250; i++) 68 | { 69 | qtrrc.calibrate(QTR_EMITTERS_ON); 70 | delay(20); 71 | } 72 | 73 | if (DEBUG) { 74 | Serial.begin(9600); 75 | for (int i = 0; i < NUM_SENSORS; i++) 76 | { 77 | Serial.print(qtrrc.calibratedMinimumOn[i]); 78 | Serial.print(' '); 79 | } 80 | Serial.println(); 81 | 82 | for (int i = 0; i < NUM_SENSORS; i++) 83 | { 84 | Serial.print(qtrrc.calibratedMaximumOn[i]); 85 | Serial.print(' '); 86 | } 87 | Serial.println(); 88 | Serial.println(); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Arduino Line Following Robot 2 | In this tutorial, you will make line following / tracking robot. This tutorial involves building a line follower robot with an QTR-8RC Reflectance Sensor Array.

3 | **Full tutorial:** https://youtu.be/wbrt2ClgZik

4 | [![IMAGE ALT TEXT HERE](http://img.youtube.com/vi/wbrt2ClgZik/0.jpg)](http://www.youtube.com/watch?v=wbrt2ClgZik)

5 | # Required Hardwares:
6 | - 1x Arduino UNO R3: http://bit.ly/2xt9MVk 7 | - 1x L293D Motor Drive Shield: http://bit.ly/2H7vmy9 8 | - QTR 8 Infrared Sensor: https://bit.ly/3eUL0Px 9 | - 2x Wheel and Gear Motor: https://bit.ly/2RXAWLS 10 | - 3 in 1 Jumper Wire: http://bit.ly/2J6de9E 11 | - 2WD Robot Car Chassis Kit: http://bit.ly/2sqIHgy 12 | - Arduino Compatible SCM & DIY Kits: http://bit.ly/2J2AFF7 13 | # Download the Source Code and install the Library:
14 | - Download AFMotor.h library: https://learn.adafruit.com/adafruit-motor-shield/library-install
15 | Uncompress the ZIP file onto your desktop
16 | Place the AFMotor folder into your arduinosketchfolder/libraries folder
17 | Rename the uncompressed folder AFMotor

18 | - Download QTR-8 library: https://github.com/pololu/qtr-sensors-arduino/releases
19 | Rename the folder “qtr-sensors-arduino-xxxx” to “QTRSensors”
20 | Drag the “QTRSensors” folder into the “libraries” directory inside your Arduino sketchbook directory
21 | 22 | --------------------------------------------------------------------------------