Beginner
10 min

Measure distances within the 20- to 500cm range with TMF8806-1A and PIC32MZ2048EFM100

Precise time-of-flight distance measurement solution

LightRanger 13 Click with Curiosity PIC32 MZ EF

Published Feb 04, 2025

Click board™

LightRanger 13 Click

Dev Board

Curiosity PIC32 MZ EF

Compiler

NECTO Studio

MCU

PIC32MZ2048EFM100

Measure distances precisely and reliably with advanced TOF technology perfect for robotics, automation, and smart appliances

A

A

Hardware Overview

How does it work?

LightRanger 13 Click is based on the TMF8806-1A, a time-of-flight (TOF) sensor from ams OSRAM designed for precise distance measurement in various applications. This sensor is housed in one of the smallest modular packages in the industry and incorporates a 940nm VCSEL that complies with Class 1 eye safety standards. Using single-photon avalanche diode (SPAD) technology, a fast time-to-digital converter (TDC) architecture, and on-chip histogram processing, the TMF8806-1A ensures highly accurate distance measurements across a detection range of 1cm to 500cm, even in challenging environments. This Click board™ is ideal for industrial, commercial, and residential use cases requiring high-resolution distance sensing, such as robotics, home and building automation, projection and display systems, factory automation, access control, security systems, and smart appliances. The sensor's dTOF technology, combined with its high-sensitivity SPAD detection, delivers exceptional signal-to-noise ratios and a wide dynamic range, ensuring reliable performance in both low-light and bright sunlight conditions. Its fast sub-nanosecond light pulse capability and 30Hz sensing rate enable rapid and precise

distance detection, making it suitable for dynamic applications. The TMF8806-1A features built-in mechanisms for calibration, dirt and smudge removal, and crosstalk compensation, ensuring consistent accuracy over time. Additionally, its integrated eye-safety circuitry halts the VCSEL driver in case of a fault, enhancing operational safety. This Click board™ is designed in a unique format supporting the newly introduced MIKROE feature called "Click Snap." Unlike the standardized version of Click boards, this feature allows the main IC area to become movable by breaking the PCB, opening up many new possibilities for implementation. Thanks to the Snap feature, the TMF8806-1A can operate autonomously by accessing its signals directly on the pins marked 1-8. Additionally, the Snap part includes a specified and fixed screw hole position, enabling users to secure the Snap board in their desired location. LightRanger 13 Click communicates with the host MCU through an I2C interface, supporting a maximum clock frequency of 1MHz for fast data exchange. In addition to the standard interface pins, the board also features an EN pin, which acts as the device enable pin. Setting this pin to a HIGH

logic level powers up the device, while setting it to LOW forces the sensor into a Shutdown mode, during which all memory content is erased. The board also includes an INT pin, configured as an open-drain interrupt output, which signals the host MCU when a ranging measurement is available, ensuring timely data processing. Additionally, the Snap section of the board includes a GPIO0 SEL jumper, which allows for the selection of the TMF8806-1A's I/O supply voltage. Setting the jumper to the VCC position enables a 3.3V or lower supply, such as 1.8V, while setting it to the GND position provides an even lower I/O supply voltage of 1.2V. After startup, the GPIO0 pin can function as a general-purpose GPIO output signal, accessible through the GPIO1 test point on the back of the board. 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. It also comes equipped with a library containing functions and example code that can be used as a reference for further development.

LightRanger 13 Click hardware overview image

Features overview

Development board

Curiosity PIC32 MZ EF development board is a fully integrated 32-bit development platform featuring the high-performance PIC32MZ EF Series (PIC32MZ2048EFM) that has a 2MB Flash, 512KB RAM, integrated FPU, Crypto accelerator, and excellent connectivity options. It includes an integrated programmer and debugger, requiring no additional hardware. Users can expand

