Advanced
30 min

Incorporate high-quality Bluetooth audio streaming into your projects with BM62 and PIC18F46K80

Fully qualified Bluetooth 5.0 stereo audio module

BT Audio 2 Click with EasyPIC v8

Published Mar 15, 2024

Click board™

BT Audio 2 Click

Dev. board

EasyPIC v8

Compiler

NECTO Studio

MCU

PIC18F46K80

Create the next generation of portable audio devices and voice communication systems

A

A

Hardware Overview

How does it work?

BT Audio 2 Click is based on BM62, a Bluetooth stereo audio module from Microchip. It includes advanced audio features, such as multi-band dynamic range control, parametric multi-band equalizer, audio widening, and virtual bass are inbuilt. The audio effect algorithms improve the user’s audio listening experience in terms of better audio quality after audio signal processing. The input and output audios have different stages, and each stage can be programmed to vary the gain response characteristics. A Digital Signal Processor (DSP) performs speech and audio processing. Advanced speech features, such as acoustic echo cancellation and noise reduction, are built. To

reduce nonlinear distortion and to help echo cancellation, an outgoing signal level to the speaker is monitored and adjusted to avoid saturation of speaker output or microphone input. BT Audio 2 Click supports one analog (line-in) signal from the external audio source. The DSP can process the analog (line-in) signal to generate different sound effects (multi-band dynamic range compression and audio widening), which can be configured using the DSP tool. Also, the module has an AFH function to avoid RF interference. It has an algorithm to check the nearby interference and to choose a clear channel for the transceiver Bluetooth signal. The on-chip Power Management

Unit (PMU) has two main features: a lithium-ion and lithium-polymer battery charger and a voltage regulator. A power switch switches the power source between the battery and an adapter. Also, the PMU provides current to drive two LEDs. The LED and button settings can be configured. This Click Board™ uses the UART communication interface and 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 different logic levels.

BT Audio 2 Click hardware overview image

Features overview

Development board

EasyPIC v8 is a development board specially designed for the needs of rapid development of embedded applications. It supports many high pin count 8-bit PIC microcontrollers from Microchip, regardless of their number of pins, and a broad set of unique functions, such as the first-ever embedded debugger/programmer. 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, EasyPIC v8 provides a fluid and immersive working experience, allowing access anywhere and under any

circumstances at any time. Each part of the EasyPIC v8 development board contains the components necessary for the most efficient operation of the same board. In addition to the advanced integrated CODEGRIP programmer/debugger module, which offers many valuable programming/debugging options and seamless integration with the Mikroe software environment, the board 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 DEVICE, and CAN are also included, including the well-established mikroBUS™ standard, two display options (graphical and character-based LCD), and several different DIP sockets. These sockets cover a wide range of 8-bit PIC MCUs, from the smallest PIC MCU devices with only eight up to forty pins. EasyPIC 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.

EasyPIC v8 horizontal image

Microcontroller Overview

MCU Card / MCU

default

Architecture

PIC

MCU Memory (KB)

64

Silicon Vendor

Microchip

Pin count

40

RAM (Bytes)

3648

You complete me!

Accessories

These standard small stereo earphones offer a high-quality listening experience with their top-notch stereo cable and connector. Designed for universal compatibility, they effortlessly connect to all MIKROE mikromedia and multimedia boards, making them an ideal choice for your electronic projects. With a rated power of 100mW, the earphones provide crisp audio across a broad frequency range from 20Hz to 20kHz. They boast a sensitivity of 100 ± 5dB and an impedance of 32Ω ± 15%, ensuring optimal sound quality. The Φ15mm speaker delivers clear and immersive audio. Cost-effective and versatile, these earphones are perfect for testing your prototype devices, offering an affordable and reliable audio solution to complement your projects.

BT Audio 2 Click accessories 1 image

Used MCU Pins

mikroBUS™ mapper

External Address Bus Negative
RA2
AN
Reset
RE1
RST
System Configuration PIn 1
RE0
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
NC
NC
3.3V
Ground
GND
GND
Multifunction button / Power-On
RC0
PWM
System Configuration PIn 2
RB0
INT
UART TX
RC6
TX
UART RX
RC7
RX
NC
NC
SCL
NC
NC
SDA
Power Supply
5V
5V
Ground
GND
GND
1

Take a closer look

Click board™ Schematic

BT Audio 2 Click Schematic schematic

Step by step

Project assembly

EasyPIC v8 front image hardware assembly

Start by selecting your development board and Click board™. Begin with the EasyPIC v8 as your development board.

