├── Diagrams └── LineFollwerRobot.png ├── LICENSE ├── LineFollowerRobot └── LineFollowerRobot.ino └── README.md /Diagrams/LineFollwerRobot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/un0038998/LineFollowerRobot/ed1202f7e7e88c642b585218aa7c5e14ea0653f8/Diagrams/LineFollwerRobot.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Ujwal Nandanwar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LineFollowerRobot/LineFollowerRobot.ino: -------------------------------------------------------------------------------- 1 | #define IR_SENSOR_RIGHT 11 2 | #define IR_SENSOR_LEFT 12 3 | #define MOTOR_SPEED 180 4 | 5 | //Right motor 6 | int enableRightMotor=6; 7 | int rightMotorPin1=7; 8 | int rightMotorPin2=8; 9 | 10 | //Left motor 11 | int enableLeftMotor=5; 12 | int leftMotorPin1=9; 13 | int leftMotorPin2=10; 14 | 15 | void setup() 16 | { 17 | //The problem with TT gear motors is that, at very low pwm value it does not even rotate. 18 | //If we increase the PWM value then it rotates faster and our robot is not controlled in that speed and goes out of line. 19 | //For that we need to increase the frequency of analogWrite. 20 | //Below line is important to change the frequency of PWM signal on pin D5 and D6 21 | //Because of this, motor runs in controlled manner (lower speed) at high PWM value. 22 | //This sets frequecny as 7812.5 hz. 23 | TCCR0B = TCCR0B & B11111000 | B00000010 ; 24 | 25 | // put your setup code here, to run once: 26 | pinMode(enableRightMotor, OUTPUT); 27 | pinMode(rightMotorPin1, OUTPUT); 28 | pinMode(rightMotorPin2, OUTPUT); 29 | 30 | pinMode(enableLeftMotor, OUTPUT); 31 | pinMode(leftMotorPin1, OUTPUT); 32 | pinMode(leftMotorPin2, OUTPUT); 33 | 34 | pinMode(IR_SENSOR_RIGHT, INPUT); 35 | pinMode(IR_SENSOR_LEFT, INPUT); 36 | rotateMotor(0,0); 37 | } 38 | 39 | 40 | void loop() 41 | { 42 | 43 | int rightIRSensorValue = digitalRead(IR_SENSOR_RIGHT); 44 | int leftIRSensorValue = digitalRead(IR_SENSOR_LEFT); 45 | 46 | //If none of the sensors detects black line, then go straight 47 | if (rightIRSensorValue == LOW && leftIRSensorValue == LOW) 48 | { 49 | rotateMotor(MOTOR_SPEED, MOTOR_SPEED); 50 | } 51 | //If right sensor detects black line, then turn right 52 | else if (rightIRSensorValue == HIGH && leftIRSensorValue == LOW ) 53 | { 54 | rotateMotor(-MOTOR_SPEED, MOTOR_SPEED); 55 | } 56 | //If left sensor detects black line, then turn left 57 | else if (rightIRSensorValue == LOW && leftIRSensorValue == HIGH ) 58 | { 59 | rotateMotor(MOTOR_SPEED, -MOTOR_SPEED); 60 | } 61 | //If both the sensors detect black line, then stop 62 | else 63 | { 64 | rotateMotor(0, 0); 65 | } 66 | } 67 | 68 | 69 | void rotateMotor(int rightMotorSpeed, int leftMotorSpeed) 70 | { 71 | 72 | if (rightMotorSpeed < 0) 73 | { 74 | digitalWrite(rightMotorPin1,LOW); 75 | digitalWrite(rightMotorPin2,HIGH); 76 | } 77 | else if (rightMotorSpeed > 0) 78 | { 79 | digitalWrite(rightMotorPin1,HIGH); 80 | digitalWrite(rightMotorPin2,LOW); 81 | } 82 | else 83 | { 84 | digitalWrite(rightMotorPin1,LOW); 85 | digitalWrite(rightMotorPin2,LOW); 86 | } 87 | 88 | if (leftMotorSpeed < 0) 89 | { 90 | digitalWrite(leftMotorPin1,LOW); 91 | digitalWrite(leftMotorPin2,HIGH); 92 | } 93 | else if (leftMotorSpeed > 0) 94 | { 95 | digitalWrite(leftMotorPin1,HIGH); 96 | digitalWrite(leftMotorPin2,LOW); 97 | } 98 | else 99 | { 100 | digitalWrite(leftMotorPin1,LOW); 101 | digitalWrite(leftMotorPin2,LOW); 102 | } 103 | analogWrite(enableRightMotor, abs(rightMotorSpeed)); 104 | analogWrite(enableLeftMotor, abs(leftMotorSpeed)); 105 | } 106 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LineFollowerRobot 2 | This repository contains code and diagram for Line Follower Robot using Arduino 3 | --------------------------------------------------------------------------------