Arduino line maze solver

Hi Everyone,

I am trying to make an arduino line maze solver robot. I am using an arduino UNO, a L298N and a 5 IR sensor array. I am using analogWrite(125) for both enable pins to reduce the speed of the robot. I am using 125 because below this the motors dont move and create a buzzing sound. The problem I am facing is that the robot's motor's response time is problematic. Some times one motor responds first, sometimes the the other motor is responding first even when both are expected to respond simultaneously. I am facing a similar delay while stopping the motors. although this time they respond together but after some delay (about 0.5-0.7 second) causing the robot to go off track. Please help me solve this issue.

Thanks

Have you tried kicking the motors with a higher PWM duty-cycle, and then reducing it after a short (100ms?) period?

1 Like

To get both motors to respond at the same time use a higher value.

To help you we would have to see your code and schematic.

I forgot to tell that I am using a 7.5 Volts 2 cell battery (contains to cells of 3.75 V each). It is connected to the arduino via the DC Jack. Vin and ground from arduino connect to the power supply pins of L298N. Rest of the schematics is clear from the code.

Here's the code that I am using. I know its incomplete but this is what I have done till now and I am trying to test basic turns using this code first. I have added stop_robot() and delay at every turn for debugging purposes because I cannot keep my computer connected to the robot while I am running it.

// Motor A connections
#define enA 10
#define in1 A0
#define in2 A1
// Motor B connections
#define enB 11
#define in3 A2
#define in4 A3

//Sensor Pins
#define s1 5
#define s2 6
#define s3 7
#define s4 8
#define s5 9



void setup() {
  // Set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  // Turn off motors - Initial state
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);


  // Start the Serial Communication
  Serial.begin(9600);

  // Set all the IR Sensor Pins as inputs
  pinMode(s1, INPUT);
  pinMode(s2, INPUT);
  pinMode(s3, INPUT);
  pinMode(s4, INPUT);
  pinMode(s5, INPUT);

  //Reduce the speed of both the motors
  analogWrite(enA, 125);
  analogWrite(enB, 125);
}

void loop() {

  int value_1 = digitalRead(s1);
  int value_2 = digitalRead(s2);
  int value_3 = digitalRead(s3);
  int value_4 = digitalRead(s4);
  int value_5 = digitalRead(s5);

  if ((value_1 == HIGH) && (value_2 == HIGH) && (value_3 == LOW) && (value_4 == HIGH) && (value_5 == HIGH)) { //Line is in center
    forward();
    Serial.println("Going Forward");
  }  else if ((value_1 == LOW) && (value_2 == LOW) && (value_3 == LOW) && (value_4 == HIGH) && (value_5 == HIGH)) { //Line is towards Left
    stop_robot();
    delay(3000);
    left();
    Serial.println("Turning Left");
    while (! ((digitalRead(s1) == HIGH) && (digitalRead(s2) == HIGH) && (digitalRead(s3) == LOW) && (digitalRead(s4) == HIGH) && (digitalRead(s5) == HIGH))) {}
    stop_robot();
  } else if ((value_1 == HIGH) && (value_2 == HIGH) && (value_3 == LOW) && (value_4 == LOW) && (value_5 == LOW)) { //Line is towards Right
    stop_robot();
    Serial.println("Let me Check!");
    delay(10000);
    check();
  } else if ((value_1 == LOW) && (value_2 == LOW) && (value_3 == LOW) && (value_4 == LOW) && (value_5 == LOW)) { //T Point
    stop_robot();
    Serial.println("T Point! Going Left");
    delay(6000);
    left();
    delay(500);
  } else if ((value_1 == HIGH) && (value_2 == HIGH) && (value_3 == HIGH) && (value_4 == HIGH) && (value_5 == HIGH)) { //Dead End!
    Serial.println("Dead End! Taking a U-Turn");
    u_turn();
  }
}



void check() {
  forward();
  Serial.println("Going forward to check");
  delay(200);
  stop_robot();
  if ((digitalRead(s1) == HIGH) && (digitalRead(s2) == HIGH) && (digitalRead(s3) == LOW) && (digitalRead(s4) == HIGH) && (digitalRead(s5) == HIGH)) { //Forward Option Available
    forward();
    Serial.println("Going Forward");

  } else if ((digitalRead(s1) == HIGH) && (digitalRead(s2) == HIGH) && (digitalRead(s3) == HIGH) && (digitalRead(s4) == HIGH) && (digitalRead(s5) == HIGH)) {//Right Only
    right();
    Serial.println("Turning Right");

  } else if ((digitalRead(s1) == LOW) && (digitalRead(s2) == LOW) && (digitalRead(s3) == LOW) && (digitalRead(s4) == LOW) && (digitalRead(s5) == LOW)) { //FINISH
    Serial.println("FINISH! I have completed the Maze");
    while (true) {}
  }


}


// Function to go forward
void forward() {
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
}

// Function to go backward
void backward() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}


// Function to go Right
void right() {
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  while (digitalRead(s3) != LOW) {}

}

// Function to go Left
void left() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
}


// Function to stop
void stop_robot() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}

// Function to take a U-Turn
void u_turn() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);

  while (digitalRead(s3) != LOW) {}
  stop_robot();
}

Thanks

Thanks for that. But code is only half of it. In order to understand what you have done we need to see the schematic.

