Try It: Lux Level Explorer
Try Keep Light level (lux) at 350 and switch Scale from Linear to Logarithmic.
Observe At 350 lux, the classification is Normal (home/office) and LED Backlight PWM is 155 of 255. Scale changes the bar length while those readouts stay the same.
Explain Light level (lux) selects a fixed brightness band; Scale changes whether the bar uses a direct ratio or a base-10 logarithm.
Calculate light response
Reset
Technical boundaries. The explorer assumes fixed lux thresholds and discrete PWM values rather than a continuous brightness response. Its range warning uses a fixed 65,535-lux cutoff and does not model changes to sensor measurement time.
Adjust the lux value to explore the example light levels. Compare it with the reference table below. The explorer warns that values above 65,535 lux need another sensor, but that is a limit of this simplified example: the BH1750 can extend its range to 100,000 lux by adjusting measurement time. Check the mode and data sheet before choosing a sensor for bright light.
viewof lux_input = Inputs. range ([0 , 120000 ], {value : 350 , step : 1 , label : "Light level (lux)" })
viewof lux_scale = Inputs. radio (["Linear" , "Logarithmic" ], {value : "Linear" , label : "Scale" })
{
let classification, color, pwmBrightness;
if (lux_input < 1 ) {
classification = "Darkness (moonlight)" ;
color = "#2C3E50" ;
pwmBrightness = 0 ;
} else if (lux_input < 50 ) {
classification = "Very dim (street lighting)" ;
color = "#9B59B6" ;
pwmBrightness = 35 ;
} else if (lux_input < 200 ) {
classification = "Dim (hallway)" ;
color = "#7F8C8D" ;
pwmBrightness = 95 ;
} else if (lux_input < 500 ) {
classification = "Normal (home/office)" ;
color = "#16A085" ;
pwmBrightness = 155 ;
} else if (lux_input < 2000 ) {
classification = "Bright (retail/overcast)" ;
color = "#3498DB" ;
pwmBrightness = 205 ;
} else if (lux_input < 25000 ) {
classification = "Very bright (daylight)" ;
color = "#E67E22" ;
pwmBrightness = 245 ;
} else {
classification = "Extremely bright (direct sun)" ;
color = "#E74C3C" ;
pwmBrightness = 255 ;
}
const maxLux = 120000 ;
const barPct = lux_scale === "Logarithmic"
? (lux_input > 0 ? (Math . log10 (lux_input) / Math . log10 (maxLux)) * 100 : 0 )
: (lux_input / maxLux) * 100 ;
return html `<div style="font-family: system-ui, sans-serif; background: #f8f9fa; border-radius: 8px; padding: 1.2rem; margin-top: 0.5rem;">
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(8rem, 1fr)); gap: 0.8rem; margin-bottom: 1rem;">
<div style="background: ${ color} ; color: white; padding: 0.8rem; border-radius: 6px; text-align: center;">
<div style="font-size: 0.8rem; opacity: 0.8;">Classification</div>
<div style="font-size: 1.1rem; font-weight: bold;"> ${ classification} </div>
</div>
<div style="background: #2C3E50; color: white; padding: 0.8rem; border-radius: 6px; text-align: center;">
<div style="font-size: 0.8rem; opacity: 0.8;">BH1750 Reading</div>
<div style="font-size: 1.4rem; font-weight: bold;"> ${ lux_input. toLocaleString ()} lux</div>
<div style="font-size: 0.75rem; opacity: 0.7;"> ${ lux_input > 65535 ? "Beyond BH1750 range!" : "Within sensor range" } </div>
</div>
<div style="background: #16A085; color: white; padding: 0.8rem; border-radius: 6px; text-align: center;">
<div style="font-size: 0.8rem; opacity: 0.8;">LED Backlight PWM</div>
<div style="font-size: 1.4rem; font-weight: bold;"> ${ pwmBrightness} </div>
<div style="font-size: 0.75rem; opacity: 0.7;">of 255 (brighter in brighter ambient light)</div>
</div>
</div>
<div style="margin-bottom: 0.8rem;">
<div style="font-size: 0.8rem; color: #555; margin-bottom: 0.3rem;">Light intensity ( ${ lux_scale. toLowerCase ()} scale)</div>
<div style="background: #ddd; border-radius: 4px; height: 24px; position: relative;">
<div style="background: ${ color} ; border-radius: 4px; height: 100%; width: ${ barPct. toFixed (1 )} %; transition: width 0.3s; min-width: 2px;"></div>
</div>
</div>
<div style="font-size: 0.85rem; color: #555; background: white; padding: 0.6rem; border-radius: 4px;">
<strong>Adaptive backlight logic:</strong> As ambient light increases, screen backlight brightness increases to keep the display readable; dimming it in low light saves power and avoids glare.
${ lux_input > 65535 ? html `<br><span style="color: #E74C3C; font-weight: bold;">Note: BH1750 max range is 65,535 lux. Values above this require a different sensor.</span>` : '' }
</div>
</div>` ;
}
Back to the chapter