void setup()
{
// inisiasi Serial comm dengan baud rate 115200
Serial.begin(115200);
}
void loop()
{
// cetak nilai lux ke serial monitor
Serial.print("Nilai LUX :");
Serial.println(LightSensorLDR());
Serial.println("");
// jeda akuisisi data
delay(1000);
}
float LightSensorLDR (){
// These constants should match the photoresistor's "gamma" and "rl10" attributes
const float GAMMA = 0.7;
const float RL10 = 50;
// Convert the analog value into lux value:
int analogValue = analogRead(A0);
float voltage = (analogValue / 4096.)* 3.3;
float resistance = 3030. * voltage / (1 - (voltage / 3.3));
double lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
if(lux<1){
lux = lux;
}else if(lux < floor(lux)+0.90){
lux = floor(lux);
}else{
lux = ceil(lux);
}
return lux;
}