Assistance Required on a Robot project with Arduino Uno

Hi everyone. I am currently embarking on a project with my team-mates, which requires us to build a robot on wheels which can track its way along a black strip (the path) without going off the black path. The rest of the area is white. The robot must also be able to climb up and down inclines.

We are to program the Arduino Uno on the robot to make it trace the black line path. The intended action for the robot is, if it leaves the black path completely, the wheels are supposed to reverse to put the robot back on path again, while, for minor path tracing errors, one wheel is supposed to turn faster than the other to make it shift left/right. The sensors are LDRs which give feedback to the Arduino Uno, while the LEDs supply the light source.

As this is an electrical engineering project, we used the following:

1x Breadboard
4x Light-Dependent Resistors (LDRs) with 220 Ohm fixed resistor in series
4x white LEDs with 10K Ohm fixed resistor in series
1x Arduino Uno board
1x LM324 (4 comparators in a chip like thing)
1x L293D (some H-bridge thing to drive the motors, with the ability to go forward and reverse by changing the voltage)
4x Variable Resistors (0-10K Ohms)

However, as of now, my robot has a huge problem which I am unable to diagnose. My robot kept moving in the reverse direction regardless of how I vary the resistance on the variable resistors or the LDRs by covering it (simulating black and white, i.e. low/high resistance).

I have attached my Arduino code and my project instruction sheet here for reference. The code can be found at the bottom of this long post.

I don’t think there is anything wrong with the Arduino code as it seems correct. The weird thing is that the robot functioned somewhat normally when I coded it the direct opposite manner (i.e. all the “lows” and “highs” and vice versa) but when I replaced it with the corrected program, the wheels went in reverse direction regardless of any changes.

May I know how should I go about diagnosing the error, and if possible, advise me on the possible errors? If you need a photo of my robot, feel free to tell me.

Thank you very much.

The Arduino code:

int ldrLeft1=0, ldrLeft2=1,ldrRight1=2,ldrRight2=3;
int wheelLeftFwd=13, wheelLeftBwd=12, wheelRightFwd=11, wheelRightBwd=10;
int greenLED1=9, greenLED2=8, redLED1=7, redLED2=6;

