Tuesday, January 11, 2011

Light Sensor on Arduino

The next board I'd like to write about, is the light sensor mini board. The situation is similar to the temperature board. Easy to connect, but this time there was no datasheet at all. I couldn't find out which photoresistor was used, so the value which was read had to be approximated as well. I found a good resource of the theory of calculation here.

Here is the source code to this example:
int lightSensor = 2;
int lightADCReading;
double currentLightInLux;
double lightInputVoltage;
double lightResistance;

void setup() {
  Serial.begin(9600);
}

void loop() {
  lightADCReading = analogRead(lightSensor);
  // Calculating the voltage of the ADC for light
  lightInputVoltage = 5.0 * ((double)lightADCReading / 1024.0);
  // Calculating the resistance of the photoresistor in the voltage divider
  lightResistance = (10.0 * 5.0) / lightInputVoltage - 10.0;
  // Calculating the intensity of light in lux       
  currentLightInLux = 255.84 * pow(lightResistance, -10/9);

  Serial.print("Light ADC reading: ");
  Serial.println(lightADCReading);
  Serial.print("Light in lux: ");
  Serial.println(currentLightInLux);
  delay(2000);
}

This is the output of the program:

2 comments:

  1. benutzt du processing oder direkt java?

    ReplyDelete
  2. Processing habe ich noch nicht benutzt. Ist für mich momentan auch nicht relevant. Für das Arduino board benutzte ich momentan die simple IDE und C. In Zukunft werden aber noch Projekte mit Java oder C# Interaktion folgen.

    ReplyDelete