LilyPad Light Sensor Hookup Guide a learn.sparkfun.com tutorial

Available online at: http://sfe.io/t411

Contents

Introduction

The LilyPad Light Sensor is an e-textile friendly version of the Ambient Light Sensor Breakout. If you've used the breakout in a project before, the hookup and code will be very similar. You will need to connect to a LilyPad Arduino or other microcontroller to read the sensor values and use in your code.

This sensor outputs an analog value from 0 to 3.3V. In bright light (full daylight) this sensor will output 3.3V, and if completely covered will output 0V. In a typical indoor lighting situation, the sensor will output from around 1 to 2V.

LilyPad Light Sensor

DEV-08464
Retired

To follow along with the code examples, we recommend:

Suggested Reading

To add this sensor to a project, you should be comfortable sewing with conductive thread and uploading code to your LilyPad Arduino. Here are some tutorials to review before working with this sensor:

What is a Circuit?

Every electrical project starts with a circuit. Don't know what a circuit is? We're here to help.

Light

Light is a useful tool for the electrical engineer. Understanding how light relates to electronics is a fundamental skill for many projects.

Insulation Techniques for e-Textiles

Learn a few different ways to protect your conductive thread and LilyPad components in your next wearables project.

LilyPad Basics: E-Sewing

Learn how to use conductive thread with LilyPad components.

Getting Started with LilyPad

An introduction to the LilyPad ecosystem - a set of sewable electronic pieces designed to help you build soft, sewable, interactive e-textile projects.

If you have not used the TEMT6000 light sensor before, we recommend checking out the TEMT6000 Hookup Guide for basic information regarding that sensor.

TEMT6000 Ambient Light Sensor Hookup Guide

October 26, 2016

Bring the ability to detect light levels to any project with the SparkFun TEMT6000 Ambient Light Sensor Breakout.

Attaching to a LilyPad Arduino

The LilyPad Light Sensor has three sew tabs - Power (+), Ground (-), and Signal (S). The signal tab should be connected to an analog tab (marked with an 'A') on the LilyPad Arduino.

To follow along with the code examples in this tutorial, connect the light sensor to a LilyPad Arduino as shown below. Use alligator clips to temporarily connect Signal to A3 on a LilyPad Arduino, (-) to (-) on the LilyPad, and (+) to A5. When you are finished prototyping, replace the alligator clips with conductive thread traces for permanent installation in your project.

alt text

Connecting to a LilyPad Arduino USB

alt text

Other LilyPad connection notes:

Reading Values in Serial Monitor

Note: This example assumes you are using the latest version of the Arduino IDE on your desktop. If this is your first time using Arduino, please review our tutorial on installing the Arduino IDE.

After connecting the light sensor, let's take a look at the values it reads under different lighting conditions. For this we'll use analogRead() and Serial.print().

Upload the following code to your LilyPad Arduino, making sure to select the correct LilyPad board from the drop down menu below. Choose LilyPad Arduino USB if using a LilyPad Arduino USB. The LilyPad Arduino Simple, LilyPad Arduino, and LilyPad Development Board, and Development Board Simple all use a LilyPad ATmega 328. Select LilyPad USB Plus if following along with the LilyPad ProtoSnap Plus.

Don't forget to select the Serial Port that your LilyPad is connected to.

Copy the following code and upload it to your LilyPad.

language:c
/******************************************************************************

LilyPad Light Sensor Example
SparkFun Electronics

This example code reads the input from a LilyPad Light Sensor and displays in
the Serial Monitor.

Light Sensor connections:
   * S pin to A3
   * + pin to A5
   * - to -

******************************************************************************/

// Set which pin the Signal output from the light sensor is connected to
// If using the LilyPad Development Board, change this to A6
int sensorPin = A3;
// Create a variable to hold the light reading
int lightValue;

void setup()
{
    // Set sensorPin as an INPUT
    pinMode(sensorPin, INPUT);

    // Set pin A5 to use as a power pin for the light sensor
    // If using the LilyPad Development Board, comment out these lines of code
    pinMode(A5, OUTPUT);
    digitalWrite(A5, HIGH);

    // Initialize Serial, set the baud rate to 9600 bps.
    Serial.begin(9600);
}

void loop()
{

   // Get the current light level
    lightValue = analogRead(sensorPin);

   // Print some descriptive text and then the value from the sensor
    Serial.print("Light value is:");
    Serial.println(lightValue);

    // Delay so that the text doesn't scroll too fast on the Serial Monitor. 
    // Adjust to a larger number for a slower scroll.
    delay(200);
}

