Beginner
10 min

Measure the ambient light intensity and detect the presence/absence of objects with TMD2755 and PIC32MZ2048EFH100

Precise measurements for both light and proximity even in challenging conditions

Ambient 14 Click with Flip&Click PIC32MZ

Published Oct 10, 2024

Click board™

Ambient 14 Click

Dev. board

Flip&Click PIC32MZ

Compiler

NECTO Studio

MCU

PIC32MZ2048EFH100

Accurately detect light and proximity for optimized device control

A

A

Hardware Overview

How does it work?

Ambient 14 Click is based on the TMD2755, an advanced sensor from ams OSRAM that combines digital ambient light sensing (ALS) and proximity detection in a highly compact 1.1mm module. The TMD2755 integrates an infrared VCSEL (Vertical-Cavity Surface-Emitting Laser) and a factory-calibrated VCSEL driver for efficient proximity detection. This sensor excels in detecting objects, such as recognizing the proximity of a user's ear to a mobile device screen, by measuring the reflected IR energy emitted by the integrated VCSEL. The board's proximity detection capabilities are supported by a sophisticated proximity engine, which includes an offset adjustment feature to eliminate unwanted reflections from nearby objects, enhancing accuracy. It also improves proximity

measurements by automatically subtracting ambient light interference. The results from both ALS and proximity detection are provided as 16-bit data, enabling precise measurement of ambient light levels for tasks like adjusting display backlight brightness. This Click board™ uses a standard 2-wire I2C interface to communicate with the host MCU, supporting Standard mode with up to 1MHz of frequency clock. It also provides interrupt-driven detect/release events through the INT pin on the mikroBUS™ socket. These interrupts are triggered when proximity results exceed or fall below user-configured threshold levels. The TMD2755 does not require a specific Power-Up sequence but requires a voltage of 1.8V for its interface and logic part to work correctly. Therefore, a small regulating LDO,

the BH18PB1WHFV, provides a 1.8V out of 3.3V mikroBUS™ power rail. Since the sensor operates on 1.8V, this Click board™ also features the PCA9306 voltage-level translator, allowing the TMD2755 to work properly with 3.3V and 5V MCU. This regulator can be activated via the SBY pin of the mikroBUS™ socket, providing an enable function simultaneously. 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. Also, it comes equipped with a library containing functions and an example code that can be used as a reference for further development.

Ambient 14 Click hardware overview image

Features overview

Development board

Flip&Click PIC32MZ is a compact development board designed as a complete solution that brings the flexibility of add-on Click boards™ to your favorite microcontroller, making it a perfect starter kit for implementing your ideas. It comes with an onboard 32-bit PIC32MZ microcontroller, the PIC32MZ2048EFH100 from Microchip, four mikroBUS™ sockets for Click board™ connectivity, two USB connectors, LED indicators, buttons, debugger/programmer connectors, and two headers compatible with Arduino-UNO pinout. Thanks to innovative manufacturing technology,

it allows you to build gadgets with unique functionalities and features quickly. Each part of the Flip&Click PIC32MZ development kit contains the components necessary for the most efficient operation of the same board. In addition, there is the possibility of choosing the Flip&Click PIC32MZ programming method, using the chipKIT bootloader (Arduino-style development environment) or our USB HID bootloader using mikroC, mikroBasic, and mikroPascal for PIC32. This kit includes a clean and regulated power supply block through the USB Type-C (USB-C) connector. All communication

methods that mikroBUS™ itself supports are on this board, including the well-established mikroBUS™ socket, user-configurable buttons, and LED indicators. Flip&Click PIC32MZ development kit allows you to create a new application in minutes. Natively supported by Mikroe software tools, it covers many aspects of prototyping thanks to a considerable number of different Click boards™ (over a thousand boards), the number of which is growing every day.

Flip&Click PIC32MZ double image

Microcontroller Overview

MCU Card / MCU

default

Architecture

PIC32

MCU Memory (KB)

2048

Silicon Vendor

Microchip

Pin count

100

RAM (Bytes)

524288

Used MCU Pins

mikroBUS™ mapper

NC
NC
AN
Device Enable
RE2
RST
ID COMM
RA0
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
NC
NC
PWM
Interrupt
RD9
INT
NC
NC
TX
NC
NC
RX
I2C Clock
RA2
SCL
I2C Data
RA3
SDA
NC
NC
5V
Ground
GND
GND
1

Take a closer look

Click board™ Schematic

Ambient 14 Click Schematic schematic

Step by step

Project assembly

Flip&Click PIC32MZ front image hardware assembly

Start by selecting your development board and Click board™. Begin with the Flip&Click PIC32MZ as your development board.

