Sunday, March 25, 2012

Simple light meter

This Arduino project is a simple light meter using a photo-transistor. An LDR would be more appropriate but the photo-transistor is what I had on hand. On the other hand the photo-transistor is sensitive to infrared, so its handy for testing that remote control.

The project consists of 10 red LEDs driven by 10 BC547 transistors. Any general purpose transistor will do. The base of each transistor is connected to the Arduino board digital pints D0 through to D7 via a 2.2K resistor. The LEDs are connected to a 12 volt source with current limiting resistors of 560 ohms.

The input is provided by a photo-transistor connect to the Arduiono's analogue input on analogue pin 0. The range of the input for the photo-transistor is 0 (full light) to 1023 (full dark), 1024 in total. This output is divided by 128, that is the full range (1024) divided by the number of LEDs (8). As the result is cast as a floating point, we round it up to the nearest integer. So 7.5 become 8. Finally we subtract this result from the total number of LEDs.

A simple for loop is used to enumerate the result and light/extinguish the appropriate number of LEDS. The source below:

int sensorPin = A0;
float sensorValue = 0;
int leds = 8;
int result = 0;
int divisor = 128;

void setup()
{
PORTD = B00000000;
pinMode(0,OUTPUT);
pinMode(1,OUTPUT);
}

void loop() {
sensorValue = analogRead(sensorPin);
result = leds - round(sensorValue / divisor);
for(int i = 0;i < leds;i++) {
if(i < result) {
digitalWrite(i,HIGH);
} else {
digitalWrite(i,LOW);
}
}
}

The results can be seen in this quick video showing how the LEDs react when a torch is brought near the photo-transistor.




Click to enlarge.

No comments:

Post a Comment