element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Does anyone know anything about making a line follower with Arduino?
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 0 replies
  • Subscribers 400 subscribers
  • Views 208 views
  • Users 0 members are here
  • electronic
  • robotics
  • robots
  • program
  • programming
  • electronics
  • arduino
Related

Does anyone know anything about making a line follower with Arduino?

Former Member
Former Member over 11 years ago

My friend Michael and I are making a line following robot as a home project. but when it is running it wont follow the line it will just keep going. i have the sensors on a test board and connected to the arduino using a arduino motor shield with the sensor reads in analogs 0-2 and the leds on digital pin 7. then the motors on digital pins 3,11,12,13.

 

this is the code we are using:

 

//Home project 1 : Line following robot

#define QUARTER_SPEED 64 //defining speeds
#define HALF_SPEED 128
#define FULL_SPEED 255
#define BACKWORD -128

int LDRleft, LDRcenter, LDRright;  //sensor values
int rotate = 60;
int leftOffset = 0, centerOffset = 0, rightOffset = 0;
int speedleft = 3;   //speed for left side of robot
int speedright = 11;  //speed for right side of robot
int directionleft = 12;  //left motor(s) forword,reverse
int directionright = 13;  //right motor(s) forword,reverse
int threshhold = 5; //sensor threshold


// sensor calibration routine
void calibrate()
{
  for (int x=0; x<10; x++) // runs loop 10 times
  { 
    digitalWrite(7, HIGH);
    delay(100);
    LDRleft = analogRead(A0);  //reads the 3 sensors
    LDRcenter = analogRead(A1);
    LDRright = analogRead(A2);
    leftOffset = leftOffset + LDRleft;
    centerOffset = centerOffset + LDRcenter;
    rightOffset = rightOffset + LDRright;
   
    delay(100);
    digitalWrite(7, LOW);
    delay(100);
  }
  //obtain average
  leftOffset = leftOffset / 10;
  centerOffset = centerOffset / 10;
  rightOffset = rightOffset / 10;
  //calculate offsets for left and right sensors
  leftOffset = centerOffset - leftOffset;
  rightOffset = centerOffset - rightOffset;
}

void setup()
{
  pinMode(7, OUTPUT); //has to be pin 7, pin 9 is a break for motors
  pinMode(speedleft, OUTPUT);
  pinMode(speedright, OUTPUT);
  pinMode(directionleft, OUTPUT);
  pinMode(directionright, OUTPUT);
  // calibrate the sensors
  calibrate();
  delay(4000);
 
 
  digitalWrite(7, HIGH);  //lights on
  delay(100);
 
  //setting motors to forward
  digitalWrite(directionleft, HIGH);
  digitalWrite(directionright, LOW);
  //setting speeds of motors
  analogWrite(speedleft, HALF_SPEED);
  analogWrite(speedright, HALF_SPEED);
  delay(100);
}
  void loop()
  {
    //make motors go forword
    digitalWrite(directionleft, HIGH);
    digitalWrite(directionright, LOW);
    analogWrite(speedleft, HALF_SPEED);
    analogWrite(speedright, HALF_SPEED);
   
    //read the sensors and add the offsets
    LDRleft = analogRead(A0) + leftOffset;
    LDRcenter = analogRead(A1);
    LDRright = analogRead(A2) + rightOffset;
   
   
    // if LDRleft is greater than the center sensor + threshold turn right
    if (LDRleft > (LDRcenter+threshhold)) {
      speedleft = HALF_SPEED + rotate;
      speedright = HALF_SPEED - rotate;
    }
   
    //if LDRright is greater than the center sensor+threshold turn left
    if (LDRright > (LDRcenter+threshhold)) {
      speedleft = HALF_SPEED - rotate;
      speedright = HALF_SPEED + rotate;
    }
   
  }

 

 

and when i run this the robot doesn't work. can anyone help me with what may be wrong and if you can please show us the errors in it so we can learn off of it. and if you can message me what the code should look like can you please message it to me to try on the robot. so is my problem in my wiring or is there an apparent problem in my code.

  • Sign in to reply
  • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube