LEGO Mindstorms Class:  Lesson 4

The Light Sensor:

The light sensor allows a Mindstorms robot to "see" the difference between light and dark.  It detects light and dark surfaces by shining a light on the surface, and seeing how much of the light bounces back to the detector.  Light surfaces reflect more light, and dark surfaces reflect less light.  You can also turn off the light bulb in the light sensor, and then the sensor will detect the amount of light around it.  We'll work with reflected light, and use the light sensor attached to 5-Minute Bot to detect dark and light surfaces that it drives over.

Because lighting conditions differ depending on where you are or what time of day it is, the light sensor needs to be calibrated every time you use it under different lighting conditions (for example if you move from a dark room to a brightly lit room, or take your robot outside).  What looks to be one color in a dark room may look different in bright sunlight.  

You have to tell the light sensor what you mean by "dark" and what you mean by "light".  When you write a program that checks the light sensor, the sensor returns a value between 0 and 100.  0 is the darkest and 100 is the lightest.  When you calibrate the sensor, it asks you to show it the darkest color it will be detecting, and it sets that as the 0 value.  Similarly, it asks to see the lightest color you will be detecting, and it sets that value to 100.  We'll see how to calibrate the sensor later on.

Let's attach a light sensor to our robot.

Attaching the Light Sensor to the 5-minute Bot:

Take off the touch sensor from the last lesson so you are only left with the 5-minute Bot (below)

You will need these parts:

Now we can assemble and attach the light sensor:

Attach the light sensor to the underside of 5-minute-Bot

Connect the light sensor to Port 3 on the Brick.  This is the default light sensor port.

That's it!  You're ready to follow the light!

Calibrating the Light Sensor:

You can calibrate the light sensor directly from the NXT-G programming environment, or write your own program to do it.  If you use the NXT programming environment directly, your brick needs to be plugged in to the computer for calibration.  Since we're using laptops, and can move them around easily, we'll use that method.


Exercise 1:  Calibrating the light sensor in the NXT-G programming environment

  1. Plug the brick into your computer.  Be sure it is turned on.
  2. Select  "Calibrate Sensors" from the "Tools" menu  in the NXT-G window.
  3. From the pop-up window (shown below), select light sensor, port 3, and click the "Calibrate" button.

  1. Look at the screen on the brick.  It is asking you to hold the light sensor over the darkest surface it will be scanning.   The changing numbers you see are the "raw" unprocessed output from the light sensor, and they range from 0 to about 1000.  Move the sensor around to surfaces with different lightness/darkness, and watch how the numbers change.  After you calibrate the sensor and use it in a program, the NXT-G program environment  will convert the numbers to a value between 0 and 100.
  2. Place the light sensor so that it is completely over a dark line on the paper your instructors have set up, then press "Enter" (the orange button).
  3. The screen is now asking you to hold the light sensor over the lightest surface it will be scanning.  Place the light sensor over the white part of the paper your instructors have set up, then press "Enter".
  4. Your light sensor is now calibrated.  The brick will remember these calibration settings until the next time you recalibrate it.  You will only need to recalibrate if you are using the light sensor in a place with different lighting conditions.

Exercise 2:  Write a program to display the final light sensor readings

Let's write a program to view the output from the calibrated light sensor.  We're using some advanced concepts here, including data wires, which carry information from one NXT programming block to another.  Data wires will let us take the numbers that are output by the light sensor block, and use them as input to the display block.  To input and output data to a NXT-G programming block, many blocks have what is called a data "hub" which clicks in and out of the data block.  You can click on the hub to make it slide in and out.  Below is a picture of the Display Block with the data hub closed, and opened.  The ports on the data hub are for taking in and putting out different kinds of data, such as numbers, text and images.

Display Block with the data hub closed:

To open the Data Hub, click on the line at the lower left of the Display Block.

A Display Block with the data hub open:
Each port is for inputting and outputting a different kind of information, such as text, start position, end position, etc.


You will need to know how to display written information on the NXT brick's screen.  The screen can write almost anything you tell it to, but it only understands numbers in "text" format.  Letters and words are automatically "text", but numbers are not.  To put the numerical (number) output from the light sensor into a form that the display block can understand, we will have to convert it to text.  Create a program named "showlight.rbt" in the NXT-G programming window (be sure to save it to the shared drive!), and follow the steps below to write it.

  1. We will want to run this program repeatedly, so set up a forever loop:


  1. Add a Light Sensor Block (from the Sensor Menu), and make sure that it reads Port 3
  2. Add a Number to Text Block (from the Advanced Menu)
  3. Now move the cursor to the plug sticking out of the bottom of the Light Sensor Block.  It will turn into a picture of a little spool of wire.  Click on the plug, and drag the cursor over to the plug to the left of the "#" plug at the bottom of the Number to Text Block.  When you let go, you will create a yellow Data Wire connecting the output of the light sensor block to the input of the Number to Text Block.

  4. Add a Display Block, and click on the data hub to make sure it pops out.

  5. Connect a data wire from the output of the Number to Text box to the capital letter "T" (for Text) on the Display Block data hub.

Set the Display block to display the input Text:

  1. Add a Wait block, and set it to wait for 0.2 seconds before repeating

  1. Now let's test out our program.  


Challenge 1:  Write a program to make the robot drive until it detects a dark line and then stop.  Use the paper that your instructors have provided, with electrical tape to make the dark lines.

Before you attempt this challenge, think about what it means for your robot to detect a dark line.  Think about what the sensor readings will be as the sensor approaches the line.  When the light sensor is over the white paper, the readings will be pretty high.  When the sensor is just a little bit over the black line, less light will get reflected, so the light sensor values will start to drop.  The more of the sensor that is over the black line, the lower the light sensor values will be.  

Hint 1:  Use the Wait for Sensor Block to let your robot keep moving until the value of the Light Sensor has dropped past a certain value.  Think about what a reasonable value might be to indicate that the sensor is over the black line.

Hint 2:  Make sure that the first Move Block in your program uses the unlimited setting, so that your program will check the light sensor while the robot is still driving.  Otherwise, the robot will finish running the move block before it checks the light sensor.

Challenge 2:  Your instructors have created a "ring" out of dark electrical tape on white paper.  The ring has a hole in it, large enough for your robot to drive through.  Write a program that allows your robot to find its way out of the ring without crossing over a black line.  Hint:  When you encounter a black line, you will want the robot to stop, turn, and travel in a different direction.  So the pseudocode (description of your program in words) would look something like this:

  1. Start driving forward.
  2. If you come across a black line, stop.
  3. Back up a few inches (you decide how many)
  4. Turn (you decide how far the robot should turn  -  try different values and see what works best!)
  5. Return to the start

Remember, each line in pseudocode corresponds to one instruction for the robot (and one programming "block" in NXT-G)  Once you get the program working, experiment with different values for the turn.  See which value works best for getting your robot out of the "ring" the fastest.

Challenge 3:  (Advanced!)  Your instructor has provided paper with dark parallel lines.  Write a program that lets the robot drive over two black lines, and stop on the third.  There are many ways to solve this problem.  See how creative you can be!  Talk to your instructors and see if your solution matches theirs.