functionality through MIKROE mikroBUS™ Click™ adapter boards, add Ethernet connectivity with the Microchip PHY daughter board, add WiFi connectivity capability using the Microchip expansions boards, and add audio input and output capability with Microchip audio daughter boards. These boards are fully integrated into PIC32’s powerful software framework, MPLAB Harmony,

which provides a flexible and modular interface to application development a rich set of inter-operable software stacks (TCP-IP, USB), and easy-to-use features. The Curiosity PIC32 MZ EF development board offers expansion capabilities making it an excellent choice for a rapid prototyping board in Connectivity, IOT, and general-purpose applications.

Curiosity PIC32MZ EF double side 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
NC
NC
RST
ID COMM
RPD4
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Device Enable
RPE8
PWM
Interrupt
RF13
INT
NC
NC
TX
NC
NC
RX
I2C Clock
RPA14
SCL
I2C Data
RPA15
SDA
NC
NC
5V
Ground
GND
GND
1

Take a closer look

Click board™ Schematic

LightRanger 13 Click Schematic schematic

Step by step

Project assembly

Curiosity PIC32MZ EF front image hardware assembly

Start by selecting your development board and Click board™. Begin with the Curiosity PIC32 MZ EF as your development board.

Curiosity PIC32MZ EF front image hardware assembly
GNSS2 Click front image hardware assembly
Prog-cut hardware assembly
GNSS2 Click complete accessories setup image hardware assembly
Curiosity PIC32 MZ EF MB 1 Access - 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
Curiosity PIC32 MZ EF 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

LightRanger 13 Click demo application is developed using the NECTO Studio, ensuring compatibility with mikroSDK's open-source libraries and tools. Designed for plug-and-play implementation and testing, the demo is fully compatible with all development, starter, and mikromedia boards featuring a mikroBUS™ socket.

Example Description
This application demonstrates the usage of the LightRanger 13 Click for distance measurement and monitoring.

Key functions:

  • lightranger13_cfg_setup - Config Object Initialization function.

  • lightranger13_init - Initialization function.

  • lightranger13_default_cfg - Click Default Configuration function.

  • lightranger13_set_threshold - This function sets a threshold level and interrupt persistance.

  • lightranger13_start_measurement - This function starts a measurement (the configuration, factory calibration and state data are used from context object).

  • lightranger13_read_result - This function reads the measurement results.

Application Init
Initializes the logger and LightRanger 13 Click with default configuration, displays device information, performs factory calibration (if enabled), and sets up distance measurement thresholds (1mm-1000mm) with persistence (5 consecutive times) to ensure reliable readings. It then starts the measurement process.

Application Task
Waits for the data ready interrupt signal, indicating a new measurement is available. Upon receiving the signal, it reads the distance and temperature data from the sensor and logs the values to USB UART.

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 LightRanger 13 Click example
 *
 * # Description
 * This application demonstrates the usage of the LightRanger 13 Click for distance
 * measurement and monitoring.
 *
 * The demo application is composed of two sections:
 *
 * ## Application Init
 * Initializes the logger and LightRanger 13 Click with default configuration,
 * displays device information, performs factory calibration (if enabled), and sets up
 * distance measurement thresholds (1mm-1000mm) with persistence (5 consecutive times)
 * to ensure reliable readings. It then starts the measurement process.
 *
 * ## Application Task
 * Waits for the data ready interrupt signal, indicating a new measurement is available.
 * Upon receiving the signal, it reads the distance and temperature data from the sensor
 * and logs the values to USB UART.
 *
 * @note
 * The factory calibration step can be enabled or disabled by commenting/uncommenting
 * the `FACTORY_CALIBRATION_DISABLE` macro.
 *
 * @author Stefan Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "lightranger13.h"

// Comment out the line below to enable factory calibration
#define FACTORY_CALIBRATION_DISABLE

