LilyPad Light Sensor Hookup Guide
Contributors:
Gella
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.
Don't forget to select the Serial Port that your LilyPad is connected to.
- If prototyping with a LilyPad ProtoSnap Plus, change sensorPin to A2.
- If prototyping with a LilyPad Development Board, change sensorPin to A6.
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.