Arduino Light Sensor

I am writing this mostly as notes to myself, though if anyone else finds it useful, all the better.

I found this tutorial, Arduino + Photocell (Light Sensor) + LED, from Jaycon Systems on YouTube. I bought some Arduino boards and components at a Radio Shack in Houston a few weeks ago, so I decided to build it. The basic idea is that the LED glows dimly when light shines on the photocell, but shines brightly when the photocell is in the dark.

Besides the Arduino Uno board, a breadboard and some jumper wires, there are only four components:

  • an LED (the green box at the bottom of the image below)
  • a CdS photoresistor (the round red thing on the left-side of the picture)
  • a 330 Ohm resistor (orange, orange, brown, gold bands)
  • a 10,000 Ohm resistor (brown, black, orange, gold bands)


Connect the components this way: A wire (red) from the 5V pin to one end of the photocell. A wire (yellow) and the 10kOhm resistor from the other end of the photocell to the A0 pin. The other end of the 10 kOhm resistor to the short leg of the LED and to Ground. The long end of the LED to the 330 Ohm resistor. The other end of the 330 Ohm resistor to the ‘3’ pin on the Arduino board.

Here is what I think the circuit diagram is:


The code to run it is included in the Arduino libraries under Examples > 0.3Analog > AnalogInOutSerial, but you’ll have to modify analogOutPin to be ‘3’ and change the ‘outputValue’ line. For the LED I used, I changed the line to ‘outputValue = map(sensorValue, 100, 500, 0, 255);’. I first had a different LED and different values in the map() command, but found that by changing LEDs and vales in the code, I was able to get a LED that shines at a low level when the ambient light is bright, and that brightens when the ambient light level drops.

Here’s the code:

Analog input, analog output, serial output

Reads an analog input pin, maps the result to a range from 0 to 255
and uses the result to set the pulsewidth modulation (PWM) of an output pin.
Also prints the results to the serial monitor.

The circuit:
* potentiometer connected to analog pin 0.
Center pin of the potentiometer goes to the analog pin.
side pins of the potentiometer go to +5V and ground
* LED connected from digital pin 9 to ground

created 29 Dec. 2008
modified 9 Apr 2012
by Tom Igoe

This example code is in the public domain.


// These constants won’t change. They’re used to give names
// to the pins used:
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 3; // Analog output pin that the LED is attached to

int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)

void setup() {
// initialize serial communications at 9600 bps:

void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 100, 500, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);

// print the results to the serial monitor:
Serial.print(“sensor = ” );
Serial.print(“\t output = “);

// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:


    Leave a Reply

    Fill in your details below or click an icon to log in: Logo

    You are commenting using your account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s

%d bloggers like this: