3.5. Detecting Light

3.5.1. Our goal

In this tutorial, we are making a Grove Light Sensor light up a Grove Led depending on how strong the intensity of the light is. The Grove Light Sensor and the Grove Led are both connected to the GoPiGo3 and use the following ports.

  • Port "AD1" for the light sensor.

  • Port "AD2" for the LED.

Important

Since this tutorial is based on Led tutorial, we recommend following that one before going through the current one.

3.5.2. The code we analyse

The code we’re analyzing in this tutorial is the following one.

# import the time library for the sleep function
import time

# import the GoPiGo3 drivers
import easygopigo3 as easy

# Create an instance of the GoPiGo3 class.
# GPG will be the GoPiGo3 object.
gpg = easy.EasyGoPiGo3()

# Create an instance of the Light sensor
my_light_sensor = gpg.init_light_sensor("AD1")
my_led = gpg.init_led("AD2")

# loop forever while polling the sensor
while(True):
    # get absolute value
    reading = my_light_sensor.read()
    # scale the reading to a 0-100 scale
    percent_reading = my_light_sensor.percent_read()

    # check if the light's intensity is above 50%
    if percent_reading >= 50:
      my_led.light_off()
    else:
      my_led.light_max()
    print("{}, {:.1f}%".format(reading, percent_reading))

    time.sleep(0.05)

The source code for this tutorial can also be found here on github.

3.5.3. The modules

Start by importing 2 important modules:

import time
import easygopigo3 as easy

The easygopigo3 module is used for interacting with the GoPiGo3 robot, whilst the time module is generally used for delaying actions, commands, setting timers etc.

3.5.4. The objects

After this, we need to instantiate an easygopigo3.EasyGoPiGo3 object. We are using the EasyGoPiGo3 object for creating an instance of Led class, which is necessary for controlling the Grove Led and for reading off of the Grove Light Sensor.

gpg = easy.EasyGoPiGo3()

Now that we have an EasyGoPiGo3 object, we can instantiate a LightSensor and Led objects. The argument of each of the 2 initializer methods represents the port to which a device is connected.

my_light_sensor = gpg.init_light_sensor("AD1")
my_led = gpg.init_led("AD2")

Note

See the following graphical representation as a reference to where the ports are.

3.5.5. Main part

Let’s make the LED behave in the following way.

  • When the light’s intensity is below 50%, turn on the LED.

  • When the light’s intensity is above 50%, turn off the LED.

To do this, we need to read the percentage value off of the light sensor - the variable responsible for holding the value is called percent_reading. Depending on the determined percentage, we turn the LED on or off.

To do all this, check out the following code snippet.

while(True):
    # get absolute value
    reading = my_light_sensor.read()
    # scale the reading to a 0-100 scale
    percent_reading = my_light_sensor.percent_read()

    # check if the light's intensity is above 50%
    if percent_read >= 50:
      my_led.light_off()
    else:
      my_led.light_max()
    print("{}, {:.1f}%".format(reading, percent_reading))

    time.sleep(0.05)

3.5.6. Running it

Here’s the fun part. Let’s run the python script.

Connect the Grove Light Sensor to your GoPiGo3 robot to port "AD1" and Grove Led to port "AD2". Within a terminal on your Raspberry Pi, type the following 2 commands:

cd ~/Desktop/GoPiGo3/Software/Python/Examples
python easy_Light_Sensor.py
http://i.imgur.com/AyVhrvi.gif