Line following robot using arduino uno

Hi, I am a student doing my first year now i am working on my project. My project is line following robot using arduino uno with l293d motor driver. My problem is my robot is not sensing black line on floor but it sensing when i keep something black near to the sensor i don't know why. i tried my best by changing the motor speed in code and by callibrating the sensor to a point of white surface = 1023, black surface = 33. please help me to complete my project.
I refered this project in youtube channel.
My code
"' #define Motor11 7
#define Motor12 6
#define Motor21 9
#define Motor22 8
#define PWMmotor1 5
#define PWMmotor2 10

int valuePWM1=120; // speed motor 1
int valuePWM2=150; // speed motor 2

void setup() {

pinMode(Motor11,OUTPUT);
pinMode(Motor12,OUTPUT);
pinMode(Motor21,OUTPUT);
pinMode(Motor22,OUTPUT);
pinMode(PWMmotor1,OUTPUT);
pinMode(PWMmotor2,OUTPUT);

pinMode(A0, INPUT); // initialize Right sensor as an inut
pinMode(A1, INPUT); // initialize Left sensor as as input

}

void loop() {

int LEFT_SENSOR = analogRead(A0);
int RIGHT_SENSOR = analogRead(A1);

if(RIGHT_SENSOR<36 && LEFT_SENSOR<36) //FORWARD
{
digitalWrite(Motor11, HIGH);
digitalWrite(Motor12, LOW);
digitalWrite(Motor21, HIGH);
digitalWrite(Motor22, LOW);
analogWrite(PWMmotor1, valuePWM1);
analogWrite(PWMmotor2, valuePWM1);
}

else if(RIGHT_SENSOR>36 && LEFT_SENSOR<36) //LEFT
{
digitalWrite(Motor11, LOW);
digitalWrite(Motor12, HIGH);
digitalWrite(Motor21, HIGH);
digitalWrite(Motor22, LOW);
analogWrite(PWMmotor1, valuePWM2);
analogWrite(PWMmotor2, valuePWM2);
}

else if(RIGHT_SENSOR<36 && LEFT_SENSOR>35) //RIGHT
{
digitalWrite(Motor11, HIGH);
digitalWrite(Motor12, LOW);
digitalWrite(Motor21, LOW);
digitalWrite(Motor22, HIGH);
analogWrite(PWMmotor1, valuePWM2);
analogWrite(PWMmotor2, valuePWM2);
}

else if(RIGHT_SENSOR>35 && LEFT_SENSOR>35) //BACK
{
digitalWrite(Motor11, LOW);
digitalWrite(Motor12, LOW);
digitalWrite(Motor21, LOW);
digitalWrite(Motor22, LOW);
delay(10000);
}
}"'

channel website:https://forbiddenbit.com/en/arduino-projects/line-flower-arduino

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Run the robot and look at the values to troubleshoot:

void loop() {

int LEFT_SENSOR = analogRead(A0);
int RIGHT_SENSOR = analogRead(A1);

Serial.print(LEFT_SENSOR);
Serial.print(' ');
Serial.println(RIGHT_SENSOR);

Don't forget to run the begin method on Serial, so you don't complain that serial doesn't work...

#define Motor11 7
#define Motor12 6
#define Motor21 9
#define Motor22 8
#define PWMmotor1 5
#define PWMmotor2 10

int valuePWM1 = 120; // speed motor 1
int valuePWM2 = 150; // speed motor 2

void setup() {
  Serial.begin(115200);

  pinMode(Motor11, OUTPUT);
  pinMode(Motor12, OUTPUT);
  pinMode(Motor21, OUTPUT);
  pinMode(Motor22, OUTPUT);
  pinMode(PWMmotor1, OUTPUT);
  pinMode(PWMmotor2, OUTPUT);

  pinMode(A0, INPUT); // initialize Right sensor as an inut
  pinMode(A1, INPUT); // initialize Left sensor as as input
}

void loop() {
  int LEFT_SENSOR = analogRead(A0);
  int RIGHT_SENSOR = analogRead(A1);
  Serial.println(String(LEFT_SENSOR) + "  " + String(RIGHT_SENSOR));
  delay(20);
}

Oh, you like to use the String class for no important reason...

void loop() {
  Serial.print(analogRead(A0));
  Serial.print("  ");
  Serial.println( analogRead(A1));
  delay(20);
}

Or even...

#include <Streaming.h>
...
{
Serial << analogRead(A0) << "   " << analogRead(A1) << endl ;

Oh, you like to use the Streaming.h for no important reason... :wink:

But it won't cause memory fragmentation. :slight_smile: It's a much more common and standard way to do terminal I/O in C++.

good to know. thanks.


Serial << analogRead(A0) << F("  ") << analogRead(A1) << endl ;

Hi, @nalanahathavan
Welcome to the forum.

Can you please post some images of your project?
This will let us see your component layout.

Those sensors do not detect the black line, they detect the IR from the emitter reflecting back of a surface.
If the is little or no reflection, like a black object, the response will be low.

What is the distance between the sensor assembly and the road surface.
Is it like in the link you posted?
Untitled2

Tom... :grinning: :+1: :coffee: :australia:

No I just used in code for hightling sorry for that.

Thank u for your reply. I used 1.6cm black tape and i keep the sensor in chassis according to that dimension.

I have one doubt is white surface must be pure white??

Hi,
Have you adjusted the trim pots on the sensor PCBs?

They will adjust the level that Black and White output changes.
Here is the schematic, the circuit is configured as a comparator.

The originator of that link, says:

The sensor measures the amount of reflected light and sends the value to the arduino. There is a potentiometer on the sensor, with which we can adjust the sensitivity of the sensor.

The output is either HIGH or LOW, not an analog value.

Tom.... :grinning: :+1: :coffee: :australia:

yes i did it is working properly when i keep black object near the sensor it works but it doesn't work on track. what can i do??

Hi,
Did you try @kolaha code, and monitor the IDE monitor screen and see what values you are getting from the sensors when they are in postion.

Tom.... :grinning: :+1: :coffee: :australia:

incorrect. LM358 is an OP amplifier not comparator.

Well, my knowledge of electronics is not enough to discuss amplifiers or comparators, but I know there are sensors with both analogic and digital outputs like this one (that I´m currently using on a line follower):

But the one that it´s pointed on the project seems to be digital. If so, a better approach is to digital.Read() HIGH or LOW signal and use the trimpot to adjust the sensibility of the sensor acording to the distance that it is from the floor. Just place the sensor where it will be and calibrate it with the trimpot (it´s very sensitive. So, move it slowly).

See that OP mentions that he could read correctly if the sensor is close to the line.

If I´m writing nonsense and the sensor can be analog.Read(), it would be nice to change "if" conditions threshold. Considering that white is 1023 and black is 33, there´s no need to use 35 or 36. Using 200 will be enough.

And yes, the color of the floor and ambient light level will influence the measurements.

i found one picture of such module in web just like OP shows. it is based on LM393(differential comparator). that means we don't know what OP got.
but it is correct that if analog module is adjusted (e.g. black 0.5V and white 3V), it can be attached to digital input pin.