Intermediate
30 min

Connect your IoT devices to 5G networks easily with ENS22 and PIC18F57Q43

Experience the power of 5G with our NB-IoT

5G NB IoT Click with UNI-DS v8

Published Sep 13, 2023

Click board™

5G NB IoT Click

Dev Board

UNI-DS v8

Compiler

NECTO Studio

MCU

PIC18F57Q43

Elevate your IoT connectivity to new heights with a platform that combines the benefits of NB-IoT technology, including power efficiency and extended coverage, for a reliable and secure IoT experience

A

A

Hardware Overview

How does it work?

5G NB IoT Click is based on the ENS22, an NB-IoT Wireless Module platform from Thales that boosts highly efficient future 5G connectivity for the IoT. The ENS22 IoT wireless module marries future 5G connectivity with expanded coverage and enhanced security features to connect and protect industrial IoT solutions. Delivering data speeds up to 27 Kbit/s downlink and 63 Kbit/s uplink, the ENS22 IoT module is ideal for long life data-only solutions such as utility meters and smart city solutions. 5G NB IoT Click features an integrated NB-IoT transceiver, multi-band mobile cellular devices. The module can operate over 698-960 MHz and 1695-2180MHz with a 200 kHz system bandwidth. It is designed to communicate with

mobile network operator (MNO) infrastructure equipment using the 3GPP NB-IoT radio protocol. The ENS22 IoT module platform offers a suite of NB-IoT connectivity solutions optimized specifically for IoT applications and prepared to support release 14 without the need to migrate to a new chipset. It delivers Five Band LTE (3, 5, 8, 20, 28) connectivity with deep indoor coverage and extended range in rural areas. The module offers a built-in IP stack which supports a range of internet services protected by an enhanced security concept. Incremental Firmware Over The Air (FOTA) updates allow revision of only the portion of code that needs updating, saving power and bandwidth to extend the life span of IoT solutions.

The module’s simplified power supply design and advanced management system extends battery lifetime and improves TCO. This Click board™ is equipped with the USB type C connector. It allows the module to be powered and configured by a personal computer. The FT230X IC requires drivers in order to work. FTDI offers drivers for all major OSes on their official driver download web page. Also, Windows OS drivers are included in the download section, below. 5G NB IoT Click have fully-integrated Li-Ion or Li-Polymer battery charger witch in combination with module up to 10 ~ 15 years of battery life allow user using it completely standalone and battery powered ony.

5G NB IoT Click hardware overview 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)

128

Silicon Vendor

Microchip

Pin count

48

RAM (Bytes)

8196

You complete me!

Accessories

LTE Flat Rotation Antenna is a versatile choice for boosting the performance of 3G/4G LTE devices. With a wide frequency range of 700-2700MHz, it ensures optimal connectivity on major cellular bands worldwide. This flat antenna features an SMA male connector, making it easy to attach directly to your device or SMA module connector. One of its standout features is its adjustable angle, which can be set in 45⁰ increments (0⁰/45⁰/90⁰), allowing you to fine-tune the antenna's orientation for maximum signal reception. With an impedance of 50Ω and a VSW Ratio of <2.0:1, this antenna ensures a reliable and efficient connection. Its 5dB gain, vertical polarization, and omnidirectional radiation pattern enhance signal strength, making it suitable for various applications. Measuring 196mm in length and 38mm in width, this antenna offers a compact yet effective solution for improving your connectivity. With a maximum input power of 50W, it can handle the demands of various devices.

5G NB IoT Click accessories image

Used MCU Pins

mikroBUS™ mapper

NC
NC
AN
Ignition
PB3
RST
UART CTS
PE0
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
NC
NC
PWM
UART RTS
PA0
INT
UART TX
PC6
TX
UART RX
PC7
RX
NC
NC
SCL
NC
NC
SDA
Power Supply
5V
5V
Ground
GND
GND
1

Take a closer look

Schematic

5G NB IoT 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
GNSS2 Click front image hardware assembly
SiBRAIN for PIC32MZ1024EFK144 front image hardware assembly
GNSS2 Click complete accessories setup image hardware assembly
v8 SiBRAIN Access 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 5G NB IoT Click driver.

