This library provides an easy interface to use the TSL2561 Ambient Light Sensor with STM32 Microcontrollers over I2C.
The Library is created using STM32 HAL Libraries and it is intended to be used specifically in a STM32CubeIDE based project. (It requires "main.h" file generated by STM32Cube)
- Initialize and configure the TSL2561 sensor.
- Get the lux value from the sensor.
- Low level I2C Read and Write functions to access the sensor registers.
- Easy to integrate with any STM32 HAL based project.
- Include the "TSL2561.h" header file in your source file
- Create a TSL2561 struct and initialize it by calling TSL2561_Init()
- Call TSL2561_GetLux() and pass the sensor struct to get the lux value
- Use the lower level functions if needed for further configurations
The sensor address can be either 0x39(default), 0x29 or 0x49 based on the ADDR pin logic level. Pass the correct address based on your sensor wiring.
- SCL -> SCL
- SDA -> SDA
- VDD -> 3.3V
- GND -> GND
#include "TSL2561.h"
#define TSL2561_ADDR 0x39
int main(void)
{
TSL2561 tslSensor;
float luxValue;
char* displayBuffer;
if (HAL_I2C_IsDeviceReady(&hi2c1, TSL2561_ADDR, 1, HAL_MAX_DELAY) == HAL_OK)
{
if (TSL2561_Init(&tslSensor, &hi2c1, TSL2561_ADDR) == HAL_OK)
{
/* Get the light sensor value */
if (TSL2561_GetLux(&tslSensor, &luxValue) == HAL_OK)
{
/* Display the value */
sprintf(displayBuffer, "Light: %0.2f lux", luxValue);
HAL_UART_Transmit(&huart2, (uint8_t*)displayBuffer, strlen(displayBuffer), HAL_MAX_DELAY);
}
}
}
}