light4 2.1.0.0
Main Page

Light 4 click

PRVIH_PAR_RECENICA_SA_PRODUCT_PAGE_DA_ISPRATE_CELINU

[click Product page](CLICK_PRODUCT_PAGE_LINK)


Click library

  • Author : Stefan Ilic
  • Date : Feb 2024.
  • Type : I2C type

Software Support

We provide a library for the Light 4 Click as well as a demo application (example), developed using MikroElektronika compilers. The demo can run on all the main MikroElektronika development boards.

Package can be downloaded/installed directly from NECTO Studio Package Manager(recommended way), downloaded from our LibStock™ or found on Mikroe github account.

Library Description

This library contains API for Light 4 Click driver.

Standard key functions :

Example key functions :

  • light4_write_reg This function writes a byte into the selected register by using I2C serial interface.
    err_t light4_write_reg ( light4_t *ctx, uint8_t reg, uint8_t data_in );
    err_t light4_write_reg(light4_t *ctx, uint8_t reg, uint8_t data_in)
    Light 4 Register writing function.
  • light4_sw_reset This function is used to perform software reset of Light 4 click board.
    err_t light4_sw_reset ( light4_t *ctx );
    err_t light4_sw_reset(light4_t *ctx)
    Light 4 Software reset function.
  • light4_read_channel_data This function is used to read data from selected channel of Light 4 click board.
    err_t light4_read_channel_data ( light4_t *ctx, uint8_t channel_sel, float *channel_data );
    err_t light4_read_channel_data(light4_t *ctx, uint8_t channel_sel, float *channel_data)
    Light 4 get channel data function.

Example Description

This example demonstrates the use of Light 4 Click board by measuring

the ambient light level in Lux.

The demo application is composed of two sections :

Application Init

Initializes the driver, performs the click default configuration

and checking I2C Communication by reading Device ID.

void application_init ( void )
{
log_cfg_t log_cfg;
light4_cfg_t light4_cfg;
LOG_MAP_USB_UART( log_cfg );
log_init( &logger, &log_cfg );
log_info( &logger, " Application Init " );
// Click initialization.
light4_cfg_setup( &light4_cfg );
LIGHT4_MAP_MIKROBUS( light4_cfg, MIKROBUS_1 );
if ( I2C_MASTER_ERROR == light4_init( &light4, &light4_cfg ) )
{
log_error( &logger, " Communication init." );
for ( ; ; );
}
if ( LIGHT4_ERROR == light4_default_cfg ( &light4 ) )
{
log_error( &logger, " Default configuration." );
for ( ; ; );
}
uint8_t dev_id = 0;
light4_read_reg ( &light4, LIGHT4_REG_ID, &dev_id );
if ( LIGHT4_DEVICE_ID == dev_id )
{
log_printf( &logger, " Device ID: 0x%.2X \r\n", ( uint16_t ) dev_id );
}
else
{
log_error( &logger, " Read error." );
for ( ; ; );
}
log_info( &logger, " Application Task " );
}
#define LIGHT4_MAP_MIKROBUS(cfg, mikrobus)
MikroBUS pin mapping.
Definition: light4.h:263
#define LIGHT4_REG_ID
Definition: light4.h:89
#define LIGHT4_DEVICE_ID
Definition: light4.h:230
err_t light4_read_reg(light4_t *ctx, uint8_t reg, uint8_t *data_out)
Light 4 Register reading function.
@ LIGHT4_ERROR
Definition: light4.h:319
void application_init(void)
Definition: main.c:30

Application Task

Reading channel 0 ambient light level in lux once per

second and displaying it on the UART terminal.

void application_task ( void )
{
float channel_data = 0;
err_t error_flag;
error_flag = light4_read_channel_data( &light4, LIGHT4_CHANNEL0_SEL, &channel_data );
if( LIGHT4_OK == error_flag )
{
log_printf( &logger, " Data: %.2f Lux \r\n", channel_data );
}
else if ( LIGHT4_ANALOG_SAT == error_flag )
{
log_error( &logger, " Analog saturation \r\n" );
}
Delay_ms( 1000 );
}
#define LIGHT4_CHANNEL0_SEL
Light 4 Channel selection setting.
Definition: light4.h:236
@ LIGHT4_OK
Definition: light4.h:318
@ LIGHT4_ANALOG_SAT
Definition: light4.h:320
void application_task(void)
Definition: main.c:77

The full application code, and ready to use projects can be installed directly from NECTO Studio Package Manager(recommended way), downloaded from our LibStock™ or found on Mikroe github account.

Other Mikroe Libraries used in the example:

  • MikroSDK.Board
  • MikroSDK.Log
  • Click.Light4

Additional notes and informations

Depending on the development board you are using, you may need USB UART click, USB UART 2 Click or RS232 Click to connect to your PC, for development systems with no UART to USB interface available on the board. UART terminal is available in all MikroElektronika compilers.