ACTIVITY: Calibrating the IR Sensors

Calibrating the IR Sensors – Overview

We know that line following robots move on a specified “line”. But how do they know where the line is?

Come in IR sensors!

Line following robots uses electronic devices called IR sensors to detect the line they have to follow.

Let’s understand how they work.

Understanding the Logic

An IR sensor consists of 2 LEDs: one which transmits the IR light and one which receives the IR light. When the IR rays are transmitted, they bounce from the nearest surface and get back to the receiver LED. That’s how an IR sensor detects an object.

But to detect colors, we depend on the number of rays the surface reflects:

  1. The dark surface will absorb more IR rays and as a result, the receiver will get fewer IR rays. 
  2. White or shiny objects will absorb less IR rays and as a result the receiver will get more IR rays.

We can get the sensor values in PictoBlox and based on that value we can estimate whether the surface is black or white.

  1. If the sensor detects the black line, its output value is increased. This means that the sensor is active.
  2. If it detects the white area, its output value decreases. This means that the sensor is inactive.

The value above which the sensor detects the black line, we will call the threshold value. If the sensor value is less than the threshold, it means that the sensor hasn’t detected the line yet.

Before we start using our line following robot, we need to first calibrate the IR sensors, i.e. make sure they’re working fine. For that, we’ll make a script in PictoBlox to calibrate them. We’ll connect Quarky to PictoBlox and find out the threshold value for its IR sensors.

evive Alert
IR sensors DON’T work in sunlight. IR rays from the sun increase the overall threshold of the sensors therefore they stay active all time. A closed environment or nighttime is the place/time to work with your line following robot.

Let’s Code!

Finding the Threshold Values

  1. Open a new project by selecting New from File.
  2. Connect Quarky to PictoBlox as shown in the previous lesson.
  3. Go to the Variables palette and create a variable named Sensor Value.


  4. Next, make the following script to keep updating the variable value to the left IR sensor value. 
    You will notice that it appears in the top-left corner of the stage. We’ll use this variable to find out the threshold value for our IR sensors.
  5. Let’s first find out the threshold value of the first IR sensor, i.e. the Left Sensor.
  6. Take out the track from your Quarky kit and bring the black line pretty close to the sensor. Observe the change in the variable value on the Stage. Note this value.
  7. Then bring the white space close to the sensor. You’ll notice that the value decreases. Note down the new value.

    The threshold value will be in between these two values. E.g., if the value when the sensor detects the black line is 2000 and when it detects the white space is 500, then we will keep the threshold at 1200.

    Repeat the process for the second i.e. the right IR sensor. Note that it is possible for the values of both the sensors to be different.

Getting Feedback via LEDs

Now, to check if the value works fine for our sensors, let’s add a feedback loop, something that will indicate that the sensor has detected the black line.

  1. Add a when flag clicked block from the Events palette.
  2. Go to the Sensors palette and add a set () IR sensor threshold to () block and write the threshold value you found out above.
  3. Next, add a forever block from the Control palette. We’re using this block because we want to continuously check if the sensor detects the line.
  4. Then, add an if-else block inside the forever block.
  5. Next, go to the Sensors palette and add an is () IR sensor active? block inside the white space of the if-else block. The IR sensor will be active when it detects the black line. This means that its value should be greater than the threshold value. This block will essentially check if the sensor value is greater than the threshold value.
  6. To indicate that the sensor is active, let’s light up one of the LEDs of the dot matrix. From the Display palette, add a set LED x () y () to () with brightness () block under the if arm of the if-else block. Set the colour as green from the drop-down by clicking the color circle on the block.
  7. Duplicate the block and drop it under the else arm of the if-else block. Set the color as red. this time.

Calibrating the IR Sensors

Now, run the script by clicking the green flag and bring the black line of the track close to the IR sensor. One of the following three conditions will happen:

  1. Your calibration value is HIGH: The black region will not be detected in this case. Reduce the calibration value.
  2. Your calibration value is LOW: The white region will not be detected in this case. Therefore, increase the calibration value.
  3. Your calibration value is OK. The white and black regions are detected accurately.

Now modify the script to add the detection value for the right IR sensor.

Now, run the script by clicking the green flag and bring the black line of the track close to the right IR sensor.

Final Testing

Make sure you have the left and the right sensors calibrated properly before you move ahead.