EasyPIC v8 front image hardware assembly
GNSS2 Click front image hardware assembly
MCU DIP 40 hardware assembly
GNSS2 Click complete accessories setup image hardware assembly
EasyPIC v8 Access DIPMB 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
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

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 BT Audio 2 Click driver.

Key functions:

  • btaudio2_set_device_name - This function sets the local device name

  • btaudio2_make_call - This function makes a call to the specified phone number

  • btaudio2_volume_up - This function increases the volume of a specified gain mask

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 BT Audio 2 Click Example.
 *
 * # Description
 * This example demonstrates the use of BT Audio 2 click board by reading the commands
 * received from remote device and performing adequate actions accordingly.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes the driver and configures the click board.
 *
 * ## Application Task
 * Reads all the received events and parses them.
 *
 * ## Additional Function
 * - static err_t btaudio2_event_handler ( btaudio2_t *ctx ) - This function handles SPP data event 
 * and several BTM state events added in. The other events will just be displayed on the USB UART in hex format.
 *
 * @note
 * We have used the Serial Bluetooth Terminal smartphone application for the test. 
 * A smartphone and the click board must be paired in order to exchange messages
 * with each other. So make sure to pair your device with the click board and
 * connect it to using the Serial Bluetooth Terminal application, then you will be able 
 * to send commands listed below.
 *
 * @author Stefan Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "btaudio2.h"

static btaudio2_t btaudio2;
static log_t logger;

// Bluetooth device name
#define DEVICE_NAME                 "BT Audio 2 click"

// SPP Commands list
#define COMMAND_PLAY                "play"          // play music
#define COMMAND_PAUSE               "pause"         // pause music
#define COMMAND_STOP                "stop"          // stop music
#define COMMAND_NEXT                "next"          // next song
#define COMMAND_PREVIOUS            "prev"          // previous song
#define COMMAND_VOLUME_UP           "up"            // volume up
#define COMMAND_VOLUME_DOWN         "down"          // volume down
#define COMMAND_MODE_UP             "mode"          // switch equalizer mode
#define COMMAND_CALL                "call"          // call command followed by the number
#define COMMAND_VOICE               "voice"         // start voice recognition app

