macca-nz

Basic Arduino Line Follower

Mar 10th, 2026 (edited)
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 5.55 KB | Source Code | 0 0
  1. /*
  2.     Basic Line follow Robot as in video here https://youtu.be/zWyry58pARk
  3.     Hardware:
  4.     1 x Arduino Uno
  5.     2 x IR sensors
  6.     1 x Motor driver (L298N)
  7.     1 x Board or perspex for chassis
  8.     1 x switch to suit
  9.     1 x small breadboard or screw tag strip
  10.     1 x 2S LiPo 7.4V battery
  11.    
  12.     Misc:    
  13.     Mounting hardware
  14.     hookup wires  
  15.     double sided tape and/or hot glue
  16.  
  17.     Because we are using the L298N motor driver we use 6 x pins
  18.     Enable Left and Right are the PWM speed control pins = 2 pins
  19.     Forward Left and Right = 2 pins
  20.     Reverse Left and Right = 2 pins
  21.  
  22.     Always check your wiring and jumper settings several times to be sure
  23. */
  24.  
  25. //PWM Values - Change value to suit speed typically between 150 and 200 with the full range being 0 to 255
  26. #define PULSE_PWM     190       //a 20mS pulse of this value to overcome inertia for the slower run value
  27. #define PWM_RUN       150       //this is the run value
  28.  
  29. //Uno Pins
  30. #define LEFT_EN   10            //see note in setup
  31. #define LEFT_FWD  8
  32. #define LEFT_REV  6
  33. #define RIGHT_EN  9             //see note in setup
  34. #define RIGHT_FWD 12
  35. #define RIGHT_REV 7
  36. #define LEFT_IR   3
  37. #define RIGHT_IR  2
  38.  
  39. bool right_sensor;              //Variable holds right IR sensor state
  40. bool left_sensor;               //Variable holds left IR sensor state
  41. bool lastLeftSensor = 1;        //So we only do an action when a state changes
  42. bool lastRightSensor = 1;       //So we only do an action when a state changes
  43.  
  44.  
  45.  
  46. void setup(){
  47.   pinMode(LEFT_EN, OUTPUT);             //Left Motor PWM Pin
  48.   pinMode(LEFT_FWD, OUTPUT);             //Left Motor Forward Pin
  49.   pinMode(LEFT_REV, OUTPUT);            //Left Motor Reverse Pin
  50.   pinMode(RIGHT_EN, OUTPUT);            //Right Motor PWM Pin
  51.   pinMode(RIGHT_FWD, OUTPUT);            //Right Motor Forward Pin
  52.   pinMode(RIGHT_REV, OUTPUT);            //Right Motor Reverse Pin
  53.   pinMode(LEFT_IR, INPUT);              //IR Sensor Left
  54.   pinMode(RIGHT_IR, INPUT);              //IR Sensor Right
  55.  
  56.   /* Note PWM timers are in pairs as noted
  57.     This Direct Registar Config changes PWM from 1khz to 7.8khz carrier frequency  */
  58.   //TCCR0B = TCCR0B & B11111000 | B00000010 ;   //TIMER0 for PWM @ 7812.5 hz on D5 and D9
  59.   TCCR1B = TCCR1B & B11111000 | B00000010 ;     //TIMER1 for PWM @ 7812.5 hz on D9 and D10
  60.   //TCCR2B = TCCR2B & B11111000 | B00000010 ;   //TIMER2 for PWM @ 7812.5 hz on D3 and D11
  61.  
  62.   //Start in OFF state
  63.   digitalWrite(LEFT_EN, LOW);     //Left Motor PWM Pin
  64.   digitalWrite(LEFT_FWD, LOW);    //Left Motor Forward Pin
  65.   digitalWrite(LEFT_REV, LOW);    //Left Motor Forward Pin
  66.   digitalWrite(RIGHT_EN, LOW);     //Left Motor PWM Pin
  67.   digitalWrite(RIGHT_FWD, LOW);    //Left Motor Forward Pin
  68.   digitalWrite(RIGHT_REV, LOW);    //Left Motor Forward Pin
  69.  
  70.   delay(1000);              //Pause before loop starts
  71. }
  72.  
  73. void loop(){
  74.   left_sensor = digitalRead(LEFT_IR);         //get left IR sensor state
  75.   right_sensor = digitalRead(RIGHT_IR);       //get right IR sensor state
  76.  
  77.   //This sets up a state engine so we only drop inside if one state changes
  78.   // The "!=" is Not Equal to and the "||" is a logical OR
  79.   if(left_sensor != lastLeftSensor || right_sensor != lastRightSensor){
  80.     //Straght ahead
  81.     if (left_sensor == 0 && right_sensor == 0) {  //If true both motor forward
  82.       digitalWrite(LEFT_FWD, HIGH);               //Set Left Forward HIGH
  83.       digitalWrite(RIGHT_FWD, HIGH);              //Set Right Forward HIGH
  84.       digitalWrite(LEFT_REV, LOW);                //Left Reverse LOW
  85.       digitalWrite(RIGHT_REV, LOW);               //Right Reverse LOW
  86.       analogWrite(RIGHT_EN, PULSE_PWM);           //PWM Pulse Right Motor
  87.       analogWrite(LEFT_EN, PULSE_PWM);            //PWM Pulse Left Motor
  88.       delay(20);
  89.       analogWrite(RIGHT_EN, PWM_RUN);             //PWM Run Right Motor
  90.       analogWrite(LEFT_EN, PWM_RUN);              //PWM Run Left Motor
  91.     }
  92.     //Turn Right - Left Sensor OFF - Left REV HIGH & Right FWD HIGH
  93.     if (left_sensor == 1 && right_sensor == 0) {
  94.       digitalWrite(LEFT_FWD, LOW);                //
  95.       digitalWrite(RIGHT_FWD, HIGH);
  96.       digitalWrite(LEFT_REV, HIGH);
  97.       digitalWrite(RIGHT_REV, LOW);
  98.       analogWrite(RIGHT_EN, PULSE_PWM);           //PWM Pulse Right Motor
  99.       analogWrite(LEFT_EN, PULSE_PWM);            //PWM Pulse Left Motor
  100.       delay(20);
  101.       analogWrite(RIGHT_EN, PWM_RUN);             //PWM Run Right Motor
  102.       analogWrite(LEFT_EN, PWM_RUN);              //PWM Run Left Motor
  103.     }
  104.     //Turn Left - Right Sensor OFF - Left FWD HIGH & Right REV HIGH
  105.     if (left_sensor == 0 && right_sensor == 1) {
  106.       digitalWrite(LEFT_FWD, HIGH);
  107.       digitalWrite(RIGHT_FWD, LOW);
  108.       digitalWrite(LEFT_REV, LOW);
  109.       digitalWrite(RIGHT_REV, HIGH);
  110.       analogWrite(RIGHT_EN, PULSE_PWM);           //PWM Pulse Right Motor
  111.       analogWrite(LEFT_EN, PULSE_PWM);            //PWM Pulse Left Motor
  112.       delay(20);
  113.       analogWrite(RIGHT_EN, PWM_RUN);             //PWM Run Right Motor
  114.       analogWrite(LEFT_EN, PWM_RUN);              //PWM Run Left Motor
  115.     }
  116.     //All Stop - All motor pins set LOW
  117.     if (left_sensor == 1 && right_sensor == 1) {
  118.       digitalWrite(LEFT_FWD, LOW);
  119.       digitalWrite(RIGHT_FWD, LOW);
  120.       digitalWrite(LEFT_REV, LOW);
  121.       digitalWrite(RIGHT_REV, LOW);
  122.       digitalWrite(RIGHT_EN, LOW);
  123.       digitalWrite(LEFT_EN, LOW);  
  124.     }
  125.   }
  126.   lastRightSensor = right_sensor;       //creates the state engine for right IR sensor
  127.   lastLeftSensor  = left_sensor;        //creates the state engine for left IR sensor
  128. }
  129.  
Advertisement
Add Comment
Please, Sign In to add comment