Here's the schematic diagram that I have made. The only difference is that I am using a 5 sensor array instead of 5 individual sensors (the array part was not available in the schematic software) It does not have any potentiometer and I have a 7.4V DC battery connected through the DC jack of arduino UNO.

Thanks

Sorry but that is a physical layout diagram NOT a schematic. Technically it shows you very little.

So you should show this.

This is why we hate that Fritzing crap here.

A schematic would show the name of each board, and the name of each connector on each board. As it is this is just a blury mess, and you can't see any detail.
It would also help if you could post more information about this IR sensor array, like a link to a data sheet or where you got it from. The same applies to that motor board.

A L298 based board is a very poor choice for working with such a low voltage. This is because that chip uses transistors to switch and you loose so much voltage across it when it switches. Have you measured the voltage on the motor when it is running?

You may want to read this before you proceed:-
how to get the best out of this forum

Here's the sensor array link from where i got it:

Here's the L298N Module that I am using:

It comes to around 3 Volts when I am running it with analogWrite(125) and battery fully charged(7.55V). I want to lower it further but motors stop running if I take value below 125.

I am using wires for my connections shown in the image below.

The sensor array works perfectly fine and correctly detects the line(indicated by LED's onboard the array). But the problem is that due to uneven response time of the motors the robot doesn't go straight and when it has to stop, by the time the motors stop, the robot has already gone off track.

Please help
Thanks

OK, in order to turn off the motors quickly you need to not only have the two direction pins at the same level but you also need to set the PWM enable to high. This produces the fast stopping mode where the current generated from a moving unpowered motor is directed back into the motor coils. This is known as flywheel breaking.
If the PWM enable is set to low this will simply let the motor come to a free running stop.
With your code you are half way between those two as you are still applying a PWM signal, so you are not stopping as fast as you could.

1 Like

Thanks a lot for your help.
I used analogWrite(200) before stopping and the robot is stopping instantly.

This is why I like asking my problems on the arduino forum. Apart from getting help about my problem, I always learn something new.

But one problem still remains is that it drifts to one side (sometimes left, sometimes right, and sometimes no drift at all) while going forward.
Please help
Thanks

I am afraid that this is the nature of this kind of robot. They don’t travel in a straight line. The way you get round it is to put rotary encoders on the wheels and make sure that they make the same number of pulses. Even so you can get wheel slip on one wheel or other.

It goes straight once it starts. It doesn't appear to have any difference in rpm of the motors. The problem is that the response time of the motors is causing it to drift in another direction. Sometimes one motor responds first, sometimes the other and sometimes both respond together. The difference in response time is not much(about 0.5 second) but it causes it to change the direction from the very start. Although the robot goes in a straight line once it starts moving.

Please help
Thanks

So have you experimented with increasing the PWM before starting the motors?

You are unfeasibly lucky then - a lottery ticket might be a good idea :wink:

There's too much variation in motor manufacture and wheels and the surface you're running on to expect your robot to run straight for long. You really need some other sensors to help keep things running straight.

Hi,
Can you please post some images of your creation?

Why have you got delays as long as 6seconds in your code?
When you use delay(), you STOP all code until the time is up.
This means a 6second delay will make your robot move in the one direction for 6seconds and NOT respond to any inputs to make it change direction.

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

It does have some variation when I run it for long. But it isn't supposed to run for long in a straight line.(It is solving a maze! :smiley: ) I have a 5 feet by 5 feet maze and it has quite a number of turns. The longest it has to go straight is somewhere about 3 feet and deviation is almost invisible at that distance.

Yes I have tries that many times with different values of increased PWM. I have tried even putting them on full power ( by digitalWriting enable pins as high) but no success.

I had put those long delays to debug that whether it is correctly detecting the intersections ( I cannot keep the robot connected to the computer while I run it). I programmed it to stop at every intersection for some seconds (for now).

I place it at the start (which is at one edge with the finish at opposite edge) and as soon as I switch it on, it goes goes off track before reaching any intersection due to difference in response times of the motors.

Please help
Thanks

So what current capacity does your battery have? Starting two motors takes quite a current surge are you sure you can supply that without the voltage dropping?

To find the initial stall current measure the resistance of the motor windings and use ohms law to find the current from this and the voltage you measure across the motor when it is running.

I found out that the resistance is 5 ohms for both the motors and they work on 1.1 volts. So applying the ohms law the current required will be 0.22 Amps or 220 mA for each motor totaling to 440 mA for both motors.

My battery has 2 cells of 3.7 V , 2200 mah each which means i have a battery of 7.4 V , 4400mah. I guess it can easily supply this current.

Please help

Thanks

No. If you have two cells in series that will give you 7.4V with a. 2200mAH capacity. You only ever double the capacity when wiring them in parallel or double the voltage by wiring them in series.

Any way the capacity in mAH is not an indication of the current you can draw, that is determined by the C rating of the battery.

How do you manage to only give them 1.1V from a battery of 7.4V? Do you use some sort of regulator?

Or have you measured the voltage across the battery when it is turned on? The symptom of trying to draw more current from a battery than it will supply is for the voltage from that battery to drop.

Uhmm... Where is the other voltage going?... Do you have problems in the connections?

Note: A Li-ion battery has a full charge voltage of 4.2V and 3.2V when it has to be recharged.
Then, nominal voltage is (4.2+3.2)/2=3,7V.
Someone writes 3.6V considering a minimum voltage of 3.0V: (4.2+3.0)/2=3.6V.