/**
 * @brief BT Audio 2 event handler function.
 * @details This function handles SPP data event and several BTM state events added in.
 * The other events will just be displayed on the USB UART in hex format.
 * @param[in] ctx : Click context object.
 * See #btaudio2_t object definition for detailed explanation.
 * @return @li @c  >=0 - Command ACK event response,
 *         @li @c   -1 - Error.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t btaudio2_event_handler ( btaudio2_t *ctx );

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    btaudio2_cfg_t btaudio2_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.
    btaudio2_cfg_setup( &btaudio2_cfg );
    BTAUDIO2_MAP_MIKROBUS( btaudio2_cfg, MIKROBUS_1 );
    if ( UART_ERROR == btaudio2_init( &btaudio2, &btaudio2_cfg ) ) 
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }
    
    if ( BTAUDIO2_OK != btaudio2_default_cfg ( &btaudio2 ) )
    {
        log_error( &logger, " Default configuration." );
        for ( ; ; );
    }
    
    if ( BTAUDIO2_OK != btaudio2_set_device_name ( &btaudio2, DEVICE_NAME ) )
    {
        log_error( &logger, " Set device name." );
        for ( ; ; );
    }
    
    log_info( &logger, " Application Task " );
}

void application_task ( void ) 
{
    if ( BTAUDIO2_OK == btaudio2_read_event ( &btaudio2 ) ) 
    {
        btaudio2_event_handler ( &btaudio2 );
    }
}

int main ( void ) 
{
    application_init( );
    
    for ( ; ; ) 
    {
        application_task( );
    }

    return 0;
}

static err_t btaudio2_event_handler ( btaudio2_t *ctx )
{
    err_t error_flag = BTAUDIO2_OK;
    static btaudio2_eq_mode_t eq_mode = BTAUDIO2_EQ_MODE_OFF;
    switch ( ctx->event_packet.opcode )
    {
        case BTAUDIO2_EVT_REPORT_SPP_DATA:
        {
            if ( 0 == ctx->event_packet.param_buf[ 1 ] ) // single packet
            {                
                uint16_t payload_len = ( ( uint16_t ) ctx->event_packet.param_buf[ 4 ] << 8 ) | 
                                                      ctx->event_packet.param_buf[ 5 ];
                if ( strstr ( &ctx->event_packet.param_buf[ 6 ], COMMAND_PLAY ) )
                {
                    error_flag |= btaudio2_send_mmi_action ( ctx, 0, BTAUDIO2_MMI_PLAY_PAUSE_MUSIC );
                    log_printf( &logger, " > play music\r\n\n" );
                } 
                else if ( strstr ( &ctx->event_packet.param_buf[ 6 ], COMMAND_PAUSE ) )
                {
                    error_flag |= btaudio2_send_mmi_action ( ctx, 0, BTAUDIO2_MMI_PLAY_PAUSE_MUSIC );
                    log_printf( &logger, " > pause music\r\n\n" );
                }
                else if ( strstr ( &ctx->event_packet.param_buf[ 6 ], COMMAND_STOP ) )
                {
                    error_flag |= btaudio2_send_mmi_action ( ctx, 0, BTAUDIO2_MMI_STOP_MUSIC );
                    log_printf( &logger, " > stop music\r\n\n" );
                }
                else if ( strstr ( &ctx->event_packet.param_buf[ 6 ], COMMAND_NEXT ) )
                {
                    error_flag |= btaudio2_send_mmi_action ( ctx, 0, BTAUDIO2_MMI_NEXT_SONG );
                    log_printf( &logger, " > next song\r\n\n" );
                }
                else if ( strstr ( &ctx->event_packet.param_buf[ 6 ], COMMAND_PREVIOUS ) )
                {
                    error_flag |= btaudio2_send_mmi_action ( ctx, 0, BTAUDIO2_MMI_PREVIOUS_SONG );
                    log_printf( &logger, " > previous song\r\n\n" );
                }
                else if ( strstr ( &ctx->event_packet.param_buf[ 6 ], COMMAND_VOLUME_UP ) )
                {
                    error_flag |= btaudio2_volume_up ( &btaudio2, 0, BTAUDIO2_VOLUME_MASK_A2DP );
                    log_printf( &logger, " > volume up\r\n\n" );
                }
                else if ( strstr ( &ctx->event_packet.param_buf[ 6 ], COMMAND_VOLUME_DOWN ) )
                {
                    error_flag |= btaudio2_volume_down ( &btaudio2, 0, BTAUDIO2_VOLUME_MASK_A2DP );
                    log_printf( &logger, " > volume down\r\n\n" );
                }
                else if ( strstr ( &ctx->event_packet.param_buf[ 6 ], COMMAND_CALL ) )
                {
                    uint8_t phone_number[ 19 ] = { 0 };
                    memcpy ( phone_number, strstr ( &ctx->event_packet.param_buf[ 6 ], COMMAND_CALL ) + 5, payload_len - 7 );
                    error_flag |= btaudio2_make_call ( &btaudio2, 0x00, phone_number );
                    log_printf( &logger, " > calling number: %s\r\n\n", phone_number );
                }
                else if ( strstr ( &ctx->event_packet.param_buf[ 6 ], COMMAND_VOICE ) )
                {
                    error_flag |= btaudio2_send_mmi_action ( ctx, 0, BTAUDIO2_MMI_VOICE_DIAL );
                    log_printf( &logger, " > start voice recognition\r\n\n" );
                }
                else if ( strstr ( &ctx->event_packet.param_buf[ 6 ], COMMAND_MODE_UP ) )
                {
                    if ( BTAUDIO2_EQ_MODE_USER1 == eq_mode )
                    {
                        eq_mode = BTAUDIO2_EQ_MODE_OFF;
                    }
                    else
                    {
                        eq_mode++;
                    }
                    btaudio2_set_eq_mode ( &btaudio2, eq_mode );
                    log_printf( &logger, " > equalizer mode: " );
                    switch ( eq_mode )
                    {
                        case BTAUDIO2_EQ_MODE_OFF:
                        {
                            log_printf( &logger, "off\r\n\n" );
                            break;
                        }
                        case BTAUDIO2_EQ_MODE_SOFT:
                        {
                            log_printf( &logger, "soft\r\n\n" );
                            break;
                        }
                        case BTAUDIO2_EQ_MODE_BASS:
                        {
                            log_printf( &logger, "bass\r\n\n" );
                            break;
                        } 
                        case BTAUDIO2_EQ_MODE_TREBLE:
                        {
                            log_printf( &logger, "treble\r\n\n" );
                            break;
                        }  
                        case BTAUDIO2_EQ_MODE_CLASSIC:
                        {
                            log_printf( &logger, "classic\r\n\n" );
                            break;
                        }
                        case BTAUDIO2_EQ_MODE_ROCK:
                        {
                            log_printf( &logger, "rock\r\n\n" );
                            break;
                        }
                        case BTAUDIO2_EQ_MODE_JAZZ:
                        {
                            log_printf( &logger, "jazz\r\n\n" );
                            break;
                        }
                        case BTAUDIO2_EQ_MODE_POP:
                        {
                            log_printf( &logger, "pop\r\n\n" );
                            break;
                        }
                        case BTAUDIO2_EQ_MODE_DANCE:
                        {
                            log_printf( &logger, "dance\r\n\n" );
                            break;
                        }
                        case BTAUDIO2_EQ_MODE_RNB:
                        {
                            log_printf( &logger, "rnb\r\n\n" );
                            break;
                        }
                        case BTAUDIO2_EQ_MODE_USER1:
                        {
                            log_printf( &logger, "user1\r\n\n" );
                            break;
                        }
                        default:
                        {
                            log_printf( &logger, "unknown\r\n\n" );
                            break;
                        }
                    }
                }
                else
                {
                    log_printf( &logger, " Unknown command: ", &ctx->event_packet.param_buf[ 6 ] );
                    for ( uint16_t cnt = 0; cnt < payload_len; cnt++ )
                    {
                        log_printf( &logger, "%c", ctx->event_packet.param_buf[ cnt + 6 ] );
                    }
                    log_printf( &logger, "\r\n\n" );
                }
            }
            break;
        }
        case BTAUDIO2_EVT_BTM_STATE:
        {
            switch ( ctx->event_packet.param_buf[ 0 ] )
            {
                case BTAUDIO2_BTM_STATE_PAIRING_SUCCESSFULL:
                {
                    log_printf( &logger, " < Pairing successfull - linked device ID: %u\r\n\n", 
                                ( uint16_t ) ctx->event_packet.param_buf[ 1 ] );
                    break;
                }
                case BTAUDIO2_BTM_STATE_SCO_LINK_CONNECTED:
                {
                    log_printf( &logger, " < SCO link connected - linked device ID: %u\r\n\n", 
                                ( uint16_t ) ctx->event_packet.param_buf[ 1 ] );
                    break;
                }
                case BTAUDIO2_BTM_STATE_SCO_LINK_DISCONNECTED:
                {
                    log_printf( &logger, " < SCO link disconnected - linked device ID: %u\r\n\n", 
                                ( uint16_t ) ctx->event_packet.param_buf[ 1 ] );
                    break;
                }
                case BTAUDIO2_BTM_STATE_ACL_CONNECTED:
                {
                    log_printf( &logger, " < ACL connected - linked data base: %u\r\n\n", 
                                ( uint16_t ) ctx->event_packet.param_buf[ 1 ] );
                    break;
                }
                case BTAUDIO2_BTM_STATE_ACL_DISCONNECTED:
                {
                    log_printf( &logger, " < ACL disconnected - " );
                    if ( 0 == ctx->event_packet.param_buf[ 1 ] )
                    {
                        log_printf( &logger, "disconnection\r\n\n" );
                    }
                    else if ( 1 == ctx->event_packet.param_buf[ 1 ] )
                    {
                        log_printf( &logger, "link loss\r\n\n" );
                    }
                    break;
                }
                case BTAUDIO2_BTM_STATE_STANDBY_STATE:
                {
                    log_printf( &logger, " < Standby state\r\n\n" );
                    break;
                }
                case BTAUDIO2_BTM_STATE_UNKNOWN_AUDIO_SOURCE:
                {
                    log_printf( &logger, " < Unknown audio source\r\n\n" );
                    break;
                }
                case BTAUDIO2_BTM_STATE_AUX_IN_AUDIO_SOURCE:
                {
                    log_printf( &logger, " < AUX-IN audio source\r\n\n" );
                    break;
                }
                case BTAUDIO2_BTM_STATE_A2DP_AUDIO_SOURCE:
                {
                    log_printf( &logger, " < A2DP audio source\r\n\n" );
                    break;
                }
                default:
                {
                    log_printf( &logger, " < EVENT BTM STATE: " );
                    for ( uint16_t cnt = 0; cnt < ctx->event_packet.param_len; cnt++ )
                    {
                        log_printf( &logger, "0x%.2X ", ( uint16_t ) ctx->event_packet.param_buf[ cnt ] );
                    }
                    log_printf( &logger, "\r\n\n" );
                    break;
                }
            }
            break;
        }
        default: 
        {
            log_printf( &logger, " < EVENT 0x%.2X: ", ( uint16_t ) ctx->event_packet.opcode );
            for ( uint16_t cnt = 0; cnt < ctx->event_packet.param_len; cnt++ )
            {
                log_printf( &logger, "0x%.2X ", ( uint16_t ) ctx->event_packet.param_buf[ cnt ] );
            }
            log_printf( &logger, "\r\n\n" );
            break;
        }
    }
    return error_flag;
}

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