Key functions:

  • c5gnbiot_send_cmd - Send command function

  • c5gnbiot_set_sim_apn - Set sim card APN function

  • c5gnbiot_send_text_message - Send SMS in PDU mode 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 
 * \brief 5gNbIot Click example
 * 
 * # Description
 * This example reads and processes data from 5G NB IoT click.
 *
 * The demo application is composed of two sections :
 * 
 * ## Application Init 
 * Initializes driver and wake-up module and sets default configuration for connecting device to network.
 * 
 * ## Application Task  
 * Waits for device to connect to network and then sends an SMS to the selected phone number.
 * 
 * ## Additional Function
 * - static void c5gnbiot_clear_app_buf ( void )
 * - static void c5gnbiot_error_check( err_t error_flag )
 * - static void c5gnbiot_log_app_buf ( void )
 * - static void c5gnbiot_check_connection( void )
 * - static err_t c5gnbiot_rsp_check ( void )
 * - static err_t c5gnbiot_process ( void )
 * 
 * @note 
 * In order for the example to work, user needs to set the phone number to which he wants 
 * to send an SMS, and also will need to set an APN and SMSC of entered SIM card.
 * Enter valid data for the following macros: SIM_APN, SIM_SMSC and PHONE_NUMBER_TO_MESSAGE.
 * E.g. 
    SIM_APN "vipmobile"
    SIM_SMSC "+381610401"
    PHONE_NUMBER_TO_MESSAGE "+381659999999"
 * 
 * @author MikroE Team
 *
 */
// ------------------------------------------------------------------- INCLUDES

#include "board.h"
#include "log.h"
#include "c5gnbiot.h"

#define APP_OK                              0
#define APP_ERROR_DRIVER                    -1
#define APP_ERROR_OVERFLOW                  -2
#define APP_ERROR_TIMEOUT                   -3

#define RSP_OK                              "OK"
#define RSP_ERROR                           "ERROR"

#define SIM_APN                             ""  // Set valid SIM APN
#define SIM_SMSC                            ""  // Set valid SMS Service Center Address
#define PHONE_NUMBER_TO_MESSAGE             "" // Set Phone number to message
#define MESSAGE_CONTENT                     "5G NB IoT Click" // Messege content 

#define PROCESS_BUFFER_SIZE                 300

#define WAIT_FOR_CONNECTION                 0
#define CONNECTED_TO_NETWORK                1

static c5gnbiot_t c5gnbiot;
static log_t logger;

static char app_buf[ PROCESS_BUFFER_SIZE ]  = { 0 };
static int32_t app_buf_len                  = 0;
static int32_t app_buf_cnt                  = 0;

static uint8_t app_connection_status        = WAIT_FOR_CONNECTION;

static err_t app_error_flag;

/**
 * @brief 5G NB IoT clearing application buffer.
 * @details This function clears memory of application buffer and reset its length and counter.
 * @note None.
 */
static void c5gnbiot_clear_app_buf ( void );

/**
 * @brief 5G NB IoT data reading function.
 * @details This function reads data from device and concats data to application buffer.
 * 
 * @return @li @c  0 - Read some data.
 *         @li @c -1 - Nothing is read.
 *         @li @c -2 - Application buffer overflow.
 *
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t c5gnbiot_process ( void );

/**
 * @brief 5G NB IoT check for errors.
 * @details This function checks for different types of errors and logs them on UART.
 * @note None.
 */
static void c5gnbiot_error_check( err_t error_flag );

/**
 * @brief 5G NB IoT logs application buffer.
 * @details This function logs data from application buffer.
 * @note None.
 */
static void c5gnbiot_log_app_buf ( void );

/**
 * @brief 5G NB IoT response check.
 * @details This function checks for response and returns the status of response.
 * 
 * @return application status.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t c5gnbiot_rsp_check ( void );

/**
 * @brief 5G NB IoT chek connection.
 * @details This function checks connection to the network and 
 *          logs that status to UART.
 * 
 * @note None.
 */
static void c5gnbiot_check_connection( void );

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

