Intermediate
30 min

Create advanced optical sensning solution based on TMD37253 and PIC18F96J94

Precision in every shade: The future of light sensing

Light mix-sens Click with UNI-DS v8

Published Sep 24, 2023

Click board™

Light mix-sens Click

Dev Board

UNI-DS v8

Compiler

NECTO Studio

MCU

PIC18F96J94

Experience the ultimate in sensor fusion with our all-in-one solution, providing precise proximity, color, and ambient light measurements for enhanced user experiences

A

A

Hardware Overview

How does it work?

Light mix-sens Click is based on the TMD37253 slim module, from ams OSRAM, incorporates an IR LED and factory calibrated LED driver. The proximity detection feature provides object detection (e.g. mobile device screen to the user’s ear) by photodiode detection of reflected IR energy (sourced by the integrated LED). Detect/release events are interrupt-driven, and occur when proximity result crosses upper and/or lower threshold settings. The proximity engine features offset adjustment registers to compensate for unwanted IR energy reflection at the sensor. Proximity results are further improved by automatic ambient light subtraction. The ALS detection feature provides photopic light intensity data. The color photodiodes have UV and IR blocking filters and a dedicated data converters producing 16-bit data. This architecture allows applications to accurately measure ambient light which enables devices to calculate illuminance and color temperature to control display backlight and chromaticity. Proximity results are affected

by three fundamental factors: the integrated IR LED emission, IR reception, and environmental factors, including target distance and surface reflectivity. The IR reception signal path begins with IR detection from a photodiode and ends with the 8-bit proximity result in the PDATA register. A signal from the photodiode is amplified, and offset adjusted to optimize performance. Offset correction or cross-talk compensation is accomplished by adjustment to the POFFSET register. The analog circuitry of the device applies the offset value as a subtraction to the signal accumulation; therefore a positive offset value has the effect of decreasing the results. The color and ALS reception signal path begins as photodiodes receive filtered light and ends with 16-bit results. The IR photodiode primarily used for proximity sensing is multiplexed with the green channel’s ADC to measure the IR content of ambient light. The color photodiodes are filtered with UV and IR filters. The IR photodiode is filtered to receive only IR. A signal from the RGBC photodiodes

simultaneously accumulate for a period of time set by the value in ATIME before the results are available. Measurement of IR must be done in a separate integration because it shares the ADC with the green photodiode. Gain is adjustable from 1x to 128 x to facilitate the operation over a wide range of lighting conditions. Custom LUX equations are used to calculate the amount of ambient light, color temperature, as well as, determine the light type (e.g. LED, fluorescent, incandescent, etc.) using the ALS results. The TMD37253 module operates at 1.8V power supply with 1.8V I2C bus for reduced power consumption. For integration on Mikrobus, complete voltage regulation and logic level translation has been implemented. Therefore, this Click Board™ is designed to be operated only with a 3.3V logic level. A proper logic voltage level conversion should be performed before the Click board™ is used with MCUs with logic levels of 5V.

Light mix-sens Click top side image
Light mix-sens Click bottom side image

Features overview

Development board

UNI-DS v8 is a development board specially designed for the needs of rapid development of embedded applications. It supports a wide range of microcontrollers, such as different STM32, Kinetis, TIVA, CEC, MSP, PIC, dsPIC, PIC32, and AVR MCUs regardless of their number of pins, and a broad set of unique functions, such as the first-ever embedded debugger/programmer over WiFi. The development board is well organized and designed so that the end-user has all the necessary elements, such as switches, buttons, indicators, connectors, and others, in one place. Thanks to innovative manufacturing technology, UNI-DS v8 provides a fluid and immersive working experience, allowing access anywhere and under any

