Intermediate
30 min

Power up your projects with Bluetooth 5 LE NINA-B312 and ATmega32

Wire-free wonder: Bluetooth, your shortcut to connectivity

BLE 4 Click with EasyAVR v7

Published Nov 01, 2023

Click board™

BLE 4 Click

Dev. board

EasyAVR v7

Compiler

NECTO Studio

MCU

ATmega32

Our Bluetooth 5 LE solution offers extended range and higher data throughput, enabling reliable and fast data transmission for a wide range of IoT and smart device applications.

A

A

Hardware Overview

How does it work?

BLE 4 Click is based on the NINA-B312, a stand-alone Bluetooth 5 low-energy module from u-blox, based on the nRF52840 chip. The nRF52840 is the mid-range member of the nRF52 Series SoC family. It meets the challenges of a broad range of applications requiring Bluetooth 5 feature sets, protocol concurrency, and a rich and varied set of peripherals and features. In addition, it offers generous memory availability for both Flash and RAM. It is operated by a set of AT commands over the UART interface, which makes the BLE 4 click very easy to use. By integrating most of the critical components on the chip, the NINA-B312 allows the module to overcome any imperfections of external discrete components, allowing signal transmission

power of up to 10dBm, and -94 dBm sensitivity for the receiver, using the on-chip antenna. The NINA-B312 module is built around an ARM® Cortex™-M4 CPU with a floating point unit running at 64 MHz. It has NFC-A Tag for use in simplified pairing and payment solutions and numerous digital peripherals and interfaces such as PDM. Therefore, BLE 4 Click has an onboard 2-pin header that can connect the optional NFC antenna. It is also fully multiprotocol capable with full protocol concurrency. It has protocol support for Bluetooth 5, Bluetooth mesh, and 2.4 GHz proprietary stacks. Besides the mikroBUS™ socket, BLE 4 click also features two onboard buttons, T1 and T2. These buttons can be used for various

purposes and are user-programmable. This Click Board™ also has an onboard user-programmable RGB LED - LD2, which, combined with the mentioned buttons, can be used for various purposes for basic user interaction without any external components required besides the BLE 4 click. 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.

BLE 4 Click hardware overview image

Features overview

Development board

EasyAVR v7 is the seventh generation of AVR development boards specially designed for the needs of rapid development of embedded applications. It supports a wide range of 16-bit AVR microcontrollers from Microchip and has a broad set of unique functions, such as a powerful onboard mikroProg programmer and In-Circuit debugger over USB. The development board is well organized and designed so that the end-user has all the necessary elements in one place, such as switches, buttons, indicators, connectors, and others. With four different connectors for each port, EasyAVR v7 allows you to connect accessory boards, sensors, and custom electronics more

efficiently than ever. Each part of the EasyAVR v7 development board contains the components necessary for the most efficient operation of the same board. An integrated mikroProg, a fast USB 2.0 programmer with mikroICD hardware In-Circuit Debugger, offers many valuable programming/debugging options and seamless integration with the Mikroe software environment. Besides it also includes a clean and regulated power supply block for the development board. It can use a wide range of external power sources, including an external 12V power supply, 7-12V AC or 9-15V DC via DC connector/screw terminals, and a power source via the USB Type-B (USB-B)

connector. Communication options such as USB-UART and RS-232 are also included, alongside the well-established mikroBUS™ standard, three display options (7-segment, graphical, and character-based LCD), and several different DIP sockets which cover a wide range of 16-bit AVR MCUs. EasyAVR v7 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.

EasyAVR v7 horizontal image

Microcontroller Overview

MCU Card / MCU

default

Architecture

AVR

MCU Memory (KB)

32

Silicon Vendor

Microchip

Pin count

40

RAM (Bytes)

2048

Used MCU Pins

mikroBUS™ mapper

Data Terminal Ready
PA7
AN
Reset
PA6
RST
UART CTS
PA5
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Data Set Ready
PD4
PWM
UART RTS
PD2
INT
UART TX
PD1
TX
UART RX
PD0
RX
NC
NC
SCL
NC
NC
SDA
NC
NC
5V
Ground
GND
GND
1

Take a closer look

Click board™ Schematic

BLE 4 Click Schematic schematic

Step by step

Project assembly

EasyAVR v7 front image hardware assembly

Start by selecting your development board and Click board™. Begin with the EasyAVR v7 as your development board.

EasyAVR v7 front image hardware assembly
GNSS2 Click front image hardware assembly
EasyAVR v7 Access DIP 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 DIP image step 7 hardware assembly
EasyPIC PRO v7a Display Selection Necto Step 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

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 BLE 4 Click driver.

