Skip to content

Light sensor

Description

LTR-303 is a digital ambient light sensor. Communication is carried over I2C interface. Dynamic range spans from 0.01 lux to 64000 lux with different configuration options. Interrupt line provides asynchronous data readout capability.

Part details

Manufacturer: Lite-On Technology, Inc.

Part number: LTR-303ALS-01

Datasheet: link

Code example

Compiling example

This example is based on Mbed OS and requires a simple setup. For full description check Code Setup page.

LTR303 driver

Mbed OS driver for SensorIO can be found here: LTR303 on Github.

Code

In this example IRQ line is used to provide callback with lux value to user. Please mind that sensor's callback is executed in the context of sensor's own thread.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "mbed_events.h"
#include "Serial.h"
#include "ltr303.h"

mbed::Serial console(USBTX, USBRX, 115200);
mbed::I2C iic(SENSORS_SDA, SENSORS_SCL);
mbed::InterruptIn irq(LTR303_INT, PinMode::PullUp);
events::EventQueue scheduler;

Ltr303 sensor(iic, irq);

void measDone(float);

// callback from driver in separate execution context
void dataCallback(float result)
{
    scheduler.call_in(500, measDone, result);
}

// executor in this context
void measDone(float result)
{
    console.printf("Light sensor output: %5.3f lux.\n", result);
    // request next measurement
    sensor.get(dataCallback);
}

int main(int argc, char **argv)
{
    console.printf("This is SensorIO\r\n");

    // schedule first measurement, it will chain next in callback
    sensor.get(dataCallback);

    // running event loop
    scheduler.dispatch_forever();
}

Example main.cpp file can be downloaded from here.