Ambient Light

Note: The code snippets in this article are intended for Mita version 1a1ed7881e2220895ec60ce4f657972538c43195 and XDK-Workbench versions 3.4.0 and higher.

This article introduces the configuration and use of the Ambient Light Sensor in Mita. The article’s focus is on the software and API provided by the platform. For more specific information about the hardware itself, specifically the Light Sensor MAX44009 used by the XDK, please read the corresponding MAX44009 datasheet.

  1. Ambient Light API Overview
  2. Reading Ambient Light Data
  3. Configuring the Ambient Light Sensor
    1. Manual Mode
    2. Integration Time
    3. Continuous Mode
    4. High Brightness
  4. Full Code Example
  5. Appendix
    1. XDK Console Output example

Ambient Light API Overview

Mita provides the possibility to use the Ambient Light Sensor resource without setting it up beforehand. If the configuration and implementation provided by Mita are insufficient for a certain purpose, it is recommended to make adapting changes in the generated C code.

The Ambient Light Sensor implementation in Mita is based on the Platform Sensor library, which is included in the XDK SDK. It contains modules for a range of sensors and virtual sensors. The image below illustrates the Sensor Stack.


Reading Ambient Light Data

Reading the Ambient Light data is fairly simple, as it does not require any kind of initialization beforehand.

The following code will read the sensor value of the ambient light sensor and print the value to the console every 1 second.

every 1 second {
    var light =;
    println(`Light: ${light}`);

Configuring the Ambient Light Sensor

This section provides a brief overview of the configuration for the Ambient Light Sensor. For more information on specific settings, please refer to the MAX44009 datasheet.

The following code offers an example for how to set up the Light Sensor.

setup light {
    manual_mode = true;
    continuous_mode = true;
    high_brightness = true;
    integration_time = MS_800;

Individual configuration-items are elaborated upon in the following sub-chapters.

Manual Mode

The Light Sensor MAX44009 will configure itself automatically per default. If any of the other configuration-items should be set, then the manual mode should be set to true. Otherwise, it should be left as false.

The configuration item manual_mode can be set to one of the following values:

  • true
  • false (default)

Integration Time

The integration time setting defines how long the Ambient Light Sensor collects light for a single measurement. This directly affects how much time a single measurement will take. If manual mode is not activated, the light sensor automatically sets the integration time.

The configuration item integration_time can be set to one of the following values:

  • MS_800 (default)
  • MS_400
  • MS_200
  • MS_100
  • MS_50
  • MS_25
  • MS_12_5
  • MS_6_25

This configuration item is only used if manual_mode is set to true.

Continuous Mode

Per default, the light sensor will only measure the light intensity once every 800 milliseconds, even if an integration time lower than 800ms is set. If the continuous mode is activated, the light sensor will continuously measure the light intensity. That means, once a measurement is finished and the result stored, the next one is immediately started.

The configuration item continuous_mode can be set to one of the following values:

  • true
  • false (default)

This configuration item is only used if manual_mode is set to true.

High Brightness

This setting activates the high brightness mode. In this mode, only 1/8 of the photo diode current is used as input for the Analog to Digital Converter (ADC). This setting is used in high-brightness situations to avoid saturation/clipping effects of the ADC.

The configuration item high_brightness can be set to one of the following values:

  • true
  • false (default)

This configuration item is only used if manual_mode is set to true.

Full Code Example

package main;
import platforms.xdk110;

setup light {
    manual_mode = false;

every 100 milliseconds {
    var light : double =;
    println(`${light} lux`);


XDK Console Output example

The following console log is an example output of the full code example above.