Beginner
10 min

Track light intensity with precision using TSL2583 and PIC18F57Q43

From light to digital data

Illuminance Click with Curiosity Nano with PIC18F57Q43

Published Feb 13, 2024

Click board™

Illuminance Click

Dev Board

Curiosity Nano with PIC18F57Q43

Compiler

NECTO Studio

MCU

PIC18F57Q43

Seamlessly integrate light intensity measurements into digital systems, enabling automation, analytics, and enhanced decision-making capabilities

A

A

Hardware Overview

How does it work?

Illuminance Click is based on the TSL2583, a high-sensitivity light-to-digital converter from ams. The TSL2583 combines one broadband photodiode (visible plus infrared) and one infrared-responding photodiode on a single CMOS integrated circuit capable of providing a near-photopic response over an effective 16-bit dynamic range (16-bit resolution). Two integrating analog-to-digital converters (ADC) convert the photodiode currents to a digital output representing the irradiance measured on each channel. Besides general-purpose light sensing applications, the TSL2583 is explicitly designed for displays (LCD, OLED) to extend battery life and provide optimum viewing in diverse lighting conditions. The TSL2583 communicates with the MCU using the standard

I2C 2-Wire interface with a maximum frequency of 400kHz. Besides, it allows choosing the least significant bit (LSB) of its I2C slave address using the SMD jumper labeled I2C ADD. An integration of both ADC channels co-occurs. Upon completion of the conversion cycle, the conversion result is transferred to the Channel 0 and Channel 1 data registers, respectively. The transfers are double-buffered to ensure that the integrity of the data is maintained. After the transfer, the device automatically begins the next integration cycle. This sensor also supports an interrupt feature, routed to the INT pin on the mikroBUS™ socket, that simplifies and improves system efficiency by eliminating the need to poll a sensor for a light intensity value. The purpose of the interrupt

function is to detect a meaningful change in light intensity, where the user can define the concept of a significant change in light intensity and time or persistence. Users can define a threshold above and below the current light level, where an interrupt generates when the conversion value exceeds either of these limits. This Click board™ can be operated only with a 3.3V logic voltage level. The board must perform appropriate logic voltage level conversion before using MCUs with different logic levels. However, the Click board™ comes equipped with a library containing functions and an example code that can be used as a reference for further development.

Illuminance Click hardware overview image

Features overview

Development board

PIC18F57Q43 Curiosity Nano evaluation kit is a cutting-edge hardware platform designed to evaluate microcontrollers within the PIC18-Q43 family. Central to its design is the inclusion of the powerful PIC18F57Q43 microcontroller (MCU), offering advanced functionalities and robust performance. Key features of this evaluation kit include a yellow user LED and a responsive

mechanical user switch, providing seamless interaction and testing. The provision for a 32.768kHz crystal footprint ensures precision timing capabilities. With an onboard debugger boasting a green power and status LED, programming and debugging become intuitive and efficient. Further enhancing its utility is the Virtual serial port (CDC) and a debug GPIO channel (DGI

GPIO), offering extensive connectivity options. Powered via USB, this kit boasts an adjustable target voltage feature facilitated by the MIC5353 LDO regulator, ensuring stable operation with an output voltage ranging from 1.8V to 5.1V, with a maximum output current of 500mA, subject to ambient temperature and voltage constraints.

PIC18F57Q43 Curiosity Nano double side image

Microcontroller Overview

MCU Card / MCU

default

Architecture

PIC

MCU Memory (KB)

128

Silicon Vendor

Microchip

Pin count

48

RAM (Bytes)

8196

You complete me!

Accessories

Curiosity Nano Base for Click boards is a versatile hardware extension platform created to streamline the integration between Curiosity Nano kits and extension boards, tailored explicitly for the mikroBUS™-standardized Click boards and Xplained Pro extension boards. This innovative base board (shield) offers seamless connectivity and expansion possibilities, simplifying experimentation and development. Key features include USB power compatibility from the Curiosity Nano kit, alongside an alternative external power input option for enhanced flexibility. The onboard Li-Ion/LiPo charger and management circuit ensure smooth operation for battery-powered applications, simplifying usage and management. Moreover, the base incorporates a fixed 3.3V PSU dedicated to target and mikroBUS™ power rails, alongside a fixed 5.0V boost converter catering to 5V power rails of mikroBUS™ sockets, providing stable power delivery for various connected devices.

Curiosity Nano Base for Click boards accessories 1 image

Used MCU Pins

mikroBUS™ mapper

NC
NC
AN
NC
NC
RST
NC
NC
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
NC
NC
PWM
Interrupt
PA6
INT
NC
NC
TX
NC
NC
RX
I2C Clock
PB1
SCL
I2C Data
PB2
SDA
NC
NC
5V
Ground
GND
GND
1

Take a closer look

Schematic

Illuminance Click Schematic schematic

Step by step

Project assembly