void setup() {
  // put your setup code here, to run once:
  pinMode(ldrLeft1, INPUT);
  pinMode(ldrLeft2, INPUT);
  pinMode(ldrRight1, INPUT);
  pinMode(ldrRight2, INPUT);
  pinMode(wheelLeftFwd, OUTPUT);
  pinMode(wheelLeftBwd, OUTPUT);
  pinMode(wheelRightFwd, OUTPUT);
  pinMode(wheelRightBwd, OUTPUT);
  pinMode(greenLED1, OUTPUT);
  pinMode(greenLED2, OUTPUT);
  pinMode(redLED1, OUTPUT);
  pinMode(redLED2, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if ((digitalRead(ldrLeft1) == LOW) && (digitalRead(ldrLeft2) == LOW) && (digitalRead(ldrRight1) == LOW) && (digitalRead(ldrRight2) == LOW)) {
    // all LDRs are on white, reverse
    analogWrite(wheelLeftFwd, 0);
    analogWrite(wheelLeftBwd, 128);
    analogWrite(wheelRightFwd, 0);
    analogWrite(wheelRightBwd, 128);
  }
  else if ((digitalRead(ldrLeft1) == LOW) && (digitalRead(ldrRight1) == HIGH) && (digitalRead(ldrRight2) == HIGH)) {
    // leftmost LDR is on white
    if (digitalRead(ldrLeft2) == LOW) {
      // second left LDR is on white, turn right sharply
      // right wheel reverse 50, left wheel forward 100
      analogWrite(wheelLeftBwd, 0);
      analogWrite(wheelLeftFwd, 128);
      analogWrite(wheelRightFwd, 0);
      analogWrite(wheelRightBwd, 64);
    }
    else {
      // second left LDR is on black, turn right smoothly
      // right wheel forward 0, left wheel forward 100
      analogWrite(wheelLeftBwd, 0);
      analogWrite(wheelLeftFwd, 128);
      analogWrite(wheelRightFwd, 0);
      analogWrite(wheelRightBwd, 0);
    }
  }
  
  else if ((digitalRead(ldrRight1) == LOW) && (digitalRead(ldrRight1) == HIGH) && (digitalRead(ldrRight2) == HIGH)) {
    // rightmost LDR is on white
    if (digitalRead(ldrRight2) == LOW) {
      // second right LDR is on white, turn left sharply
      // left wheel reverse 50, right wheel forward 100
      analogWrite(wheelRightBwd, 0);
      analogWrite(wheelRightFwd, 128);
      analogWrite(wheelLeftFwd, 0);
      analogWrite(wheelLeftBwd, 64);
    }
    else {
      // second right LDR is on black, turn left smoothly
      // left wheel forward 0, right wheel forward 100
      analogWrite(wheelRightBwd, 0);
      analogWrite(wheelRightFwd, 128);
      analogWrite(wheelLeftFwd, 0);
      analogWrite(wheelLeftBwd, 0);
    }
  }
  
  else {
    // all LDRs on black, proceed normally
    // both wheels forward 100
    analogWrite(wheelRightBwd, 0);
    analogWrite(wheelRightFwd, 128);
    analogWrite(wheelLeftBwd, 0);
    analogWrite(wheelLeftFwd, 128);
  }
     
}

Warmest Regards,

galaxy_twirl

Project Lab 2 - DC Motors and Sensors.doc (570 KB)

Im not sure if this is the cause to your problem, but it is something to look into.

If you're using analogWrite() for your wheels, then you might want to use the pins with the ~ symbol next to them. Pins 12 and 13 don't support PWM, so the analogWrite will only make them HIGH or LOW, and not in between.

I have had issues using analogWrite(_pin, 0) to make the pin go LOW, so you may want to just use digitalWrite(_pin, LOW) instead. (analogWrite(_pin, 0) does equal digitalWrite(_pin, LOW), but for some reason its iffy when done through analogWrite(). )

Hi HazardsMind. Thank you for your help. :slight_smile: I will feed this back to my teammate who is the programmer and we will discuss about it again.

I think my friend used analogWrite for the wheels because he wants to control the wheel speed. Somehow, despite using the same gears, one wheel will always spin faster (appreciable to the human eye) than the other, causing the robot to not stay on the black track. May I know if there is a way to solve this? I am thinking of adding a resistor by applying the formula for rotational DC motors, but I am not sure this is alright. May I have your opinions?

Thank you. :slight_smile:

Warmest Regards,

galaxy_twirl

You can use the analogWrite() function to control the speed of your motors, but to do so, you need to use the correct pins. They are the ones the have the ~ symbol next to them on the Arduino UNO.

As for your actual problem, you need to debug your sensors to see what they return when they find the line and when they don't find the line. Maybe something is obscuring them from taking a proper reading, or perhaps they are not turning on when they see the black line.

I don't know, that's why you need to do some tests.

I see. Thank you.

I will have to go to the lab to check and I will update this thread again! :slight_smile:

Just wondering, how do I debug the sensors? Do I do it through the computer or by looking at the action of the robot when I cover the LDRs or make changes to the variable resistors? This is because when the robot runs, I have to remove it from the PC. The voltage to the robot supplied through Arduino Uno is via a 7.2V (1.2V X 6) battery pack.

Thank you once again.

Warmest Regards,

galaxy_twirl

This is because when the robot runs, I have to remove it from the PC.

How big is the robot? You can just prop it up on blocks and manually feed a paper with black lines on it, by the sensors and see what they show in the serial monitor or LCD display.

Hmm. I don't have a photo of the robot with me at the moment, though I guess it is the "standard" size.

Oh ya! That's true. I can prop the robot up. But, what do I use in the computer to show the output by the sensors?

Thank you :slight_smile:

Warmest Regards,

galaxy_twirl

If you have a few bucks to spare:
Arduino Robot

Hi, use a DMM on the input pins connected to the LDR to see if you have a level that indicates white or black and is high and low enough to be recognised as a 1 or 0 by the arduino.
Can you see the level change enough, if the change is not enough to indicate 1 or 0, then use analog input pins and detect the analog levels for white and black.

I'd almost say that the sketch was written all at once and then you tried to debug, not good.
Write a sketch to get the LDR sensor inputs working and the logic of what different combinations mean.
Then write a sketch to get the motors running and steering the robot.
Then put them together.
I suggest you use separate function, or in old language subroutines, each for straight ahead , turn left, turn right and reverse.
This will help simplify and allow other reader to understand the main part of the sketch.

Tom...... :slight_smile:

To C-F-K:

C-F-K:
If you have a few bucks to spare Arduino Robot

Thanks for your suggestion but I think this will kill the fun in making the robot, along with the sense of accomplishment when my friends and I finally get the robot working. Haha. :wink:

Warmest Regards,

galaxy_twirl

To TomGeorge:

Thank you for your advice! :smiley: May I know what do you mean by using a DMM? What is a DMM?

Ah I will feedback your comment on the code to my friend who wrote the program. Meanwhile, I think we will try changing the ports as suggested by HazardsMind. :smiley:

I shall update here again~ :slight_smile:

Warmest Regards,

galaxy_twirl

Hi, a DMM is a Digital MultiMeter, if you do not have one, I would suggest you do, even an al-cheepo.
They will be good for low voltage stuff and will prove invaluable when it comes to hardware debugging.

Tom… :slight_smile:

I see. Yup, my laboratory has a DMM, so I can use that. :slight_smile:

Just a side question, if I had soldered my LDRs onto a circuit board, do I have to remove them to test for their individual resistances with a DMM? I am thinking of doing this step to check if my LDRs are working.

Thank you~ :slight_smile:

Hi, can you tell me what experience you have with electronics and programming.
Also what subject this is a project for?
What course are you doing and what level/year are you at?

Tom....... :slight_smile:

To TomGeorge:

I have no experience in electronics and programming... I only have basic A level knowledge of electronics, while for programming, I learnt it 3 months ago.

This is my project for my course, Electrical Engineering, under the course, Introduction to Circuits and Systems.

I am currently in Year 1.

General question:

Just wondering, if I connect my battery pack of 7.2V to my breadboard and the L light pulsates in 3 short bursts followed by a longer pause in a repetitive motion, what does this mean? I realised that the wheels are not turning too. I had to remove a wire to a wheel and reconnect it before the robot starts up again. However, upon stopping the wheels but covering the LDRs, the problem (flashing L lights in 3 short bursts followed by a pause) occurred again.

May I ask what is happening?

Thank you.

Warmest Regards,

galaxy_twirl

Edit:
I second Tom George. Write code that ONLY reads the sensors and ensure that works. Then write code that ONLY controls the motors and ensure that works. Do all of the above to make sure your program and apparatus is running. Your programmer should know how to do this, it is a fairly simple task.

By isolating the individual systems electronically and not physically, you save yourself the difficulty of having to solder/resolder all the parts and save time.

For your question:

Short answer:
That would be the Arduino initializing routine. The three blinks and the long one are what happen whenever you turn on/program an arduino.

Long answer:
An arduino's brain is basically a single ATmega chip, which runs a program over and over again repeatedly. The ATmega chip cannot not actually be directly be programmed C++ (the code we use to program arduinos) with a USB, which is where another chip mounted on the board comes in. This chip is a Serial to USB chip.

So when you program an arduino and hit upload, it sends the program to the Serial to USB chip. This chip converts your program into an INTERFACE the arduino's brain (ATMega) can understand and programs it. When powered on, ATmega will run its bootloader. The light pattern you observed is triggered right here, which indicates the bootloader is running. If the bootloader receives any code from the chip, it will save it and the ATMega will run that. If not, it will run the last program which is saved in the ATMega's memory. Since the bootloader only happens at power on, the chip will force the arduino to reset itself, in order for it to receive any program via the computer usb port. This is why you will observe the same pattern when uploading code and when powering on the device externally.

Goodluck!
Stef

*I corrected this post after AWOL politely pointed out to me I made some mistakes

StefL:
When powered on, the bootloader will take the last program it recieved and translate it into a language the arduino's brain (ATMega) can understand and uploads it.

Short answer:
Utter nonsense.

Hi,
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

Thanks, Tom....... :slight_smile:

I reread some reference and I've fixed my misunderstanding.

AWOL I know you're a big shot brattain global mod but I doubt that gives you leave to make cocky, supercilious remarks. In fact I thought being a mod meant your job is to STOP the people who leave cocky snide remarks.

Aren't we all here to learn and help each other?

and by the way edited and corrected my post.

Hi StefL. Thank you for taking your time to write this post in order for me to understand what is going on. :slight_smile: The three repeated flashes on L were actually due to the battery pack not supplying enough power to the Arduino board. The problem was resolved when my friend changed a new battery pack. :slight_smile:

UPDATES on progress:

Our robot is finally able to move on it own somewhat correctly, after I made a new PCB with the necessary components on it, thus eliminating the hardware issues. We also changed a couple of 10K Ohm trimmers and some L324M (or L342N, I can't remember) comparator chips. We also calibrated the sensitivity of the LDRs according to the track. We did a couple of changes to the Arduino code so everything works fine now.

EXCEPT

One problem. :frowning: My robot is still rather unstable in the sense that it does not track the black track accurately at times, especially at 90 degree bends, where it may get 'stuck' and start to move back and forth without any progress. And it was painfully slow when it climbed upslope, as it moved left and right, instead of going straight up. May I have some advice on these issues?

Thank you! :smiley:

Hi, can you post a diagram of how you have your sensors layed out and PLEASE a copy of your circuit.
LM324 are probably the worst IC's you could use as compartors.
LM339 are far superior and designed for single ended supply.

Tom....... :slight_smile: