Lesson 4

Lesson 4: Detecting Light with Apeiros

In this lesson you will learn how to measure the ambient light levels using the (3) front-mounted light sensors and the ApeirosAdSensors class library.

Prerequisites

Before starting this lesson be sure to follow the Initial Setup & Configuration. Also, we strongly recommend that you take some time to read "Getting Started with Arduino and Genuino products".

 

Introduction

Apeiros provides a total of (3) front-mounted ambient light sensors: front left (U5), front center (U6) and front right (U7). You can easily read the current state of the front light sensors by using the ApeirosAdSensors class library. Want to learn more about the specific light sensor, P/N TEMT6000X01, that is used by Apeiros, then click here.

Apeiros Class Library Functions

The ApeirosAdSensors class library contains a total of (3) different functions for reading the current value for each light sensor.

uint16_t getLeftLight();
uint16_t getCenterLight();
uint16_t getRightLight();

Recall that we need to first declare an instance of the ApeirosAdSensors class library. However, we do NOT need to add anything to the setup() function.

// Declare Apeiros class instances.//
/***********************************/
ApeirosAdSensors sensors;
/***********************************/

void setup() {
  // put your setup code here, to run once:

}

Once we have declared an instance, sensors, of the ApeirosAdSensors library, then we can access the light sensor functions. Suppose that we want to read the current value for each light sensor. First we need to create three unsigned integer variables, so that we can use them to store each sensor reading. We use an unsigned integer because a valid light sensor reading can be a positive 10-bit integer from 0 to 1023 and will never be negative.

void loop() {
  // put your main code here, to run repeatedly:
 
  // Read left light sensor value.
  unsigned int leftLight = sensors.getLeftLight();

  // Read center light sensor value.
  unsigned int centerLight = sensors.getCenterLight();

  // Read right light sensor value.
  unsigned int rightLight = sensors.getRightLight();
  
  delay(10);  // Delay for a specified amount of time in milliseconds
}

A valid sensor reading can be any positive value in the range of 0 to 1023. A value of 0 corresponds to an extremely low level of light (dark). While a value of 1023 corresponds to a high level of light (bright). Once we have a valid sensor reading from each light sensor, then we can use them to control the left and right motors. For this example, we will create a light tracking behavior using the front left and right light sensors. We have to figure out how to map a light sensor value to a motor PWM value.

Keep in mind that a valid light sensor value can be a maximum of 1023 and a motor PWM value can be a value of 180. More specifically, we need to map the current left light reading to the right motor PWM and the current right light value to the left motor PWM. By doing this, Apeiros will drive towards a light source using a motor PWM value that is based upon the amount of incident light.

void loop() {
  // put your main code here, to run repeatedly:
 
  // Read left light sensor value.
  unsigned int leftLight = sensors.getLeftLight();

  // Read center light sensor value.
  unsigned int centerLight = sensors.getCenterLight();

  // Read right light sensor value.
  unsigned int rightLight = sensors.getRightLight();

  int rightMotorSpeed = leftLight/5;

  int leftMotorSpeed = rightLight/5;

  motors.setSpeeds(leftMotorSpeed, rightMotorSpeed); // Set motor speeds using light levels.

  delay(10);  // Delay for a specified amount of time in milliseconds
}

Did you know that when an organism is attracted to light that it is considered to be positively phototropic. When an organism wants to move away from light then it is considered to be negatively phototropic. In this example, our light tracking code will make Apeiros positively phototropic like a moth to a bright light!

EXPERIMENT


 

Can you create a sketch that will make Apeiros negatively phototropic and run away from light? HINT: You need to think about the sign (+/-) of the motor speeds.

Can you create a custom light behavior that uses the center light sensor as well as the left and right light sensors? HINT: If the center light sensor value is greater than each of the left and right light sensor values, then drive Apeiros straight forward with equal motor PWM values.