Curiosity Nano Base for Click boards front image hardware assembly

Start by selecting your development board and Click board™. Begin with the Curiosity Nano with PIC18F57Q43 as your development board.

Curiosity Nano Base for Click boards front image hardware assembly
Barometer 13 Click front image hardware assembly
PIC18F57Q43 Curiosity Nano front image hardware assembly
Prog-cut hardware assembly
Curiosity Nano with PICXXX MB 1 - upright/background hardware assembly
Necto image step 2 hardware assembly
Necto image step 3 hardware assembly
Necto image step 4 hardware assembly
Necto image step 5 hardware assembly
Necto image step 6 hardware assembly
PIC18F57Q43 Curiosity MCU Step hardware assembly
Necto No Display image step 8 hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Debug Image Necto Step hardware assembly

Track your results in real time

Application Output

1. Once the code example is loaded, pressing the "DEBUG" button initiates the build process, programs it on the created setup, and enters Debug mode.

2. After the programming is completed, a header with buttons for various actions within the IDE becomes visible. Clicking the green "PLAY" button starts reading the results achieved with the Click board™. The achieved results are displayed in the Application Output tab.

DEBUG_Application_Output

Software Support

Library Description

This library contains API for Illuminance Click driver.

Key functions:

  • illuminance_set_atime - This function sets the timing register for the selected integration time

  • illuminance_set_gain - This function sets the gain level

  • illuminance_read_raw_data - This function checks if the data is ready and then reads the raw ADC data from two channels

Open Source

Code example

This example can be found in NECTO Studio. Feel free to download the code, or you can copy the code below.

/*!
 * \file 
 * \brief Illuminance Click example
 * 
 * # Description
 * This example demonstrates basic Illuminance Click functionality.
 *
 * The demo application is composed of two sections :
 * 
 * ## Application Init 
 * Initialize device and driver.
 * 
 * ## Application Task  
 * Every second calculate illuminance measured by sensor and log 
 * results to UART Terminal.
 * 
 * *note:* 
 * By default, integration time is set to 402ms but it may be modified
 * by user using illuminance_write_data() function and provided macros.
 * 
 * \author MikroE Team
 *
 */
// ------------------------------------------------------------------- INCLUDES

#include "board.h"
#include "log.h"
#include "illuminance.h"

// ------------------------------------------------------------------ VARIABLES

static illuminance_t illuminance;
static log_t logger;

static uint16_t value_ch0;
static uint16_t value_ch1;
static uint16_t lux_value;
static uint16_t lux_value_old;
static uint8_t sensitivity;

// ------------------------------------------------------ APPLICATION FUNCTIONS

void application_init ( void )
{
    log_cfg_t log_cfg;
    illuminance_cfg_t cfg;

    /** 
     * Logger initialization.
     * Default baud rate: 115200
     * Default log level: LOG_LEVEL_DEBUG
     * @note If USB_UART_RX and USB_UART_TX 
     * are defined as HAL_PIN_NC, you will 
     * need to define them manually for log to work. 
     * See @b LOG_MAP_USB_UART macro definition for detailed explanation.
     */
    LOG_MAP_USB_UART( log_cfg );
    log_init( &logger, &log_cfg );
    log_info( &logger, "---- Application Init ----" );

    //  Click initialization.

    illuminance_cfg_setup( &cfg );
    ILLUMINANCE_MAP_MIKROBUS( cfg, MIKROBUS_1 );
    illuminance_init( &illuminance, &cfg );
    illuminance_default_cfg ( &illuminance );

    // Variable Initializations for this example.
    
    lux_value_old = 0;
    sensitivity = 50;
}

void application_task ( void )
{
    illuminance_get_result( &illuminance, &value_ch0, &value_ch1 );

    lux_value = illuminance_calculate_lux( ILLUMINANCE_TSL2561_GAIN_0X, ILLUMINANCE_TSL2561_INTEGRATIONTIME_402MS , value_ch0, value_ch1 );
    Delay_ms( 1000 );

    if ( ( ( lux_value - lux_value_old ) > sensitivity ) && ( ( lux_value_old - lux_value ) > sensitivity ) )
    {
        log_printf( &logger, "\r\n--------------------------------" );
        log_printf( &logger, "\r\nFull  Spectrum: %u [ lux ]", lux_value );
        log_printf( &logger, "\r\nVisible  Value: %u [ lux ]", value_ch0 - value_ch1 );
        log_printf( &logger, "\r\nInfrared Value: %u [ lux ]", value_ch1 );    
        log_printf( &logger, "\r\n--------------------------------\r\n" );
        
        lux_value_old = lux_value;
    }
}

void main ( void )
{
    application_init( );

    for ( ; ; )
    {
        application_task( );
    }
}

// ------------------------------------------------------------------------ END

Additional Support

Resources

Love this project?

'Buy This Kit' button takes you directly to the shopping cart where you can easily add or remove products.