Light Sensor

In this project, you'll learn how to use a light sensor to automatically turn on and off a LED. The light sensor is a photo resistor that can sense the amount of light in a room. So by connecting a light sensor and a LED to the Arudino, the LED can be automatically turned on when the room is dark and turned off when it is bright.

Unlike a switch (such as the push button or the tilt sensor) that can only be either turned on or off, a photo resistor can allow varying amounts of electricity to pass through depending on its resistance just like a regular resistor. An electric signal having a range of values is called an analog signal as opposed to a digital signal with only a HIGH or LOW value.

To read an analog signal from the photo resistor, we need to use one of the analog input pins on the Arduino. There are six analog input pins on the Arduino board labeled A0 to A5.

Parts needed:
  • Arduino
  • A photo resistor
  • A 22K Ω resistors (red-red-orange)
  • Wires
  • Breadboard
Software stuff you'll learn:
Photo resistor      22K ohm resistor
1 Making the connections
  • Connect one end of the photo resistor to positive 5V on the Arduino.
  • Connect the other end of the photo resistor to pin A0 on the Arduino and to one end of the 22K Ω resistor.
  • Connect the other end of the resistor to GND on the Arduino board.
Light sensor schematic
Light sensor connections
2 Create a new Bare Minimum program by selecting File from the menu
  • then select Examples
  • then select Basics
  • then select BareMinimum

Then type in this program.


The Serial.begin command prepares the serial monitor for printing outputs. The number 9600 is the speed at which the monitor will communicate at.

The analogRead command reads in an analog value (a number between 0 and 1023) from the specified analog input pin (pin A0 in this case). This number is assigned and stored in the sensorValue variable.

The Serial.println command prints out the number that is stored in the sensorValue variable to the monitor.
Light sensor program
3 Open the serial monitor by clicking on the Serial Monitor icon. Serial monitor icon
4 Make sure that the baud rate (number at the bottom right corner of the monitor) matches the number specifed in the Serial.begin command. In this case, it is 9600. Monitor output
5 Run the program and notice the numbers being printed in the serial monitor.

The numbers get smaller when you cover the photo resistor with your hand, and gets larger when you move your hand away.

What is the smallest and largest number that you get?

Calculate the middle number between the smallest and largest number that you get. You will use this middle number in the next experiment.