Single Line Sensor

In this setup, a single light sensor is positioned on the robot and programmed to follow the outer-edge of a black line. We commonly refer to the robot executing a ‘Z’ pattern while following the black line.

How it Works:

  1. the light sensor sees black –> it turn slightly away from the line
  2. the light sensor sees white (not black) –> the robot turns slightly back towards the line

The robot is making a ‘Z’ pattern – turning towards line, away form line etc etc

Single Sensor Line Follower Sample Code

Things to note:

  1. A Forever loop is used to repeatedly get sensor readings and apply the appropriate steering correction to the wheels, as fast as possible. The loop will repeat several times per second.
  2. Inside the loop, we repeatedly check the light sensor and then switch based on wether we detect black or white. We typical set the sensor to detect black and the sensitivity of the sensor to about 35% (real black). Then if we detect black we turn one way, and when detecting white the opposite way.
  3. Motor block duration is set to Unlimited because we just want to start moving that way then immediately loop back and check the sensor again to see if we need to change direction (Unlimited here means “Go this way until I tell you otherwise”).

You can control the shape of the detecting “Z” in a variety of ways.  How about rather then one motor breaking it driving in opposite direction? How about varying speed (power) of the motors?

Single Sensor More Advanced Model

An intermediate form of the above scenario is possible where an additional calibration point is inserted.  Imagine that we now have the states:

a — See black — we are on the line

b — See ‘grey’ (between black and white light sensor value) — we are on the edge of the line, so less sharp turn needed

c — We see no black or grey (see white) we are totally of the line

Things to note:

  1. A Forever loop is used to repeatedly get sensor readings and apply the appropriate steering correction to the wheels, as fast as possible. The loop will repeat several times per second.
  2. Inside the loop, we repeatedly check the light sensor, which will return a calibrated number from 0 to 100 that corresponds to how much white the light sensor is seeing.  100 would mean totaly over white, 0 is totally over black, 50 would mean that we are exactly centered on the border between the black and the white, and other values are between these cases.

The goal is to go as straight as possible, so if we are mostly centered on the border then just go straight.  If we are way off to the side, then make a hard turn in the correct direction.  If we are slightly off center, then make a small steering correction in the correct direction.

To do this, we split the light range into five ranges, which we can respond to separately, by dividing the light sensor reading by 20, which takes the 0-100 number to 0-4 (or sometimes 5) and then using a Switch with five conditions for the numbers 0-4,  (and 4 is the “default” condition so that it will catch the 5 values too).  In these conditions we do:

0 = Hard Right turn by greatly reducing power to motor C

1 = Gradual Right turn by slightly reducing power to motor C

2 = Straight Ahead with both motors equal

3 = Gradual Left turn by slightly reducing power to motor B

4 = Hard Left turn by greatly reducing power to motor B

Individual Motor blocks are used to control each wheel instead of using a Move block with steering, since this keeps the rotation sensors from interfering with the amount of steering.

The Motor duration is Unlimited because we just want to start moving that way then immediately loop back and check the sensor again to see if we need to change direction (Unlimited here means “Go this way until I tell you otherwise”).