Lab 6 - Light Sensor

Objective: to make an LED change its brightness level according to how much light is hitting the light sensor

Parts List

Light Sensor 1
10k Ohm resistor 1
LED (any color) 1
330 Ohm resistor 1
Wire (red) 1
Wire (black) 1
Wires (any) 4

Parts Set-Up

Instructional Parts Set-Up

Part Connection 1 Connection 2
Light Sensor F5 F6
10k Ohm Resistor I1 I5
LED H19 H20
330 Ohm Resistor J20 GND
Wire (any color) J1 +ve column
Wire (any color) J5 Arduno Analog 0
Wire (any color) J6 -ve column
Wire (any color) J19 Pin 9
Wire (red) +ve column Arduino 5V
Wire (black) -ve column Arduino GND

Visual Parts Set-Up 


Lab 6 Code

Without Comments


With Comments

Line Code Comment
1 int lightPin = 0; this is the integer to store the value coming in from the light sensor
int ledPin = 9; this is the standard integer to represent the Arduino pin to control the led
void setup() declares the mandatory setup() function
{ this bracket shows us the start of the setup() function
pinMode (ledPin, OUTPUT) setting up ledPin (pin 9) as an output
} this bracket ends the setup() function
void loop() this starts the loop that will repeat
{ this bracket declares the start of the loop() function
int lightLevel = analogRead(lightPin); creates the integer called lightLevel which stores the value returned from the light sensor
10 lightLevel = map(lightLevel, 0, 900, 0, 255); this converts the lightLevel value from a number between 0-900 to 0-255
11 lightLevel = constrain(lightLevel, 0, 255); this makes sure the lightLevel number is between 0 and 255
12 analogWrite(ledPin, lightLevel); power is sent to ledPin (pin 9) at the power level equal to lightLevel (now 0-255)
13 } this bracket closes out the loop() function



This command converts a number from one range to another. In this case, you have light coming in that will give a reading from 0-900.

You need this to be the output for your LED brightness, which has to be from 0-255.  Map DOES NOT limit the output value to the lower and upper limits, should it end up outside the requested range.

Map follows this format:


This keeps a value within a range.  If the value is above a range, it will give it the maximum value.  If it is below a range, it will give it the minimum.