What's the purpose of this project?
The purpose of this project is to interface the LTR-303ALS ambient light sensor with an Arduino to measure and monitor light intensity in various environments. The sensor can accurately detect ambient light levels and provide precise lux values, making it ideal for applications such as automatic lighting control, adaptive screen brightness adjustments, and environmental monitoring. By reading light levels in real-time, users can design systems that respond dynamically to changing lighting conditions, improving energy efficiency and enhancing user experience.
What are we going to learn in this tutorial?
In this tutorial, you’ll learn how to:
- Connect the LTR-303ALS sensor to Arduino and establish I2C communication.
- Use an existing library or write custom code to interface with the sensor, enhancing your understanding of I2C protocols.
- Measure ambient light levels in lux using the sensor, interpreting data for practical applications.
- Develop projects like automated lighting and adaptive brightness control, gaining hands-on experience in creating responsive systems based on light intensity measurements.
This hands-on guide provides insights into sensor integration and real-time data monitoring with Arduino.
What do we need to start this project?
As you probably know, we need some hardware and software to do this project. The titles of these hardware and software are provided to you in the table below and you can prepare/download by clicking on each of them and get ready to start.
Required hardware
|
Required software
|
---|---|
Arduino UNO
|
|
First as shown in the image below, we connect the GebraBit LTR303ALS module to the Arduino UNO as follows:
Then download and add the GebraBit LTR303ALS library to your Arduino IDE.
Required Library
|
---|
If you don’t know how to add GebraBit libraries to Arduino IDE, refer to the tutorial link below.
LTR303ALS library and driver
In addition to the modular design of various sensors and ICs, GebraBit tries to provide variety of structured and hardware-independent libraries in C language for the ease of users in setting up and developing software.
For this purpose, after preparing each GebraBit module, the users can refer to the “tutorial” section of the desired module and download the dedicated library, which contains the “ .h” and “ .c” file (Header and Source) and a sample training program under “GebraBit STM32F303”, “GebraBit ATMEGA32A” or “Arduino” development boards.
All the defined functions and structures in the library are commented in full detail and all the received parameters in the arguments of the functions and their return values, are briefly explained. Since the libraries are hardware independed, the user can easily add the library in any of their favorite compilers and develop it by desired microcontroller and development board.
GebraBit_LTR303ALS.h header file
In this file, based on the datasheet of the sensor or IC, all address registers, the values of each register are defined in the form of “Enumeration”. Also, the casing of the LTR303ALS sensor and the configurations related to each of the LTR303ALS sensor internal blocks are defined in the form of a “STRUCT” with the name GebraBit_LTR303ALS.
USER REGISTER MAP
The registry map or sensor commands are defined in this section:
/************************************************
* USER REGISTER MAP *
***********************************************/
#define LTR303ALS_ALS_CONTR 0x80
#define LTR303ALS_ALS_MEAS_RATE 0x85
#define LTR303ALS_PART_ID 0x86
#define LTR303ALS_MANUFAC_ID 0x87
#define LTR303ALS_ALS_DATA_CH1_0 0x88 ////0xA0 TO 0xAE
#define LTR303ALS_ALS_DATA_CH1_1 0x89
#define LTR303ALS_ALS_DATA_CH0_0 0x8A
#define LTR303ALS_ALS_DATA_CH0_1 0x8B
#define LTR303ALS_ALS_STATUS 0x8C /* I2C Address */
#define LTR303ALS_INTERRUPT 0x8F
#define LTR303ALS_ALS_THRES_UP_0 0x97
#define LTR303ALS_ALS_THRES_UP_1 0x98
#define LTR303ALS_ALS_THRES_LOW_0 0x99
#define LTR303ALS_ALS_THRES_LOW_1 0x9A
#define LTR303ALS_INTERRUPT_PERSIST 0x9E
#define LTR303ALS_ADDRESS 0x29
/*----------------------------------------------*
* USER REGISTER MAP End *
*----------------------------------------------*/
LTR303ALS_ALS_Mode Enum
This enum is used to change the sensor working mode:
typedef enum ALS_Mode
{
STANDBY = 0 ,
ACTIVE
}LTR303ALS_ALS_Mode;
LTR303ALS_ALS_Gain Enum
The values of this enum are used to set the sensor gain:
typedef enum ALS_Gain
{
ALS_GAIN_1X = 0,
ALS_GAIN_2X = 1,
ALS_GAIN_4X = 2,
ALS_GAIN_8X = 3,
ALS_GAIN_48X = 6,
ALS_GAIN_96X = 7,
} LTR303ALS_ALS_Gain;
LTR303ALS_Integration_Time Enum
The values of this enum are used to select the sensor data integration time:
typedef enum Integration_Time
{
ALS_INTEGTIME_100_mS,
ALS_INTEGTIME_50_mS ,
ALS_INTEGTIME_200_mS,
ALS_INTEGTIME_400_mS,
ALS_INTEGTIME_150_mS,
ALS_INTEGTIME_250_mS,
ALS_INTEGTIME_300_mS,
ALS_INTEGTIME_350_mS,
} LTR303ALS_Integration_Time;
LTR303ALS_ Measurement_Rate Enum
The measurement rate of sensor data values determined by this enum values:
typedef enum Measurement_Rate
{
ALS_MEASRATE_50_mS,
ALS_MEASRATE_100_mS,
ALS_MEASRATE_200_mS,
ALS_MEASRATE_500_mS,
ALS_MEASRATE_1000_mS,
ALS_MEASRATE_2000_mS,
} LTR303ALS_Measurement_Rate;
LTR303ALS_Data_Status Enum
The values of this enum determine whether the read data is new or old values:
typedef enum Data_Status
{
OLD_DATA = 0 ,
NEW_DATA
}LTR303ALS_Data_Status;
LTR303ALS_ Interrupt_Status Enum
This enum is used to enable or disable the sensor interrupt:
typedef enum Interrupt_Status
{
INTERRUPT_INACTIVE = 0 ,
INTERRUPT_ACTIVE
}LTR303ALS_Interrupt_Status;
LTR303ALS_Data_Valid Enum
Using this enum, it is determined whether the read data is correct or not:
typedef enum Data_Valid
{
DATA_IS_VALID = 0 ,
DATA_IS_INVALID
}LTR303ALS_Data_Valid;
LTR303ALS_Interrupt_Mode Enum
Using this enum, the sensor interrupt mode is selected:
typedef enum Interrupt_Mode
{
INT_PIN_INACTIVE = 0 ,
INT_PIN_TRIG_INTERRUPT
}LTR303ALS_Interrupt_Mode;
LTR303ALS_Interrupt_Polarity Enum
Using this enum, the interrupt pin logic level (active low or active high) is selected when it occurs:
typedef enum Interrupt_Polarity
{
ACTIVE_LOW = 0 ,
ACTIVE_HIGH
}LTR303ALS_Interrupt_Polarity;
LTR303ALS_Interrupt_Persist Enum
Using this enum, it is determined after how many repetitions of a mode, the interrupt should occur:
typedef enum Interrupt_Persist
{
EVERY_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_2_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_3_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_4_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_5_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_6_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_7_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_8_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_9_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_10_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_11_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_12_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_13_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_14_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_15_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_16_ALS_VALUE_OUT_OF_THR_RANGE,
} LTR303ALS_Interrupt_Persist;
LTR303ALS_Reset_Status Enum
By using this enum, the sensor reset status is determined:
typedef enum
{
FAILED = 0 ,
DONE
}LTR303ALS_Reset_Status;
LTR303ALS struct
1. typedef struct LTR303ALS
2. {
3. uint8_t Register_Cache;
4. uint8_t PART_ID;
5. uint8_t MANUFACTURE_ID;
6. LTR303ALS_Reset_Status RESET;
7. LTR303ALS_ALS_Mode ALS_MODE;
8. LTR303ALS_ALS_Gain ALS_GAIN;
9. uint8_t ALS_GAIN_VALUE;
10. LTR303ALS_Measurement_Rate MEASUREMENT_RATE;
11. LTR303ALS_Integration_Time INTEGRATION_TIME;
12. float INTEGRATION_TIME_VALUE;
13. LTR303ALS_Data_Status DATA_STATUS;
14. LTR303ALS_Interrupt_Status INTERRRUPT_STATUS;
15. LTR303ALS_Data_Valid DATA;
16. LTR303ALS_Interrupt_Mode INTERRUPT_MODE;
17. LTR303ALS_Interrupt_Polarity INTERRUPT_POLARITY;
18. LTR303ALS_Interrupt_Persist INTERRUPT_PERSIST;
19. uint16_t INTERRUPT_UPPER_THRESHOLD;
20. uint16_t INTERRUPT_LOWER_THRESHOLD;
21. uint8_t REGISTER_DATA[REGISTER_DATA_BUFFER_SIZE];
22. uint16_t ALS_DATA_CH1;//Reference to uint16_t where IR-only data will be stored
23. uint16_t ALS_DATA_CH0;//Reference to uint16_t where visible+IR data will be stored
24. double RATIO;
25. double ALS_LUX;
26. }GebraBit_LTR303ALS;
27.
Declaration of functions
At the end of this file, all the functions for reading and writing in LTR303ALS registers, sensor configuration and receiving data from the sensor are declared:
1. extern void GB_LTR303ALS_Read_Reg_Data(uint8_t regAddr, uint8_t *data) ;
2. extern void GB_LTR303ALS_Burst_Read(uint8_t regAddr, uint8_t *data, uint16_t byteQuantity);
3. extern void GB_LTR303ALS_Read_Reg_Bits (uint8_t regAddr, uint8_t start_bit, uint8_t len, uint8_t* data);
4. extern void GB_LTR303ALS_Write_Command( uint8_t cmd);
5. extern void GB_LTR303ALS_Write_Reg_Data(uint8_t regAddr, uint8_t data) ;
6. extern void GB_LTR303ALS_Write_Reg_Bits(uint8_t regAddr, uint8_t start_bit, uint8_t len, uint8_t data);
7. /********************************************************
8. * Declare LTR303ALS Configuration Functions *
9. ********************************************************/
10. extern void GB_LTR303ALS_Soft_Reset ( GebraBit_LTR303ALS * LTR303ALS ) ;
11. extern void GB_LTR303ALS_ALS_Mode ( GebraBit_LTR303ALS * LTR303ALS , LTR303ALS_ALS_Mode als ) ;
12. extern void GB_LTR303ALS_ALS_Gain ( GebraBit_LTR303ALS * LTR303ALS , LTR303ALS_ALS_Gain gain ) ;
13. extern void GB_LTR303ALS_Measurement_Repeat_Rate ( GebraBit_LTR303ALS * LTR303ALS , LTR303ALS_Measurement_Rate rate ) ;
14. extern void GB_LTR303ALS_Integration_Time ( GebraBit_LTR303ALS * LTR303ALS , LTR303ALS_Integration_Time intg ) ;
15. extern void GB_LTR303ALS_Read_Part_ID ( GebraBit_LTR303ALS * LTR303ALS ) ;
16. extern void GB_LTR303ALS_Read_Manufacture_ID ( GebraBit_LTR303ALS * LTR303ALS ) ;
17. extern void GB_LTR303ALS_Read_ALS_STATUS ( GebraBit_LTR303ALS * LTR303ALS ) ;
18. extern void GB_LTR303ALS_Interrupt_Mode ( GebraBit_LTR303ALS * LTR303ALS , LTR303ALS_Interrupt_Mode mode ) ;
19. extern void GB_LTR303ALS_Interrupt_Polarity ( GebraBit_LTR303ALS * LTR303ALS , LTR303ALS_Interrupt_Polarity polar ) ;
20. extern void GB_LTR303ALS_Interrupt_Persist ( GebraBit_LTR303ALS * LTR303ALS , LTR303ALS_Interrupt_Persist persist ) ;
21. extern void GB_LTR303ALS_Interrupt_Upper_Limitation ( GebraBit_LTR303ALS * LTR303ALS , uint16_t limit );
22. extern void GB_LTR303ALS_Interrupt_Lower_Limitation ( GebraBit_LTR303ALS * LTR303ALS , uint16_t limit ) ;
23. extern void GB_LTR303ALS_initialize( GebraBit_LTR303ALS * LTR303ALS ) ;
24. extern void GB_LTR303ALS_Configuration(GebraBit_LTR303ALS * LTR303ALS) ;
25. extern void GB_LTR303ALS_Get_ALS_CH0_CH1_DATA(GebraBit_LTR303ALS * LTR303ALS);
26. extern void GB_LTR303ALS_Lux_Reading(GebraBit_LTR303ALS * LTR303ALS);
27. extern void GB_LTR303ALS_Get_Data(GebraBit_LTR303ALS * LTR303ALS);
GebraBit_LTR303ALS.c source file
In this file, which is written in C language, all the functions are commented in full detail, and all the parameters received in the arguments of the functions and their return values are clearly explained so we confine to these explanations and invite users to check this file directly for more information.
Sample program in Arduino
After connecting the module to Arduino and adding the library to the IDE, go to the following path: File > Examples > GebraBit_LTR303ALS > Luminosity
Description of Sample file
Enums and functions required by GebraBit LTR303ALS module have been added to the structures. In the next part, a variable named LTR303ALS of the GebraBit_LTR303ALS structure type (this structure is in the GebraBit_LTR303ALS header and is explained in the GebraBit_LTR303ALS library description section) is defined for the configuration of the GebraBit LTR303ALS module:
GebraBit_LTR303ALS LTR303ALS;
In the next part of the written code, using the GB_LTR303ALS_initialize (<R303ALS) and GB_LTR303ALS_Configuration (<R303ALS) functions, we set the GebraBit LTR303ALS module and finally, in the loop part of the program, the data is read from the sensor and the Luminosity values are continuously received:
void setup() {
Wire.begin(); // Initialize the I2C bus
Serial.begin(9600); // Initialize serial communication for debugging
GB_LTR303ALS_initialize(<R303ALS); // Initialize the HTU2XD sensor
GB_LTR303ALS_Configuration(<R303ALS); // Configure the HTU2XD sensor
}
void loop() {
GB_LTR303ALS_Get_Data(<R303ALS); // Read data from the sensor
Serial.print("Lux: ");
Serial.print(LTR303ALS.ALS_LUX);
Serial.println(" lx");
delay(2000); // Delay between readings
}
The Sample file code text:
#include "GebraBit_LTR303ALS.h"
GebraBit_LTR303ALS LTR303ALS;
void setup() {
Wire.begin(); // Initialize the I2C bus
Serial.begin(9600); // Initialize serial communication for debugging
GB_LTR303ALS_initialize(<R303ALS); // Initialize the HTU2XD sensor
GB_LTR303ALS_Configuration(<R303ALS); // Configure the HTU2XD sensor
}
void loop() {
GB_LTR303ALS_Get_Data(<R303ALS); // Read data from the sensor
Serial.print("Lux: ");
Serial.print(LTR303ALS.ALS_LUX);
Serial.println(" lx");
delay(2000); // Delay between readings
}
Connect your arduino to computer and select your Arduino Board
Then Verify and Upload the Sample code
After uploading the code, open the serial monitor and you can see the luminosity values.
In the following, you can download the GebraBit_LTR303ALS Library, the schematic of the modules and the “LTR303ALS datasheet”.
Program output video
The video of the module operation will be uploaded soon