25 June 2011

Arduino UNO: PhotoCell - sensing light

In this tutorial, we will send the analog readings obtained from a Photo Resistor, also known as a Light Dependent Resistor (LDR), to the computer. We will display the each reading on the monitor using a simple Processing sketch.


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

Parts Required:

  • 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.


Fritzing sketch:



Arduino Sketch


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* ==========================================================
Project : Send Photo resistor values to computer
Author: ScottC
Created: 25th June 2011
Description: This sketch will make the arduino read Photo resistor
             values on analog pin A0. The analog readings will
             be dependent on the amount of light reaching the
             sensor. The Analog readings will be sent to the
             computer via the USB cable using Serial communication.
==============================================================
*/

int photoRPin = 0; 
int minLight;          //Used to calibrate the readings
int maxLight;          //Used to calibrate the readings
int lightLevel;
int adjustedLightLevel;

void setup() {
 Serial.begin(9600);
 
 //Setup the starting light level limits
 lightLevel=analogRead(photoRPin);
 minLight=lightLevel-20;
 maxLight=lightLevel;
}

void loop(){
 //auto-adjust the minimum and maximum limits in real time
 lightLevel=analogRead(photoRPin);
 if(minLight>lightLevel){
 minLight=lightLevel;
 }
 if(maxLight<lightLevel){
 maxLight=lightLevel;
 }
 
 //Adjust the light level to produce a result between 0 and 100.
 adjustedLightLevel = map(lightLevel, minLight, maxLight, 0, 100); 
 
 //Send the adjusted Light level result to Serial port (processing)
 Serial.println(adjustedLightLevel);
 
 //slow down the transmission for effective Serial communication.
 delay(50);
}

Arduino Sketch Explanation:

Serial.begin(9600) : starts the serial communication between the Arduino and Processing
lightLevel=analogRead(photoRPin) : take a reading from the analog pin 0.
minLight and maxLight: automatically adjust the minimum and maximum range of the sensor
adjustedLightLevel: used to map the reading from the PhotoCell to a value between 0-100.

The adjustment of the light level is not necessary, and could be handled in Processing instead.
The delay(50) at the end of the sketch, is only used to slow down the transmission of the Serial messages to Processing. Depending on what you want to do with the sensor data, you may wish to increase or decrease the amount of delay.



 Processing Sketch

Here is a very basic Processing Sketch that will allow you to receive data from the Serial port attached to the Arduino and display the reading on your computer screen. In my case, the Arduino is connected to COM13. You may need to change this if you decide to follow in my footsteps.

The processing sketch will look for the line feed character ' \n' coming from COM13, which will trigger a serialEvent(). You can get processing to look for another character if you want to: just change the character within the bufferUntil() command. The draw() method is made redundant because the screen updating is handled by the serialEvent() in response to serial data communication with the Arduino UNO.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* ============================================================
Processing Version: 2.2.1
Project: Display Sensor data on computer screen
Author:  ScottC
Created: 25th June 2011
Description: This Processing sketch will allow you to display
             sensor data received from the Arduino via the
             serial COM port (or USB cable).
=============================================================== */
import processing.serial.*;
Serial myPort;
String sensorReading="";
PFont font;


void setup() {
  size(400,200);
  myPort = new Serial(this, "COM13", 9600);
  myPort.bufferUntil('\n');
  font = createFont(PFont.list()[2],32);
  textFont(font);
}

void draw() {
  //The serialEvent controls the display
}  

void serialEvent (Serial myPort){
 sensorReading = myPort.readStringUntil('\n');
  if(sensorReading != null){
    sensorReading=trim(sensorReading);
  }

  writeText("Sensor Reading: " + sensorReading);
}


void writeText(String textToWrite){
  background(255);
  fill(0);
  text(textToWrite, width/20, height/2);   
}

This is how the data will be displayed on the computer screen:

If this tutorial helped you, please support me here:



6 comments:

  1. I want to to connect simulink model of AUV to arduino ,sensors and actuators and want technical details of it

    ReplyDelete
    Replies
    1. Hi Anonymous,
      Sounds like an interesting project, but have never used Simulink, and have never heard of it until I saw your comment. Perhaps try the Arduino Forums and draw from collective intelligence. Sorry I could not help.

      Delete
  2. i want to use a Ledpin in it

    ReplyDelete
    Replies
    1. Have a look at some of my other tutorials. There is one that uses an LED pin.

      Delete
  3. I'm trying to make a light sensing car, can I used what you have produced to do so?

    ReplyDelete

Feel free to leave a comment :