circumstances at any time. Each part of the UNI-DS v8 development board contains the components necessary for the most efficient operation of the same board. An advanced integrated CODEGRIP programmer/debugger module offers many valuable programming/debugging options, including support for JTAG, SWD, and SWO Trace (Single Wire Output)), and seamless integration with the Mikroe software environment. Besides, it also includes a clean and regulated power supply module for the development board. It can use a wide range of external power sources, including a battery, an external 12V power supply, and a power source via the USB Type-C (USB-C) connector. Communication options such as USB-UART, USB

HOST/DEVICE, CAN (on the MCU card, if supported), and Ethernet is also included. In addition, it also has the well-established mikroBUS™ standard, a standardized socket for the MCU card (SiBRAIN standard), and two display options for the TFT board line of products and character-based LCD. UNI-DS v8 is an integral part of the Mikroe ecosystem for rapid development. Natively supported by Mikroe software tools, it covers many aspects of prototyping and development thanks to a considerable number of different Click boards™ (over a thousand boards), the number of which is growing every day.

UNI-DS v8 horizontal image

Microcontroller Overview

MCU Card / MCU

default

Type

8th Generation

Architecture

PIC

MCU Memory (KB)

64

Silicon Vendor

Microchip

Pin count

100

RAM (Bytes)

3862

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
PB0
INT
NC
NC
TX
NC
NC
RX
I2C Clock
PC3
SCL
I2C Data
PC4
SDA
NC
NC
5V
Ground
GND
GND
1

Take a closer look

Schematic

Light mix-sens Click Schematic schematic

Step by step

Project assembly

Fusion for PIC v8 front image hardware assembly

Start by selecting your development board and Click board™. Begin with the UNI-DS v8 as your development board.

Fusion for PIC v8 front image hardware assembly
Buck 22 Click front image hardware assembly
SiBRAIN for PIC32MZ1024EFK144 front image hardware assembly
v8 SiBRAIN 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 Compiler Selection Step Image hardware assembly
NECTO Output Selection Step Image hardware assembly
Necto image step 6 hardware assembly
Necto image step 7 hardware assembly
Necto image step 8 hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Necto PreFlash Image hardware assembly

Track your results in real time

Application Output

After pressing the "FLASH" button on the left-side panel, it is necessary to open the UART terminal to display the achieved results. By clicking on the Tools icon in the right-hand panel, multiple different functions are displayed, among which is the UART Terminal. Click on the offered "UART Terminal" icon.

UART Application Output Step 1

Once the UART terminal is opened, the window takes on a new form. At the top of the tab are two buttons, one for adjusting the parameters of the UART terminal and the other for connecting the UART terminal. The tab's lower part is reserved for displaying the achieved results. Before connecting, the terminal has a Disconnected status, indicating that the terminal is not yet active. Before connecting, it is necessary to check the set parameters of the UART terminal. Click on the "OPTIONS" button.

UART Application Output Step 2

In the newly opened UART Terminal Options field, we check if the terminal settings are correct, such as the set port and the Baud rate of UART communication. If the data is not displayed properly, it is possible that the Baud rate value is not set correctly and needs to be adjusted to 115200. If all the parameters are set correctly, click on "CONFIGURE".

UART Application Output Step 3

The next step is to click on the "CONNECT" button, after which the terminal status changes from Disconnected to Connected in green, and the data is displayed in the Received data field.

UART Application Output Step 4

Software Support

Library Description

This library contains API for Light mix-sens Click driver.

Key functions:

  • lightmixsens_write_byte - Generic Write Byte function

  • lightmixsens_read_byte - Generic Read Byte function

  • lightmixsens_switch_ir_to_prox - Switch IR To Proximity function

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 main.c
 * @brief LightMixSens Click example
 * 
 * # Description
 * This example show usage of Light Mix Sens Click. It switches the IR light for separate and 
 * measure sectar of RGB lights. Click also measure proximity from the object using light source.
 *
 * The demo application is composed of two sections :
 * 
 * ## Application Init 
 * Initializes all necessary peripherals and pins, initializes I2C driver and performs 
 * the Click board default configuration to allow ALS/Color and Proximity measurements.
 * 
 * ## Application Task  
 * Waits until ALS/Color integration cycle was done and then reads the entire measurement.
 * The all results will be sent to the selected UART terminal.
 * 
 * ## Additional Functions :
 * - prox_app - This is application function which determines the proximity results.
 * 
 *
 * @author MikroE Team
 *
 */

