Help needed with line following algorithm

Been provided this code and I need to make it work for a robot that can ideally follow straight lines, bends and stop at junctions. Components used for line following are an array of 3 TCRT5000 IR sensors, and I am using a PCB that replaces the breadboard equivalent circuit used previously, screenshot of PCB design and IR sensor connections attached below.

(PCB design, with holes for resistors and IR sensor pins)

(IR sensor pin connections)
(top view of PCB)
(back view of PCB, with pins for analog pin connections to the Arduino and to its 5V and GND ports)

Problems: There is no change of the raw values of the IR sensors as stated in my code, they are all at 0, regardless if I bring them above a black line or white surface. "Set motor A PWM" and "Set motor B PWM" are 180 each, meaning that the robot is continuosly moving forwards, so the cases of 100, 110, 011 and others aren't being followed. The PCB has been tested for continuity, and I think it will work, but I am not sure why it isn't working. Any advice/help would be much appreciated.

Please post a wiring diagram, with all pins and connections clearly labeled, showing how the sensor is connected to the MCU. Hand drawn is preferred.

Use your multimeter to check the sensor outputs, and to verify that they see the difference between the background and the line.

If that works, write a simple program that just reports the sensor values and see what you get.

How was "threshold" in the line below chosen?

        if (sensorValue >= threshold) {
1 Like


For threshold, I understand it as the benchmark that tells me when the reflected light received is enough to consider the surface it was reflected from to be a black line, so I've just been mannually altering its value.
Dont have a multimeter on me right now, but I wrote the program like you said and there is no change at all in the values displayed on the Serial monitor after an analogread() for each sensor, as below.

int leftsensorpin = A5;
int middlesensorpin = A4;
int rightsensorpin = A3;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
analogRead(leftsensorpin);
Serial.print("Left:");
Serial.println(leftsensorpin);
analogRead(middlesensorpin);
Serial.print("Middle:");
Serial.println(middlesensorpin);
analogRead(rightsensorpin);
Serial.print("Right:");
Serial.println(rightsensorpin);
}

I get for left, middle and right sensor pins respectively: 19, 18, 17

What is in front of the sensors?

What are the readings with black cloth over the sensors?

Standard black tape for now, the readings don't change even when the sensors are brought to the white bit of the white table the black tape lies on.
Even without connection of the analog pins to the pins on the PCB, the same readings are displayed on the serial monitor.
No change to the readings when the sensors are rested on a black T-shirt.

The sensors are probably not wired correctly. Post a link to the data sheet.

they usually come with a breakout board that includes some necessary circuitry (probably handles the modulation of the infrared LED and a comparator / pot for triggering)

your simple analogRead() won't cut it...

With the code I attached in the main post, I was able to see some time ago that when the ir sensors were covered and different combinations of covering was tested, the motor A and B pwm speeds were changing, according to the cases defined in voidUpdateDirection() of the code.
Not sure why it isn't working now though, i think the pcb may have something to do with it...

the phototransistor's output signal is likely weak and may require amplification to be useful for interfacing with a microcontroller.

Probably the breakout boards includes amplification circuitry (e.g., a comparator or operational amplifier) to clean up the signal and convert it into a more usable form, such as a clear digital high or low output.

It looks like you have the two resistors swapped on each of the sensors (e.g. 100 Ohms to the phototransistor C). Please double check.

But that doesn't explain an analog reading of 19 (on an Arduino Uno or the like), which corresponds to about 0.1V on the collector of the phototransistor, meaning it is completely saturated.

Remove the board from the project, swap the resistors if necessary, power the board with 5V. and test operation with your multimeter under different lighting conditions.

Edit: if the phototransistor was fried by excessive current, then it could appear to be an approximate short circuit to ground.

Referring to


I've connected the resistors correctly have I not?

Not according to the photograph you posted. The package outline shows the top view.

Look up the resistor color codes, or measure the resistances with your multimeter.

This is a technical forum. Many members are professionals and a bit hard to fool.

I see now, I've completely jumbled up the 4.7k and 100 ohm resistors, my mistake for not double checking their resistances and trusting a single google search for their colour.

So is the wrong input resistances the reason why all IR sensors seems to be unresponsive to being shown a black line and a white surface?

The sensors will not work correctly with the resistors reversed, and it is conceivable that the phototransistors have been damaged. The LED won't be, but won't give off significant light with a 4K7 series resistor.

Please read reply #11 carefully, where I point out that the mistake does not explain your observations.

Swap the resistors and try again.

Will do and I'll update here when I do, but regarding the code, does it look okay? Last time I tested it it could detect different cases like the left ir sensor on the black line so it decreased motor B's pwm so that it could make a right turn, etc. Do you think I need to include/make any more changes to it?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.