void application_init ( void )
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    c5gnbiot_cfg_t c5gnbiot_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 " );
    Delay_ms( 1000 );
    
    // Click initialization.
    c5gnbiot_cfg_setup( &c5gnbiot_cfg );
    C5GNBIOT_MAP_MIKROBUS( c5gnbiot_cfg, MIKROBUS_1 );
    err_t init_flag  = c5gnbiot_init( &c5gnbiot, &c5gnbiot_cfg );
    if ( init_flag == UART_ERROR )
    {
        log_error( &logger, " Application Init Error. " );
        log_info( &logger, " Please, run program again... " );

        for ( ; ; );
    }
    
    log_info( &logger, " Power on device... " );
    Delay_ms( 1000 );
    c5gnbiot_power_on( &c5gnbiot );
    
    // dummy read
    c5gnbiot_process( );
    c5gnbiot_clear_app_buf( );
    // AT
    c5gnbiot_send_cmd( &c5gnbiot, C5GNBIOT_CMD_AT );
    app_error_flag = c5gnbiot_rsp_check( );
    c5gnbiot_error_check( app_error_flag );
    Delay_ms( 500 );
    
    // ATI - product information
    c5gnbiot_send_cmd( &c5gnbiot, C5GNBIOT_CMD_ATI );
    app_error_flag = c5gnbiot_rsp_check(  );
    c5gnbiot_error_check( app_error_flag );
    Delay_ms( 500 );
    
    // CGMR - firmware version
    c5gnbiot_send_cmd( &c5gnbiot, C5GNBIOT_CMD_CGMR );
    app_error_flag = c5gnbiot_rsp_check(  );
    c5gnbiot_error_check( app_error_flag );
    Delay_ms( 500 );
    
    // COPS - deregister from network
    c5gnbiot_send_cmd_with_parameter( &c5gnbiot, C5GNBIOT_CMD_COPS, "2" );
    app_error_flag = c5gnbiot_rsp_check(  );
    c5gnbiot_error_check( app_error_flag );
    Delay_ms( 500 );
    
    // CGDCONT - set sim apn
    c5gnbiot_set_sim_apn( &c5gnbiot, SIM_APN );
    app_error_flag = c5gnbiot_rsp_check(  );
    c5gnbiot_error_check( app_error_flag );
    Delay_ms( 500 );
     
    // CFUN - full funtionality
    c5gnbiot_send_cmd_with_parameter( &c5gnbiot, C5GNBIOT_CMD_CFUN, "1" );
    app_error_flag = c5gnbiot_rsp_check(  );
    c5gnbiot_error_check( app_error_flag );
    Delay_ms( 500 );
    
    // COPS - automatic mode
    c5gnbiot_send_cmd_with_parameter( &c5gnbiot, C5GNBIOT_CMD_COPS, "0" );
    app_error_flag = c5gnbiot_rsp_check(  );
    c5gnbiot_error_check( app_error_flag );
    Delay_ms( 2000 );
    
    // CEREG - network registration status
    c5gnbiot_send_cmd_with_parameter( &c5gnbiot, C5GNBIOT_CMD_CEREG, "2" );
    app_error_flag = c5gnbiot_rsp_check(  );
    c5gnbiot_error_check( app_error_flag );
    Delay_ms( 500 );
    
    // CIMI - request IMSI
    c5gnbiot_send_cmd( &c5gnbiot, C5GNBIOT_CMD_CIMI );
    app_error_flag = c5gnbiot_rsp_check(  );
    c5gnbiot_error_check( app_error_flag );
    Delay_ms( 500 );
    
    app_buf_len = 0;
    app_buf_cnt = 0;
    app_connection_status = WAIT_FOR_CONNECTION;
    log_info( &logger, " Application Task " );
    Delay_ms( 5000 );
}

void application_task ( void )
{  
    if ( app_connection_status == WAIT_FOR_CONNECTION )
    {
        // CGATT - request IMSI
        c5gnbiot_send_cmd_check( &c5gnbiot, C5GNBIOT_CMD_CGATT );
        app_error_flag = c5gnbiot_rsp_check(  );
        c5gnbiot_error_check( app_error_flag );
        Delay_ms( 500 );
        
        // CEREG - network registration status
        c5gnbiot_send_cmd_check( &c5gnbiot, C5GNBIOT_CMD_CEREG );
        app_error_flag = c5gnbiot_rsp_check(  );
        c5gnbiot_error_check( app_error_flag );
        Delay_ms( 500 );
        
        // CSQ - signal quality
        c5gnbiot_send_cmd( &c5gnbiot, C5GNBIOT_CMD_CESQ );
        app_error_flag = c5gnbiot_rsp_check(  );
        c5gnbiot_error_check( app_error_flag );
        Delay_ms( 5000 );
    }
    else
    {
        log_info( &logger, "CONNECTED TO NETWORK" );
        
        for( ; ; )
        {   
            log_printf( &logger, "> Sending message to phone number...\r\n" );
            c5gnbiot_send_text_message( &c5gnbiot, SIM_SMSC, PHONE_NUMBER_TO_MESSAGE, MESSAGE_CONTENT );
            app_error_flag = c5gnbiot_rsp_check(  );
            c5gnbiot_error_check( app_error_flag );
            Delay_ms( 10000 );
            Delay_ms( 10000 );
            Delay_ms( 10000 );
        }
    }
}

