Ambient Light Sensor
Goals
You know how to get a handle on the ambient light sensor connected to your computer
You can get the sensor's values and get frequently notified about new values
Get the ambient light sensor and subscribe to updates
Working with an Ambient Light 2.0 sensor works as described in the general article on how to use sensors. In this article, we assume you have successfully initialized the devices with the Tinkerforge Device Manager, and you stored all connected devices on a global variable devices
. We also declared a global variable called ambientLightSensor
.
Next, we need to know the device identifier, which 259:
// Get the ambient light sensor via its device identifer
ambientLightSensor = devices.getDeviceByIdentifier(259);
Once we have a reference to the sensor, we can register a callback function:
// We want to be informed when a new sensor value arrives
ambientLightSensor.registerListener(ambientLightChanged);
Of course, we have to actually define the function:
function ambientLightChanged(val) {
// Do something with the value object
}
Reading the light value
The sensor delivers only one value, which we can access via the val
object. The value is the light intensity measured in Lux multiplied by 100:
function ambientLightChanged(val) {
// Get the value
var value = val.getValue();
log(value.value / 100.0 + " lx");
}
Last updated
Was this helpful?