Read Light Sensor

Some of the module development boards like the LaunchPad BoosterPack incorporate an ambient light sensor. The ambient light sensor produces an output current in proportion to the amount of light incident on the the detector. This current is converted into a voltage with a resistor, and this voltage is then read by the ADC in the micro-controller. This example reads the amount of ambient light and outputs it to the console. It's a good demonstration of how to use a sensor to measure something useful.



This is one of the few examples that uses a separate helper file to convert the ADC reading from the light sensor into lux. At the top of the file you see: which is the same for all examples. But there's a second file included here: This second file contains the function definitions for the ADC reading to lux conversion. In the example, the code to read the light sensor ADC input and convert it to lux is: uint16_t lightSensor = (convertAdcToLux(getLightSensor)) * 10; The getLightSensor method is from the hal file, but the convertAdcToLux method is from the light sensor hal file (e.g. hal_Osram_SFH5711_ambient_light_sensor.h)
 * 1) include "../HAL/hal.h"
 * 1) include "../HAL/hal_Osram_SFH5711_ambient_light_sensor.h"

There are two versions of the adc to lux conversion: one using a lookup table, and one using math. These are in hal_Osram_SFH5711_ambient_light_sensor.c and are selected by defining LIGHT_SENSOR_USE_LOOKUP_TABLE or LIGHT_SENS0R_USE_MATH. The lookup table version is smaller in code size but less accurate. See the source code for more information.

This demonstrates the "building block" nature of these examples: while this example isn't very useful, it serves as a demonstration of how the light sensor works and makes it easy to incorporate it into a larger application. If you're building a system that has its own sensors, I recommend making a small example like this for each sensor you create a driver for. It's a good way of verifying proper operation of the driver instead of only trying to test inside application code.

Next Example: Read Supply Voltage

Previous: Hello World

Top: Module_Examples