void main ( void )
{
    application_init( );

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

static void c5gnbiot_clear_app_buf ( void )
{
    memset( app_buf, 0, app_buf_len );
    app_buf_len = 0;
    app_buf_cnt = 0;
}

static err_t c5gnbiot_process ( void )
{
    err_t return_flag = APP_ERROR_DRIVER;
    int32_t rx_size;
    char rx_buff[ PROCESS_BUFFER_SIZE ] = { 0 };
    
    rx_size = c5gnbiot_generic_read( &c5gnbiot, rx_buff, PROCESS_BUFFER_SIZE );

    if ( rx_size > 0 )
    { 
        int32_t buf_cnt = 0;
        return_flag = APP_OK;

        if ( app_buf_len + rx_size >= PROCESS_BUFFER_SIZE )
        {
            c5gnbiot_clear_app_buf(  );
            return_flag = APP_ERROR_OVERFLOW;
        }
        else
        {
            buf_cnt = app_buf_len;
            app_buf_len += rx_size;
        }

        for ( int32_t rx_cnt = 0; rx_cnt < rx_size; rx_cnt++ )
        {
            if ( rx_buff[ rx_cnt ] != 0 ) 
            {
                app_buf[ ( buf_cnt + rx_cnt ) ] = rx_buff[ rx_cnt ];
            }
            else
            {
                app_buf_len--;
                buf_cnt--;
            }
        }
    } 

    return return_flag;
}

static err_t c5gnbiot_rsp_check ( void )
{
    uint16_t timeout_cnt = 0;
    uint16_t timeout = 10000;
    
    err_t error_flag = c5gnbiot_process(  );
    
    if ( ( error_flag != 0 ) && ( error_flag != -1 ) )
    {
        return error_flag;
    }
    
    while ( ( strstr( app_buf, RSP_OK ) == 0 ) && ( strstr( app_buf, RSP_ERROR ) == 0 ) )
    {
        error_flag = c5gnbiot_process(  );
        if ( ( error_flag != 0 ) && ( error_flag != -1 ) )
        {
            return error_flag;
        }
        
        timeout_cnt++;
        if ( timeout_cnt > timeout )
        {
            while ( ( strstr( app_buf, RSP_OK ) == 0 ) && ( strstr( app_buf, RSP_ERROR ) == 0 ) )
            {
                c5gnbiot_send_cmd( &c5gnbiot, C5GNBIOT_CMD_AT );
                c5gnbiot_process(  );
                Delay_ms( 100 );
            }
            c5gnbiot_clear_app_buf(  );
            return APP_ERROR_TIMEOUT;
        }
        
        Delay_ms( 1 );
    }
    
    c5gnbiot_check_connection();
    
    c5gnbiot_log_app_buf();
    
    log_printf( &logger, "-----------------------------------\r\n" );
    
    return APP_OK;
}

static void c5gnbiot_error_check( err_t error_flag )
{
    if ( ( error_flag != 0 ) && ( error_flag != -1 ) )
    {
        switch ( error_flag )
        {
            case -2:
                log_error( &logger, " Overflow!" );
                break;
            case -3:
                log_error( &logger, " Timeout!" );
                break;
            default:
                break;
        }
    }
}

static void c5gnbiot_log_app_buf ( void )
{
    for ( int32_t buf_cnt = 0; buf_cnt < app_buf_len; buf_cnt++ )
    {
        log_printf( &logger, "%c", app_buf[ buf_cnt ] );
    }
    log_printf( &logger, "\r\n" );
    c5gnbiot_clear_app_buf(  );
}

static void c5gnbiot_check_connection( void )
{
    #define CONNECTED "+CGATT: 1"
    
    if ( strstr( app_buf, CONNECTED ) != 0 )
    {
        app_connection_status = CONNECTED_TO_NETWORK;
    }
}

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

Additional Support

Resources