Skip to content

EmreDUR/TSL2561_STM32_Library

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 

Repository files navigation

TSL2561 STM32 Library

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)

Features

  • 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.

Usage

  1. Include the "TSL2561.h" header file in your source file
  2. Create a TSL2561 struct and initialize it by calling TSL2561_Init()
  3. Call TSL2561_GetLux() and pass the sensor struct to get the lux value
  4. Use the lower level functions if needed for further configurations

Sensor Address

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.

Connections

  • SCL -> SCL
  • SDA -> SDA
  • VDD -> 3.3V
  • GND -> GND

Example

#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); 
      } 
    } 
  } 
}