You are on page 1of 2

Arduino UNO: PhotoCell - sensing light

Now that we have a Processing sketch (here) that can display sensor data, I
thought it would be good to test it out with a simple Arduino PhotoCell sketch.

Here is what you will need to complete the Arduino side of the project:

PhotoCell (or PhotoResistor)


10K resistor
Breadboard
Arduino UNO
3-4 wires (to connect it all together)
USB cable to upload sketch and for Serial communication with
Processing.

The Fritzing sketch:

The Arduino Code:

1 int photoRPin = 0;
2 int minLight;
3 int maxLight;
4 int lightLevel;
5 int adjustedLightLevel;
6
7 void setup() {
8 Serial.begin(9600);
9
10 //Setup the starting light level limits
11 lightLevel=analogRead(photoRPin);
12 minLight=lightLevel-20;
13 maxLight=lightLevel;
14 }
15
16 void loop(){
17 //auto-adjust the minimum and maximum limits in real time
18 lightLevel=analogRead(photoRPin);
19 if(minLight>lightLevel){
20 minLight=lightLevel;
21 }
22 if(maxLight<lightLevel){
23 maxLight=lightLevel;
24 }
25
26 //Adjust the light level to produce a result between 0 and 100.
27 adjustedLightLevel = map(lightLevel, minLight, maxLight, 0, 100);
28
29 //Send the adjusted Light level result to Serial port (processing)
30 Serial.println(adjustedLightLevel);
31
32 //slow down the transmission for effective Serial communication.
33 delay(50);
34 }

You might also like