Reading the Zumo 32U4 Downward Facing Light Sensors, Part 1

The Zumo 32U4 has 5 downward facing light sensors on the front light sensor array.  The light sensor array board also has 3 IR sensors used for proximity sensors or IR receivers for remote control.  However, two of the light sensors share data sensor lines with two of the IR proximity sensors.  For the time being, we are only going to look at using 3 of the 5 downward facing light sensors.  We will discuss more about how to switch to 5 sensors later when we talk about line following.

As usual, we need to include the library and rename the classes that we are going to use to something shorter.  In this program, we will need the Zumo32U4LCD class and the Zumo32U4LineSensors class.  The beginning of the program should look like this:

#include <Zumo32U4.h>
Zumo32U4LCD lcd;
Zumo32U4LineSensors lineSensors;

The library functions for reading the lights sensors wants to put the data into a 3 element array of unsigned integers.  An unsigned integer is nothing more than an integer that can only be positive.  An array is simply a list of values, like in the EV3.  However, reading and writing to an array in C is much easier than in EV3.  A particular element of an array is referred to by the array index inside square brackets.  If we wanted to access index 2 of array myArray, we would refer to it as myArray[2].  Like all variables in C, we need to declare them.  We are going to declare the line sensor value array at the very beginning to make it a global variable.  The line declare the array looks like this:

unsigned int lineSensorValues[3]; 

In this statement, we are declaring that the variable lineSensorValues is a 3 element array of type unsigned int.  In order to use the line sensor array, we need to run an initialization function.  This basically turns the sensors on and gets them ready for use.  We only need to run this once, so let’s put it in the setup().  The function to initialize the sensors is:

lineSensors.initThreeSensors(); 

To read the sensors, we use the function:

lineSensors.read(lineSensorValues,QTR_EMITTERS_ON); 

This stores the three uncalibrated light sensor values of each light sensor in the array lineSensorValues that we declared up above.  The “QTR_EMITTERS_ON” option forces the emitters to be on to measure the reflected light intensity.  Note that is not necessary as the default condition has the emitters turned on.  However, it is useful to include.  The left sensor value is stored in lineSensorValues[0], the middle sensor is stored in lineSensorValues[1], and the right sensor value is stored in lineSensorValues[2].  We will discussed calibrating the light sensors in a later post.

We just need to display these values, and we should be set.  In the “Hello World” program, we wrote a string to the display (a portion of text between quotes).  To write the value of a variable is done in much the same way, but no quotes.

lcd.print(lineSensorValues[0]); 

In order to fit all three values on the display, we need to stagger the position on the 2×8 element LCD.  The complete program is below:

#include <Zumo32U4.h>
Zumo32U4LCD lcd;
Zumo32U4LineSensors lineSensors;

unsigned int lineSensorValues[3];

void setup() {
  lineSensors.initThreeSensors();
}

void loop() {
  lineSensors.read(lineSensorValues,QTR_EMITTERS_ON);
  lcd.clear();
  lcd.gotoXY(2, 0);
  lcd.print(lineSensorValues[1]);
  lcd.gotoXY(0, 1);
  lcd.print(lineSensorValues[0]);
  lcd.gotoXY(4, 1);
  lcd.print(lineSensorValues[2]);
  delay(20);
} 

I also added a delay of 20 ms between updates.  This program executes each time through the loop in less than 1 ms, and the display looks like noise.  The 20 ms delay slows it down enough that one can almost read it.  The light sensor values are uncalibrated and range from a low value (100ish) to a value of about 2000 when completely saturated by an external light.  On my light colored table, I get about 110-200 depending on the sensor, and I get between 1100-1200 if the robot is leaning off the table.

One thing to try is to change QTR_EMITTERS_ON to QTR_EMITTERS_OFF.  This turns off the emitters and measures the background light.  You will probably get a value of 2000 on all of the sensors.  If you shine a light on an emitter, the value will quickly drop.  One of the start conditions of a sumo competition that we participate in often uses a light bulb to signal the start of the match.  Turning the emitters off would allow one to easily sense the light bulb.

This entry was posted in Robotics and tagged , , . Bookmark the permalink.