LilyPad Light Sensor V2 Hookup Guide

Pages
Contributors: Gella
Favorited Favorite 3

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 Tools->Board drop down menu. Choose LilyPad Arduino USB if using a LilyPad Arduino USB. The LilyPad Arduino Simple, LilyPad Arduino, LilyPad Development Board, and Development Board Simple all use a LilyPad ATmega 328. Select LilyPad USB Plus if following along with the LilyPad USB Plus or LilyPad ProtoSnap Plus. Don't forget to select the Serial Port that your LilyPad is connected to.

Note the following potential code changes:
  • If prototyping with a LilyPad ProtoSnap Plus, change sensorPin to A2.
  • If prototyping with a LilyMini ProtoSnap, change sensorPin to 1.

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 tab to A2
   * + tab to +
   * - tab to -

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

// Set which pin the Signal output from the light sensor is connected to
int sensorPin = A2;
// Create a variable to hold the light reading
int lightValue;

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

    // 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 observe the output. Numbers should begin to stream by. Note 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.