光害測量 Sky Quality Meter
使用TSL237S-LF感測器
使用TSL237S-LF感測器
1. Arduino UNO主板 x 1
2. TSL237S-LF 高敏感度光-頻率轉換器(high-sensitivity light-to-frequency converter) x 1
3. 電容0.1uF x 1
[TSL237S-LF 光-頻率轉換器 腳位]
由左至右
pin1 to arduino GND
pin2 to arduino 5V
pin3 to arduino pin2
/* Example: TSL237-LF
TSL237 Arduino pins
GND GND
Vcc +5V
Out Digital pin 2
Wire a 0.1uF capacitator between Vcc and GND close to the sensor
*/
// Pin definitions
# define TSL237 2 // Out of TSL237 connected to Digital pin 2
// Constants
int period = 1000; // Miliseconds of each light frecuency measurement
int ScalingFactor = 0.1; // Scaling factor of this sensor
float area = 0.0092; // Sensing area of TSL237 device (cm2)
float millsec01,millsec02;
// Variables
unsigned long counter = 0; // Counter of measurements during the test
unsigned long currentTime = millis();
unsigned long startTime = currentTime;
volatile long pulses = 0; // Counter of measurements of the TSL237
unsigned long frequency; // Read the frequency from the digital pin (pulses/second)
float irradiance; // Calculated irradiance (uW/cm2)
void setup() {
Serial.begin(9600); // Start and configure the serial port
attachInterrupt(0, PulseCount, RISING);
pinMode(TSL237, INPUT); // Declare the pin such as an input of data
Serial.println("Testing a TSL237 sensor:"); // Splash screen
Serial.println("-------------------------");
Serial.println();
}
void loop(){
millsec01=millis();
getfrequency(); // Request to measure the frequency
getirradiance(); // Request to calculate the irradiance (uW/cm2)
Serial.print(" ");
Serial.print(irradiance); // print the frequency (pulses/second)
Serial.println(" uW/cm2");
pulses = 0; // reset the pulses counter
millsec02=millis();
delay (2000);
}
void PulseCount()
{
pulses++;
}
unsigned long getfrequency () {
noInterrupts();
frequency = pulses /((millsec01- millsec02)/1000); // Calculate the frequency (pulses/second)
interrupts();
return (frequency);
}
float getirradiance () {
irradiance = frequency / area; // Calculate Irradiance (uW/cm2)
return (irradiance);
}
1. 數值最大可達到多少?
/*
Sky Quality Meter
Sensor: TSL237S-LF
Board: Arduino UNO R4 WiFi
*/
// Pin definitions
#define TSL237 2
// Constants
const unsigned long period = 1000; // Measurement period (ms)
float ScalingFactor = 0.1; // 保留原始設定
float area = 0.0092; // Sensing area (cm2)
// Variables
volatile unsigned long pulses = 0;
unsigned long frequency = 0;
float irradiance = 0;
// Interrupt service routine
void PulseCount() {
pulses++;
}
void setup() {
Serial.begin(9600);
pinMode(TSL237, INPUT);
attachInterrupt(
digitalPinToInterrupt(TSL237),
PulseCount,
RISING
);
Serial.println("Testing a TSL237 sensor:");
Serial.println("-------------------------");
Serial.println();
}
void loop() {
// 開始計時
unsigned long startTime = millis();
// 等待完整的測量時間
while (millis() - startTime < period) {
// 等待脈波累積
}
// 計算實際經過時間
unsigned long endTime = millis();
unsigned long elapsedTime = endTime - startTime;
// 安全讀取並清除脈波計數
noInterrupts();
unsigned long pulseCount = pulses;
pulses = 0;
interrupts();
// 計算頻率(pulses/second)
frequency = pulseCount * 1000UL / elapsedTime;
// 計算輻照度(保留原始公式)
getirradiance();
// 輸出結果
Serial.print(irradiance);
Serial.println(" uW/cm2");
}
// Calculate irradiance
float getirradiance() {
irradiance = (float)frequency / area;
return irradiance;
}