Skip to content

Instantly share code, notes, and snippets.

@hydrangeas
Created March 18, 2012 15:16
Show Gist options
  • Save hydrangeas/2075202 to your computer and use it in GitHub Desktop.
Save hydrangeas/2075202 to your computer and use it in GitHub Desktop.
TSL230R
// Reports the frequency from the TSL230, higher number means brighter
// Part: http://www.sparkfun.com/products/8940
// Article: http://bildr.org/2011/08/tsl230r-arduino/
int TSL230_Pin = 4; //TSL230 output
int TSL230_s0 = 3; //TSL230 sensitivity setting 1
int TSL230_s1 = 2; //TSL230 sensitivity setting 2
int TSL230_samples = 6; //higher = slower but more stable and accurate
void setup(){
Serial.begin(9600);
setupTSL230();
}
void loop(){
float lightLevel = readTSL230(TSL230_samples);
Serial.println(lightLevel);
}
void setupTSL230(){
pinMode(TSL230_s0, OUTPUT);
pinMode(TSL230_s1, OUTPUT);
//configure sensitivity - Can set to
//S1 LOW | S0 HIGH: low
//S1 HIGH | S0 LOW: med
//S1 HIGH | S0 HIGH: high
////
// PostScripted on March 18th, 2012
// According to DataSheets,
//S1 LOW | S0 LOW : Power down for sensitivity
//S1 LOW | S0 HIGH: 1X
//S1 HIGH | S0 LOW : 10X
//S1 HIGH | S0 HIGH: 100X
digitalWrite(TSL230_s1, LOW);
digitalWrite(TSL230_s0, HIGH);
}
float readTSL230(int samples){
//sample light, return reading in frequency
//higher number means brighter
float start = micros();
int readings = 0;
while(readings < samples){
pulseIn(TSL230_Pin, HIGH);
readings ++;
}
float length = micros() - start;
float freq = (1000000 / (length / samples)) * 10;
return freq;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment