├── README.md ├── line follower image.jpg ├── line_follower_PID.ino ├── pid block diagram.png └── pid image.png /README.md: -------------------------------------------------------------------------------- 1 | # arduino-PID-line-follower 2 | This open source codes are to control arduino based line following robot using PID algorithm 3 | -------------------------------------------------------------------------------- /line follower image.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VikieVik/arduino-PID-line-follower/dd78fd7ea890a8abb2a8dcb11333f10dc8827be4/line follower image.jpg -------------------------------------------------------------------------------- /line_follower_PID.ino: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | float Kp=0,Ki=0,Kd=0;//change the value of kp ,ki and kd factors randomly and find a set of these value wich works good for your robot 5 | float error=0, P=0, I=0, D=0, PID_value=0;//defining the intial value 0 6 | float previous_error=0, previous_I=0;//defining initially values of previous_error and previous_I 0 7 | int sensor[5]={0, 0, 0, 0, 0};//defining the sensor arrey of 5 8 | int initial_motor_speed=100;//defining the initial value of the motor speed as 100,can be changed 9 | 10 | void read_sensor_values(void);//function that reads sensor values 11 | void calculate_pid(void);//function that caluculates the pid value 12 | void motor_control(void);//function that perform motor control action 13 | 14 | void setup() 15 | { 16 | pinMode(9,OUTPUT); //PWM Pin 1 ( first PWM pin on your motor driver that is connected to arduino pwm pin) 17 | pinMode(10,OUTPUT); //PWM Pin 2 (second PWM pin of your motor driver that is connected to arduino pwm pin ) 18 | pinMode(4,OUTPUT); //Left Motor Pin 1(connected on arduino pin4) 19 | pinMode(5,OUTPUT); //Left Motor Pin 2(connected on arduino pin5) 20 | pinMode(6,OUTPUT); //Right Motor Pin 1(connected on arduino pin6) 21 | pinMode(7,OUTPUT); //Right Motor Pin 2(connected on arduino pin7) 22 | Serial.begin(9600); //Enable Serial Communications 23 | } 24 | 25 | void loop() 26 | { 27 | read_sensor_values();//sensor data is read 28 | calculate_pid();// pid is calculated 29 | motor_control();//motor speed is controlled 30 | } 31 | 32 | void read_sensor_values() 33 | { 34 | sensor[0]=digitalRead(A0);//sensor data read from A0 arduino pin 35 | sensor[1]=digitalRead(A1);//sensor data read from A1 arduino pin 36 | sensor[2]=digitalRead(A2);//sensor data read from A2 arduino pin 37 | sensor[3]=digitalRead(A3);//sensor data read from A3 arduino pin 38 | sensor[4]=digitalRead(A4);//sensor data read from A4 arduino pin 39 | 40 | if((sensor[0]==0)&&(sensor[1]==0)&&(sensor[2]==0)&&(sensor[4]==0)&&(sensor[4]==1)) 41 | error=4; 42 | else if((sensor[0]==0)&&(sensor[1]==0)&&(sensor[2]==0)&&(sensor[4]==1)&&(sensor[4]==1)) 43 | error=3; 44 | else if((sensor[0]==0)&&(sensor[1]==0)&&(sensor[2]==0)&&(sensor[4]==1)&&(sensor[4]==0)) 45 | error=2; 46 | else if((sensor[0]==0)&&(sensor[1]==0)&&(sensor[2]==1)&&(sensor[4]==1)&&(sensor[4]==0)) 47 | error=1; 48 | else if((sensor[0]==0)&&(sensor[1]==0)&&(sensor[2]==1)&&(sensor[4]==0)&&(sensor[4]==0)) 49 | error=0; 50 | else if((sensor[0]==0)&&(sensor[1]==1)&&(sensor[2]==1)&&(sensor[4]==0)&&(sensor[4]==0)) 51 | error=-1; 52 | else if((sensor[0]==0)&&(sensor[1]==1)&&(sensor[2]==0)&&(sensor[4]==0)&&(sensor[4]==0)) 53 | error=-2; 54 | else if((sensor[0]==1)&&(sensor[1]==1)&&(sensor[2]==0)&&(sensor[4]==0)&&(sensor[4]==0)) 55 | error=-3; 56 | else if((sensor[0]==1)&&(sensor[1]==0)&&(sensor[2]==0)&&(sensor[4]==0)&&(sensor[4]==0)) 57 | error=-4; 58 | else if((sensor[0]==0)&&(sensor[1]==0)&&(sensor[2]==0)&&(sensor[4]==0)&&(sensor[4]==0)) 59 | if(error==-4) error=-5; 60 | else error=5; 61 | 62 | } 63 | 64 | void calculate_pid()//calculating pid 65 | { 66 | P = error; 67 | I = I + previous_I; 68 | D = error-previous_error; 69 | 70 | PID_value = (Kp*P) + (Ki*I) + (Kd*D); 71 | 72 | previous_I=I; 73 | previous_error=error; 74 | } 75 | 76 | void motor_control()//motor control 77 | { 78 | // Calculating the effective motor speed: 79 | int left_motor_speed = initial_motor_speed-PID_value; 80 | int right_motor_speed = initial_motor_speed+PID_value; 81 | 82 | // The motor speed should not exceed the max PWM value 83 | constrain(left_motor_speed,0,255); 84 | constrain(right_motor_speed,0,255); 85 | 86 | analogWrite(9,initial_motor_speed-PID_value); //Left Motor Speed 87 | analogWrite(10,initial_motor_speed+PID_value); //Right Motor Speed 88 | //following lines of code are to make the bot move forward 89 | /*The pin numbers and high, low values might be different 90 | depending on your connections */ 91 | digitalWrite(4,HIGH); 92 | digitalWrite(5,LOW); 93 | digitalWrite(6,LOW); 94 | digitalWrite(7,HIGH); 95 | } 96 | 97 | -------------------------------------------------------------------------------- /pid block diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VikieVik/arduino-PID-line-follower/dd78fd7ea890a8abb2a8dcb11333f10dc8827be4/pid block diagram.png -------------------------------------------------------------------------------- /pid image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VikieVik/arduino-PID-line-follower/dd78fd7ea890a8abb2a8dcb11333f10dc8827be4/pid image.png --------------------------------------------------------------------------------