Once your code is uploaded, open the serial terminal in the IDE and see the output. Numbers should begin to stream by. Observe how the numbers change as the ambient light changes. Use your hand to cover the sensor or a flashlight to shine more light on it. Next we'll be using these values to control behaviors in our code.

Using Values to Trigger Behaviors

Next, we'll make some decisions in the code based on the light sensor's readings. This example code creates a simple automatic night light that turns on an LED when it’s dark.

We'll use the analogRead() function to get data from the light sensor and compare it to a variable we set for darkness level. When the readings from the light sensor fall below our threshold set for dark, it will turn on the LED.

You can hook up a LilyPad LED to pin 5 or use the built-in LED attached to pin 13.

language:c
/******************************************************************************

LilyPad Light Sensor Trigger - Automatic Night Light
SparkFun Electronics

Adapted from Digital Sandbox Experiment 11: Automatic Night Light

This example code reads the input from a LilyPad Light Sensor compares it to
a set threshold named 'dark'. If the light reading is below the threshold,
an LED will turn on.

Light Sensor connections:
   * S pin to A3
   * + pin to A5
   * - to -

Connect an LED to pin 5 or use the built-in LED on pin 13

******************************************************************************/
// The dark variable determines when we turn the LEDs on or off. 
// Set higher or lower to adjust sensitivity.
const int darkLevel = 50;

// Create a variable to hold the readings from the light sensor.
int lightValue;

// Set which pin the Signal output from the light sensor is connected to
// If using the LilyPad Development Board, change this to A6
int sensorPin = A3;

// Set which pin the LED is connected to. 
// Set to 13 if you'd rather use the LilyPad Arduino's built-in LED.
int ledPin = 5;

void setup()
{
    // Set sensorPin as an INPUT
    pinMode(sensorPin, INPUT);

    // Set LED as outputs
    pinMode(ledPin, OUTPUT);

    // Set pin A5 to use as a power pin for the light sensor
    // If using the LilyPad Development Board, comment out these lines of code
    pinMode(A5, OUTPUT);
    digitalWrite(A5, HIGH);

    // Initialize Serial, set the baud rate to 9600 bps.
    Serial.begin(9600);
}

void loop()
{
    // Read the light sensor's value and store in 'lightValue'
    lightValue = analogRead(sensorPin);

    // Print some descriptive text and then the value from the sensor
    Serial.print("Light value is:");
    Serial.println(lightValue);

    // Compare "lightValue" to the "dark" variable
    if (lightValue <= darkLevel) // If the reading is less then 'darkLevel'
    {
        digitalWrite(ledPin, HIGH); // Turn on LED
    }
    else // Otherwise, if "lightValue" is greater than "dark"
    {
        digitalWrite(ledPin, LOW);  // Turn off LED
    }

    // Delay so that the text doesn't scroll to fast on the Serial Monitor. 
    // Adjust to a larger number for a slower scroll.
    delay(100);
}

If your light sensor isn't triggering correctly, check the output of the Serial Monitor to see if there's a better value for the dark variable than what is set in the example code.

Project Examples

Light Sensitive Hat

Let your geek shine with this hat that blinks when the lights go down.

alt text

alt text

Musical Bracelet

Combining the sensor with a LilyPad Buzzer can create interesting interactive projects, for example this wearable light-controlled musical instrument or Opto-Theremin. Control tones on the buzzer by covering the LilyPad Light Sensor.

alt text

alt text

Twinkling Prom Dress

This prom dress project featured in this video uses an initial threshold setting and light sensor to trigger some LilyPad Pixel Boards.

LilyPad Safety Scarf

Create a scarf lights up when it gets dark with LilyPad and sewable LED ribbon.

LilyPad Safety Scarf

November 21, 2017

This scarf is embedded with a ribbon of LEDs that illuminate when it gets dark out, making yourself more visible to vehicle and other pedestrians.

Resources and Going Further

Here are some resources for planning a project with the light sensor:

Or, you can check out these other great light-based tutorials.

LED Cloud-Connected Cloud

Make an RGB colored cloud light! You can also control it from your phone, or hook up to the weather!

3D Printed Illuminated Wand Prop

In this tutorial, we will learn about how to create a theatrical prop for a performance by 3D printing a wand and adding an LED.

SparkFun Pulse Oximeter and Heart Rate Monitor Hookup Guide

Find out your oxygen saturation level or check out your heart rate using the MAX30101 biometric sensor and MAX32664 Biometric Hub via I2C!

Garmin LIDAR-Lite v4 (Qwiic) Hookup Guide

Connecting the Garmin LIDAR-Lite v4 to a microcontroller just got easier! Check out our hookup guide to get started.

learn.sparkfun.com | CC BY-SA 3.0 | SparkFun Electronics | Niwot, Colorado