#include "board.h"
#include "log.h"
#include "lightmixsens.h"


static lightmixsens_t lightmixsens;
static log_t logger;
lightmixsens_data_obj lightmixsens_data;
char prox_str[ 20 ];

// ------------------------------------------------------- ADDITIONAL FUNCTIONS

/**
 * @brief Light mix sens proximity function.
 * @details This is function which determines the proximity results.
 */
void prox_app ( void );

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

void application_init ( void )
{
    log_cfg_t log_cfg;
    lightmixsens_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.

    lightmixsens_cfg_setup( &cfg );
    LIGHTMIXSENS_MAP_MIKROBUS( cfg, MIKROBUS_1 );
    lightmixsens_init( &lightmixsens, &cfg );

    lightmixsens_default_cfg( &lightmixsens );

    lightmixsens_data.lightmixsens_cdata = LIGHTMIXSENS_DUMMY_DATA;
    lightmixsens_data.lightmixsens_rdata = LIGHTMIXSENS_DUMMY_DATA;
    lightmixsens_data.lightmixsens_gdata = LIGHTMIXSENS_DUMMY_DATA;
    lightmixsens_data.lightmixsens_bdata = LIGHTMIXSENS_DUMMY_DATA;
    lightmixsens_data.lightmixsens_pdata = LIGHTMIXSENS_DUMMY_DATA;

    log_printf( &logger, "*  Light mix-sens click initialization done.  *\r\n" );
}

void application_task ( void )
{
    lightmixsens_wait_atime( &lightmixsens );
    
    lightmixsens_read_word( &lightmixsens, LIGHTMIXSENS_REG_CDATA, 
                            &lightmixsens_data.lightmixsens_cdata );
    lightmixsens_read_word( &lightmixsens, LIGHTMIXSENS_REG_RDATA, 
                            &lightmixsens_data.lightmixsens_rdata );
    lightmixsens_read_word( &lightmixsens, LIGHTMIXSENS_REG_GDATA_IRDATA, 
                            &lightmixsens_data.lightmixsens_gdata );
    lightmixsens_read_word( &lightmixsens, LIGHTMIXSENS_REG_BDATA, 
                            &lightmixsens_data.lightmixsens_bdata );
    lightmixsens_read_byte( &lightmixsens, LIGHTMIXSENS_REG_PDATA, 
                            &lightmixsens_data.lightmixsens_pdata );
    
    log_printf( &logger, "- Clear light:  %.3d lx\r\n", lightmixsens_data.lightmixsens_cdata );
    log_printf( &logger, "- Red light:    %.3d lx\r\n", lightmixsens_data.lightmixsens_rdata );
    log_printf( &logger, "- Green light:  %.3d lx\r\n", lightmixsens_data.lightmixsens_gdata );
    log_printf( &logger, "- Blue light:   %.3d lx\r\n", lightmixsens_data.lightmixsens_bdata );
    
    prox_app( );
    log_printf( &logger, "** Proximity:   %s\r\n", prox_str );
    
    log_printf( &logger, "\r\n" );
    Delay_ms( 1000 );
}

void main ( void )
{
    application_init( );

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

void prox_app ( void )
{
    float prox;
    uint8_t cnt;
    
    prox = lightmixsens_data.lightmixsens_pdata;
    prox /= 255;
    prox *= 16;
    
    for ( cnt = 0; cnt < ( uint8_t ) prox; cnt++ ) {
        prox_str[ cnt ] = '|';
    }
    
    prox_str[ cnt ] = 0;
}

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

Additional Support

Resources