Key functions:

  • ble4_reset - This function allows user to reset a module

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 
 * \brief BLE4 Click example
 * 
 * # Description
 * This example reads and processes data from BLE 4 clicks.
 *
 * The demo application is composed of two sections :
 * 
 * ## Application Init 
 * Initializes driver and wake-up module.
 * 
 * ## Application Task  
 * Reads the received data and parses it.
 * 
 * ## Additional Function
 * - ble4_process ( ) - Logs all received messages on UART, and sends the certain message back to the connected device.
 * 
 * \author MikroE Team
 *
 */
// ------------------------------------------------------------------- INCLUDES

#include "board.h"
#include "log.h"
#include "ble4.h"
#include "string.h"

#define PROCESS_COUNTER 5
#define PROCESS_RX_BUFFER_SIZE 100
#define PROCESS_PARSER_BUFFER_SIZE 100

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

static ble4_t ble4;
static log_t logger;
static uint8_t data_mode = 0;

static char current_parser_buf[ PROCESS_PARSER_BUFFER_SIZE ];

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

static int8_t ble4_process ( void )
{
    int32_t rsp_size;
    uint16_t rsp_cnt = 0;
    
    char uart_rx_buffer[ PROCESS_RX_BUFFER_SIZE ] = { 0 };
    uint8_t check_buf_cnt;
    uint8_t process_cnt = PROCESS_COUNTER;
    
    // Clear current buffer
    memset( current_parser_buf, 0, PROCESS_PARSER_BUFFER_SIZE ); 
    
    while( process_cnt != 0 )
    {
        rsp_size = ble4_generic_read( &ble4, uart_rx_buffer, PROCESS_RX_BUFFER_SIZE );

        if ( rsp_size > 0 )
        {  
            // Validation of the received data
            for ( check_buf_cnt = 0; check_buf_cnt < rsp_size; check_buf_cnt++ )
            {
                if ( uart_rx_buffer[ check_buf_cnt ] == 0 ) 
                {
                    uart_rx_buffer[ check_buf_cnt ] = 13;
                }
            }
            // Storages data in current buffer
            rsp_cnt += rsp_size;
            if ( rsp_cnt < PROCESS_PARSER_BUFFER_SIZE )
            {
                strncat( current_parser_buf, uart_rx_buffer, rsp_size );
            }
            
            // Clear RX buffer
            memset( uart_rx_buffer, 0, PROCESS_RX_BUFFER_SIZE );
            
            if (strstr(current_parser_buf, "ERROR")) {
               return -1;
            }
               
            if (strstr(current_parser_buf, "OK")) {
               log_printf( &logger, "%s", current_parser_buf );
               Delay_100ms( );
               return 1;
            }
               
            if ( data_mode == 1) {
                log_printf( &logger, "%s", current_parser_buf );
                uart_write( &ble4.uart, "Hello", 5 );
                Delay_ms( 2000 );
                uart_write( &ble4.uart, "BLE4", 4 );
            }
        } 
        else 
        {
            process_cnt--;
            
            // Process delay 
            Delay_ms( 100 );
        }
    }
    
    return 0;
}

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

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

    ble4_cfg_setup( &cfg );
    BLE4_MAP_MIKROBUS( cfg, MIKROBUS_1 );
    ble4_init( &ble4, &cfg );

    ble4_reset( &ble4 );
    Delay_1sec( );
    
    log_printf( &logger, "Configuring the module...\n" );
    Delay_1sec( );
    
    ble4_set_dsr_pin( &ble4, 1 );
    Delay_ms( 20 );

    do {
        ble4_set_echo_cmd( &ble4, 1 );
        Delay_100ms( );
    }
    while( ble4_process( ) != 1 );
    
    do {
        ble4_set_local_name_cmd( &ble4, "BLE 4 Click" );
        Delay_100ms( );
    }
    while( ble4_process( ) != 1 );
    
    do {
        ble4_connectability_en_cmd( &ble4, BLE4_GAP_CONNECTABLE_MODE );
        Delay_100ms( );
    }
    while( ble4_process( ) != 1 );
    
    do {
        ble4_discoverability_en_cmd( &ble4, BLE4_GAP_GENERAL_DISCOVERABLE_MODE );
        Delay_100ms( );
    }
    while( ble4_process( ) != 1 );
    
    do {
        ble4_enter_mode_cmd( &ble4, BLE4_DATA_MODE );
        Delay_100ms( );
    }
    while( ble4_process( ) != 1 );
    
    ble4_set_dsr_pin( &ble4, 0 );
    Delay_ms( 20 );
    data_mode = 1;
    log_printf( &logger, "The module has been configured.\n" );
}

void application_task ( void )
{
    ble4_process( );
}

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.