View Full Version : Line following sensor

09-29-2008, 10:25 AM
I just bought the the QRB1134, it consists of an infrared emitting diode and a NPN silicon phototransistor mounted side by side.· Has anyone ever used this thing on the boe-bot and if so would you be able to help me write a program to get it to distinguish between a black and white surface?

I've attached the data sheet.


Mike Green
09-29-2008, 11:33 AM
The Scribbler uses an IR LED and IR phototransistor for line following (www.parallax.com/Store/Robots/RollingRobots/tabid/128/CategoryID/3/List/0/SortField/0/Level/a/ProductID/323/Default.aspx (http://www.parallax.com/Store/Robots/RollingRobots/tabid/128/CategoryID/3/List/0/SortField/0/Level/a/ProductID/323/Default.aspx)). Have a look at the software linked from the webstore page.

There's a line follower kit for the BotBot that uses a similar sensor that includes some additional parts that you can add to your sensor yourself (www.parallax.com/Store/Robots/RoboticAccessories/tabid/145/CategoryID/3/List/0/SortField/0/Level/a/ProductID/77/Default.aspx (http://www.parallax.com/Store/Robots/RoboticAccessories/tabid/145/CategoryID/3/List/0/SortField/0/Level/a/ProductID/77/Default.aspx)).Have a look at the documentation and sample programs. Some of the sample programs don't need all the extra parts. A 470 Ohm current limiting resistor for the LED and a 10K pullup for the phototransistor are the minimum additional parts that you would need.

Post Edited (Mike Green) : 9/29/2008 4:40:06 AM GMT

09-30-2008, 09:16 AM
Thanks Mike,

I was able to wire the sensor and get a reading of 1 or 0 between white and black surfaces. I now want to be able to use the sensor as a counter. For example, to be able to move it across 7 repeating black and white lines and have it return the number 7 as sort of a position holder. I have a basic idea of the concept to get it to work but can you please guide me through the pbasic code.

Mike Green
09-30-2008, 10:53 AM
How about if you give it a try and see what happens. As a hint ... you have two states. One is when the sensor reads white and the other is when the sensor reads black. Your program goes from one state (a loop) to another when the sensor has the "wrong" value for the state. What state transition do you want to count?

09-30-2008, 07:31 PM
I would have it count each time it went from black to white.· Is there an example·of pbasic code that does this so I can see what the syntax is?

Mike Green
09-30-2008, 08:07 PM
There are plenty of examples of similar things for the Scribbler and BotBot line follower add-on that I mentioned. If you haven't done it already, work through the examples in "What's a Microcontroller?" and the "Robotics with the BoeBot" tutorials which you can download from Parallax.

09-30-2008, 09:11 PM
One more question. How do you calculate that I need a 470 Ohm current limiting resistor for the LED on the sensor?

Bruce Bates
09-30-2008, 09:42 PM
vla7 -

Here is an LED calculator you can use if you find that suitable:


Bruce Bates

When all else fails, try inserting a new battery.

10-01-2008, 08:56 AM
I just discovered the COUNT command. From what I understand:
Pin = I/O pin that I use
Duration = lengh of time that I want a count in milliseconds
Variable = where the count will be stored

So if I set duration for 5000 and move my sensor back and forth from a white surface to black back to white 7 times within the 10 seconds, then I should have a value of 7 stored in variable.

This is my test program. I'm using input pin 1

cycles Var Word
COUNT 1, 5000, file
"Cycles " , DEC cycles

I keep getting a very high number for cycles. For example I'll go through three cycles and get a number like 19.

10-01-2008, 08:57 AM
I meant 5 seconds in the above comment, not 10.

Mike Green
10-01-2008, 09:56 AM
COUNT responds very quickly to pulses. If you were to look at the pulses from your sensor, you'd find that there are very short pulses near the edges of the stripes as you move the sensor across them. These probably result from irregularities in the stripes and maybe a slight tremor in your movement of the sensor. To use COUNT accurately, you'd have to use an RC filter to slow down the transitions and use a Schmitt trigger input gate as a buffer to give a crisp pulse from the slowed down/smoothed sensor pulses.

The advantages of using multiple Basic statements (like IF/GOTO) and multiple states is that this is slower. Instead of counting pulses as short as a couple of microseconds, the multiple statements take several hundred microseconds on a BS2 to execute avoiding this "false" triggering.

10-02-2008, 12:22 AM
Ok, this program seems to work. The only issue is that when I manually move the sensor back and forth across a black stripe on a sheet of white paper it occasionaly will jump two cycles with only one movement across the the black stripe. Is this affected by the lighting in the room? Would attaching an LED to the sensor to give more light give me more accurate results? Aslo, is my program the way to go about this or is there a much more efficient way of doing this?

' {$STAMP BS2}
' {$PBASIC 2.5}

cycle VAR Word

GOTO checktwo
GOTO checkone

GOTO checkthree
GOTO checktwo

cycle = cycle + 1
GOTO checkthree

DEBUG CRSRXY, 3,2, "Cycle is ", DEC cycle



Mike Green
10-02-2008, 12:30 AM
Your "glitches" probably have more to do with the DEBUG statements than anything else.
DEBUG statements take time to execute. At least 2 or 3 characters have to be sent and they
take about 1ms each to send. Try commenting out or removing the checkone/checktwo/checkthree
DEBUG statements and see what happens.

You can leave out the ELSE statements in checkone/checktwo/checkthree. You can also combine the
IF and GOTO statement that follows it and leave out the ENDIF statements. In checkthree, you can
similarly combine the IF and cycle = cycle + 1. Look in the Basic manual for examples.

10-03-2008, 10:15 PM
Below is an option, but it is a very dedicated option, you wouldnt be able to do much else other than count pulses with it. But it might be good for an idea. You could replace the outer do-loop with a for-next so that you could control how many cycles it ran for. If you have a BS2p you can use the POLLIN command to psuedo-interrupt and start the counting.


do while in1 = 0 ' wait until in1 is one

cycle = cycle+1

do while in1=1 ' hold here until in1 goes back to 0 before proceeding

loop ' go back and wait for in1 =1 again

Post Edited (GICU812) : 10/3/2008 3:21:41 PM GMT