static lightranger13_t lightranger13;
static log_t logger;

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    lightranger13_cfg_t lightranger13_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.
    lightranger13_cfg_setup( &lightranger13_cfg );
    LIGHTRANGER13_MAP_MIKROBUS( lightranger13_cfg, MIKROBUS_1 );
    if ( I2C_MASTER_ERROR == lightranger13_init( &lightranger13, &lightranger13_cfg ) ) 
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }
    
    if ( LIGHTRANGER13_ERROR == lightranger13_default_cfg ( &lightranger13 ) )
    {
        log_error( &logger, " Default configuration." );
        for ( ; ; );
    }

    log_printf( &logger, "--- DEVICE INFO ---\r\n" );
    log_printf( &logger, " App version: %u.%u.%u.%u\r\n",
                ( uint16_t ) lightranger13.info.app_ver[ 0 ], 
                ( uint16_t ) lightranger13.info.app_ver[ 1 ], 
                ( uint16_t ) lightranger13.info.app_ver[ 2 ], 
                ( uint16_t ) lightranger13.info.app_ver[ 3 ] );
    log_printf( &logger, " Chip version: %u.%u\r\n",
                ( uint16_t ) lightranger13.info.chip_ver[ 0 ], 
                ( uint16_t ) lightranger13.info.chip_ver[ 1 ] );
    log_printf( &logger, " Serial number: %.8LX\r\n\n", lightranger13.info.serial_num );
    Delay_ms ( 1000 );

#ifndef FACTORY_CALIBRATION_DISABLE
    log_printf( &logger, "--- FACTORY CALIBRATION ---\r\n" );
    log_printf( &logger, " Calibration environment:\r\n" );
    log_printf( &logger, " - Device has to be in the final (correct) optical stack\r\n" );
    Delay_ms ( 10 );
    log_printf( &logger, " - Cover glass (no smudge on the glass) in front of the device\r\n" );
    log_printf( &logger, " - No target in front of the device within 40cm\r\n" );
    log_printf( &logger, " - Dark room or low ambient light\r\n" );
    log_printf( &logger, " Performing factory calibration in 5 seconds...\r\n" );
    for ( uint8_t cnt = 5; cnt > 0; cnt-- )
    {
        log_printf( &logger, " %u\r\n", ( uint16_t ) cnt );
        Delay_ms ( 1000 );
    }
    if ( LIGHTRANGER13_ERROR == lightranger13_factory_calib ( &lightranger13 ) )
    {
        log_error( &logger, " Factory calibration." );
        for ( ; ; );
    }
    log_printf( &logger, " Factory calibration DONE.\r\n\n" );
    Delay_ms ( 1000 );
#endif

    log_printf( &logger, "--- THRESHOLD AND PERSISTANCE ---\r\n" );
    log_printf( &logger, " Setting threshold: 1mm-1000mm; and persistance: 5 times\r\n" );
    if ( LIGHTRANGER13_ERROR == lightranger13_set_threshold ( &lightranger13, 1, 1000, 5 ) )
    {
        log_error( &logger, " Set threshold." );
        for ( ; ; );
    }
    log_printf( &logger, " Set threshold DONE.\r\n\n" );
    Delay_ms ( 1000 );

    log_printf( &logger, "--- STARTING MEASUREMENT ---\r\n" );
    if ( LIGHTRANGER13_ERROR == lightranger13_start_measurement ( &lightranger13 ) )
    {
        log_error( &logger, " Start measurement." );
        for ( ; ; );
    }
    log_printf( &logger, " Measurement started.\r\n\n" );
    Delay_ms ( 1000 );
    
    log_info( &logger, " Application Task " );
}

void application_task ( void ) 
{
    lightranger13_meas_result_t result;
    // Wait for data ready interrupt
    while ( lightranger13_get_int_pin ( &lightranger13 ) );

    lightranger13_clear_interrupts ( &lightranger13 );
    
    if ( LIGHTRANGER13_OK == lightranger13_read_result ( &lightranger13, &result ) )
    {
        log_printf( &logger, " Distance: %u mm\r\n", result.dist_peak );
        log_printf( &logger, " Temperature: %d degC\r\n\n", ( int16_t ) result.temperature );
    }
}

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.