Download Print this page

Parallax TSL235R Manual page 2

Light to frequency converter arduino demo

Advertisement

Programming
To use this example, upload LightSensor sketch to your Arduino, then open the Serial Monitor
window. Ensure that the Baud Rate is set at 115200 (note the higher than usual speed used in this
sketch). Aim the front of the light sensor toward various light levels, and watch the value change
in the Serial Monitor window.
You can adjust the values used in the sketch to increase the affective sensitivity of the sensor.
One method is to increase the numeric value in the line
if (millis() - lastRead > 5)
...to something like 20 or 25. This causes the sketch to update the frequency counting less often,
thus accumulating a higher value before displaying it.
High brightness light sources may cause an interruption in the readout through the Serial
Monitor. This is due to how the sketch uses one of the Arduino's external hardware interrupt
pins. When the light is more intense, the frequency of the sensor increases proportionately, and
this causes the interrupt to re-trigger very quickly. The rest of the code in the sketch does not
have the chance to execute, causing a delay until the next value appears in the Serial Monitor
window. If using the light sensor under high brightness conditions cover with a suitable filter to
reduce its overall exposure.
volatile unsigned long count = 0;
unsigned long oldcount = 0;
unsigned long temp = 0;
unsigned long lastRead;
unsigned long avg = 0;
unsigned long freq = 0;
long loopcount = 0;
void setup() {
Serial.begin(115200);
Serial.println("Starting...");
pinMode(2, INPUT);
digitalWrite(2, HIGH);
attachInterrupt(0, count_inc, RISING);
}
void loop() {
if (millis() - lastRead > 5) {
lastRead = millis();
temp = count;
freq = temp - oldcount;
avg = avg + freq;
if (loopcount % 50) {
Serial.println(avg / 50);
avg = 0;
loopcount = 0;
}
loopcount++;
oldcount = temp;
Copyright © Parallax Inc.
// Note higher baud rate
// Set pin 2 to input
// Turn on pullup resistor
Page 2 of 3

Advertisement

loading

This manual is also suitable for:

604-00084