Flip&Click PIC32MZ front image hardware assembly
Buck 22 Click front image hardware assembly
Prog-cut hardware assembly
Flip&Click PIC32MZ - 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
Flip&Click PIC32MZ 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. Application Output - In Debug mode, the 'Application Output' window enables real-time data monitoring, offering direct insight into execution results. Ensure proper data display by configuring the environment correctly using the provided tutorial.

2. UART Terminal - Use the UART Terminal to monitor data transmission via a USB to UART converter, allowing direct communication between the Click board™ and your development system. Configure the baud rate and other serial settings according to your project's requirements to ensure proper functionality. For step-by-step setup instructions, refer to the provided tutorial.

3. Plot Output - The Plot feature offers a powerful way to visualize real-time sensor data, enabling trend analysis, debugging, and comparison of multiple data points. To set it up correctly, follow the provided tutorial, which includes a step-by-step example of using the Plot feature to display Click board™ readings. To use the Plot feature in your code, use the function: plot(*insert_graph_name*, variable_name);. This is a general format, and it is up to the user to replace 'insert_graph_name' with the actual graph name and 'variable_name' with the parameter to be displayed.

Software Support

Library Description

This library contains API for Ambient 14 Click driver.

Key functions:

  • ambient14_read_proximity - This function reads the raw proximity data. The higher the value, the closer the detected object is.

  • ambient14_read_als_ir - This function reads the raw ALS and IR data.

  • ambient14_get_illuminance - This function calculates the illuminance level (Lux) from ALS data counts input.

Open Source

Code example

The complete application code and a ready-to-use project are available through the NECTO Studio Package Manager for direct installation in the NECTO Studio. The application code can also be found on the MIKROE GitHub account.

/*!
 * @file main.c
 * @brief Ambient 14 Click example
 *
 * # Description
 * This example demonstrates the use of Ambient 14 click board by measuring 
 * the illuminance level (Lux) and the proximity data on the USB UART.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes the driver and performs the click default configuration.
 *
 * ## Application Task
 * Reads the proximity, ALS, and IR raw data counts when data is ready.
 * Calculates the illuminance level in Lux from ALS data counts and displays
 * the results on the USB UART approximately every 500ms.
 *
 * @author Stefan Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "ambient14.h"

static ambient14_t ambient14;
static log_t logger;

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    ambient14_cfg_t ambient14_cfg;  /**< Click config object. */

    /** 
     * 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.
    ambient14_cfg_setup( &ambient14_cfg );
    AMBIENT14_MAP_MIKROBUS( ambient14_cfg, MIKROBUS_1 );
    if ( I2C_MASTER_ERROR == ambient14_init( &ambient14, &ambient14_cfg ) ) 
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }
    
    if ( AMBIENT14_ERROR == ambient14_default_cfg ( &ambient14 ) )
    {
        log_error( &logger, " Default configuration." );
        for ( ; ; );
    }
    
    log_info( &logger, " Application Task " );
}

void application_task ( void ) 
{
    uint16_t proximity = 0;
    uint16_t als_data = 0;
    uint16_t ir_data = 0;
    float illuminance = 0;
    
    // Enable and wait for proximity interrupt
    ambient14_write_reg ( &ambient14, AMBIENT14_REG_INTENAB, AMBIENT14_INTENAB_PIEN );
    while ( ambient14_get_int_pin ( &ambient14 ) );

    // Read proximity data and clear interrupts
    if ( AMBIENT14_OK == ambient14_read_proximity ( &ambient14, &proximity ) )
    {
        log_printf ( &logger, " Proximity: %u\r\n", proximity );
    }
    ambient14_clear_interrupts ( &ambient14 );

    // Enable and wait for ALS interrupt
    ambient14_write_reg ( &ambient14, AMBIENT14_REG_INTENAB, AMBIENT14_INTENAB_AIEN );
    while ( ambient14_get_int_pin ( &ambient14 ) );

    // Read ALS and IR data counts, calculates illuminance level, and clear interrupts
    if ( AMBIENT14_OK == ambient14_read_als_ir ( &ambient14, &als_data, &ir_data ) )
    {
        log_printf ( &logger, " ALS: %u\r\n", als_data );
        log_printf ( &logger, " IR: %u\r\n", ir_data );
        if ( AMBIENT14_OK == ambient14_get_illuminance ( &ambient14, als_data, &illuminance ) )
        {
            log_printf ( &logger, " Illuminance: %.1f Lux\r\n\n", illuminance );
        }
    }
    ambient14_clear_interrupts ( &ambient14 );
}

int main ( void ) 
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif
    
    application_init( );
    
    for ( ; ; ) 
    {
        application_task( );
    }

    return 0;
}

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