Beginner
10 min

Enjoy in our power efficient, long range wireless communication solution based on 32001324 and ATmega328P

Metering made easy: Your go-to solution

M-BUS RF 4 Click with Arduino UNO Rev3

Published Feb 14, 2024

Click board™

M-BUS RF 4 Click

Dev Board

Arduino UNO Rev3

Compiler

NECTO Studio

MCU

ATmega328P

Stay ahead in the world of metering solutions with the innovation and efficiency provided by our wireless M-Bus transceiver.

A

A

Hardware Overview

How does it work?

M-BUS RF 4 Click is based on the 32001324, an 868MHz band transceiver module from Mipot. The radio operates at the unlicensed 868 MHz SRD frequency band and has specified serial data rates of up to 115.2 Kbps. This M-BUS RF 4 click is ideal for developing various applications, mainly solutions for water or gas, as well as heat and electricity metering applications, and even on meter and concetrator devices. The module supports various operating modes (S, T, R, C) to meet the requirements of one-way and two-way data communication, in stationary and mobile systems. The embedded stack implemented according to EN13757-4 Standard provides the

physical access to the Wireless M-Bus communication. At startup time, the Module reads INTERFACE_SELECTION input pin state, which is routed to the PWM pin (marked SEL on Click board) of the mikroBUS™ socket. This pin should be left unconnected or set to high state to select the UART interface or set to low state to select the SPI interface for communicating with the main MCU. Widely used SPI and UART interfaces allow integration flexibility and easy development of customer products. The module meets all the requirements in the industrial temperature range -40/+85°C. Besides that, the module is also certified according to R&TTED 1999/05/EC

and is compliant with the ReACH And ROHS directives. The embedded stack implemented in the module according to EN13757-4 Standard provides the physical access to the Wireless M-Bus communication. M-BUS RF 4 click communicates with the target MCU through the mikroBUS™ UART or SPI interface, with additional functionality provided by IND, SEL, and RTS CS/CTS pins. This Click Board™ is designed to be operated only with 3.3V logic level. A proper logic voltage level conversion should be performed before the Click board™ is used with MCUs with logic levels of 5V.

M-BUS RF 4 Click hardware overview image

Features overview

Development board

Arduino UNO is a versatile microcontroller board built around the ATmega328P chip. It offers extensive connectivity options for various projects, featuring 14 digital input/output pins, six of which are PWM-capable, along with six analog inputs. Its core components include a 16MHz ceramic resonator, a USB connection, a power jack, an

ICSP header, and a reset button, providing everything necessary to power and program the board. The Uno is ready to go, whether connected to a computer via USB or powered by an AC-to-DC adapter or battery. As the first USB Arduino board, it serves as the benchmark for the Arduino platform, with "Uno" symbolizing its status as the

first in a series. This name choice, meaning "one" in Italian, commemorates the launch of Arduino Software (IDE) 1.0. Initially introduced alongside version 1.0 of the Arduino Software (IDE), the Uno has since become the foundational model for subsequent Arduino releases, embodying the platform's evolution.

Arduino UNO Rev3 double side image

Microcontroller Overview

MCU Card / MCU

default

Architecture

AVR

MCU Memory (KB)

32

Silicon Vendor

Microchip

Pin count

28

RAM (Bytes)

2048

You complete me!

Accessories

Click Shield for Arduino UNO has two proprietary mikroBUS™ sockets, allowing all the Click board™ devices to be interfaced with the Arduino UNO board without effort. The Arduino Uno, a microcontroller board based on the ATmega328P, provides an affordable and flexible way for users to try out new concepts and build prototypes with the ATmega328P microcontroller from various combinations of performance, power consumption, and features. The Arduino Uno has 14 digital input/output pins (of which six can be used as PWM outputs), six analog inputs, a 16 MHz ceramic resonator (CSTCE16M0V53-R0), a USB connection, a power jack, an ICSP header, and reset button. Most of the ATmega328P microcontroller pins are brought to the IO pins on the left and right edge of the board, which are then connected to two existing mikroBUS™ sockets. This Click Shield also has several switches that perform functions such as selecting the logic levels of analog signals on mikroBUS™ sockets and selecting logic voltage levels of the mikroBUS™ sockets themselves. Besides, the user is offered the possibility of using any Click board™ with the help of existing bidirectional level-shifting voltage translators, regardless of whether the Click board™ operates at a 3.3V or 5V logic voltage level. Once you connect the Arduino UNO board with our Click Shield for Arduino UNO, you can access hundreds of Click boards™, working with 3.3V or 5V logic voltage levels.

Click Shield for Arduino UNO accessories 1 image

868MHz right-angle rubber antenna is a compact and versatile solution for wireless communication. Operating within the frequency range of 868-915MHz, it ensures optimal signal reception and transmission. With a 50-ohm impedance, it's compatible with various devices and systems. This antenna boasts a 2dB gain, enhancing signal strength and extending communication range. Its vertical polarization further contributes to signal clarity. Designed to handle up to 50W of input power, it's a robust choice for various applications. Measuring just 48mm in length, this antenna is both discreet and practical. Its SMA male connector ensures a secure and reliable connection to your equipment. Whether you're working with IoT devices, remote sensors, or other wireless technologies, the 868MHz right-angle antenna offers the performance and flexibility you need for seamless communication.

M-BUS RF 4 Click accessories image

Used MCU Pins

mikroBUS™ mapper

Data Indicate
PC0
AN
NC
NC
RST
SPI Chip Select
PB2
CS
SPI Clock
PB5
SCK
SPI Data OUT
PB4
MISO
SPI Data IN
PB3
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Communication Selection
PD6
PWM
UART RTS
PC3
INT
UART TX
PD0
TX
UART RX
PD1
RX
NC
NC
SCL
NC
NC
SDA
NC
NC
5V
Ground
GND
GND
1

Take a closer look

Schematic

M-BUS RF 4 Click Schematic schematic

Step by step

Project assembly

Click Shield for Arduino UNO front image hardware assembly

Start by selecting your development board and Click board™. Begin with the Arduino UNO Rev3 as your development board.

Click Shield for Arduino UNO front image hardware assembly
Arduino UNO Rev3 front image hardware assembly
Charger 27 Click front image hardware assembly
Prog-cut hardware assembly
Charger 27 Click complete accessories setup image hardware assembly
Arduino UNO Rev3 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 image step 5 hardware assembly
Necto image step 6 hardware assembly
Arduino UNO 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 via Debug Mode

1. Once the code example is loaded, pressing the "DEBUG" button initiates the build process, programs it on the created setup, and enters Debug mode.

2. After the programming is completed, a header with buttons for various actions within the IDE becomes visible. Clicking the green "PLAY" button starts reading the results achieved with the Click board™. The achieved results are displayed in the Application Output tab.

DEBUG_Application_Output

Software Support

Library Description

This library contains API for M-BUS RF 4 Click driver.

Key functions:

  • mbusrf4_send_command - Header and checksum are calculated and sent at the beginning (header) and finally (checksum)

  • mbusrf4_generic_write - This function write specific number of data.

  • mbusrf4_generic_read - This function read data of maximum length.

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 MBusRf4 Click example
 *
 * # Description
 * This example reads and processes data from M-BUS RF 4 clicks.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes driver init, reads basic information and checks communication
 *
 * ## Application Task
 *  In the RX mode it is waiting to receive data from another module...
 *  In the TX mode sends the data packet....
 *
 * ## Additional Function
 * - mbusrf4_process ( ) - The general process of collecting data and adding it to application buffer;
 *
 * - mbrusrf4_clear_buff ( void ) - Clear application buffer data;
 *
 * - mbusrf4_parser_tx ( void ) - Transmit data status parser;
 *
 * - mbusrf4_parser_rx ( uint8_t logg_type ) - Receiver data parser;
 *
 * - mbusrf4_log_data ( uint8_t log_type, uint8_t *log_buf, int32_t log_len ) - Log application buffer;
 *
 * ## Note: You can't send less then 10 data byte!
 *
 *
 * \author MikroE Team
 *
 */
// ------------------------------------------------------------------- INCLUDES

#include "board.h"
#include "log.h"
#include "mbusrf4.h"
#include "string.h"
#include "generic_pointer.h"

#define PROCESS_COUNTER 10
#define PROCESS_RX_BUFFER_SIZE 256
#define PROCESS_PARSER_BUFFER_SIZE 256

#define LOG_HEX 0
#define LOG_STR 1
#define LOG_DEC 2

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

#define DEMO_APP_RECEIVER
// #define DEMO_APP_TRANSMITER

static mbusrf4_t mbusrf4;
static log_t logger;

static char parser_buf[ PROCESS_PARSER_BUFFER_SIZE ];
static int32_t parser_cnt = 0;

static uint8_t * __generic_ptr parser_ptr;

uint8_t msg[ ] = "MikroE -  FW team";

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


static void mbrusrf4_clear_buff ( void );

static void mbusrf4_parser_tx ( void );

static void mbusrf4_parser_rx ( uint8_t logg_type );

static void mbusrf4_process ( void );

static void mbusrf4_log_data ( uint8_t log_type, uint8_t *log_buf, int32_t log_len );


static void mbusrf4_process ( void )
{
    int32_t rsp_size;

    char uart_rx_buffer[ PROCESS_RX_BUFFER_SIZE ] = { 0 };
    uint16_t check_buf_cnt;
    uint8_t process_cnt = PROCESS_COUNTER;


    rsp_size = mbusrf4_generic_read( &mbusrf4, uart_rx_buffer, PROCESS_RX_BUFFER_SIZE );

    if ( rsp_size > 0 )
    {
        if ( parser_cnt + rsp_size >= PROCESS_PARSER_BUFFER_SIZE )
        {
            log_info( &logger, "Buffer Overflow!" );
            mbrusrf4_clear_buff(  );
        }
        else
        {
            for( int32_t rsp_cnt = 0; rsp_cnt < rsp_size; rsp_cnt++ )
            {
                parser_buf[ parser_cnt ] = uart_rx_buffer[ rsp_cnt ];
                parser_cnt++;
                if ( parser_cnt >= parser_cnt + rsp_size )
                    break;
            }
        }
    }
}

static void mbusrf4_parser_rx ( uint8_t logg_type )
{
    const int32_t RSP_LEN = 2;
    const int32_t TIMEOUT_EXIT = 10000;
    uint8_t * __generic_ptr rsp_start;
    uint8_t full_rsp = 0;
    int32_t timeout_cnt = 0;
    int32_t rsp_len = 0;
    int32_t rsp_start_index = 0;

    for ( ; ; )
    {
        rsp_start = strchr( parser_ptr, MBUSRF4_HEADER );
        if (rsp_start != 0)
            break;
        else
            mbusrf4_process();

        timeout_cnt++;
        Delay_ms( 1 );
        if ( timeout_cnt >= TIMEOUT_EXIT )
        {
            log_error( &logger, "TIMEOUT!( Header not found )" );
            mbrusrf4_clear_buff();
            return;
        }
    }

    timeout_cnt = 0;

    for ( ; ; )
    {
        for ( int32_t cnt = 0; cnt < parser_cnt; cnt++ )
        {
            if ( rsp_start == ( parser_ptr + cnt ) )
            {
                if ( cnt + RSP_LEN <= parser_cnt )
                {
                    rsp_start_index = cnt;
                    full_rsp = 1;
                }
                else
                    full_rsp = 0;
            }
        }

        if ( full_rsp == 1 )
            break;
        else
            mbusrf4_process();

        timeout_cnt++;
        Delay_ms( 1 );
        if ( timeout_cnt >= TIMEOUT_EXIT )
        {
            log_error( &logger, "TIMEOUT! ( Response length not found )" );
            return;
        }
    }

    timeout_cnt = 0;

    rsp_len = ( int32_t )parser_buf[ rsp_start_index + 2 ];

    if ( rsp_len <= 0 )
    {
        mbrusrf4_clear_buff();
        return;
    }

    for ( ; ; )
    {
        if ( ( rsp_start_index + RSP_LEN + rsp_len + 1 ) <= parser_cnt )
            full_rsp = 1;
        else
            full_rsp = 0;

        if ( full_rsp == 1 )
            break;
        else
            mbusrf4_process();

        timeout_cnt++;
        Delay_ms( 1 );
        if ( timeout_cnt >= TIMEOUT_EXIT )
        {
            log_error( &logger, "TIMEOUT! ( Response not found )" );
            return;
        }
    }

    rsp_start_index += 3;
    mbusrf4_log_data( logg_type, &parser_buf[ rsp_start_index ], rsp_len );
    mbrusrf4_clear_buff();
}

static void mbusrf4_parser_tx ( void )
{
    const int32_t RSP_LEN = 4;
    const int32_t STATUS_DIFF = 3;
    const int32_t TIMEOUT_EXIT = 5000;

    uint8_t * __generic_ptr rsp_start;
    uint8_t full_rsp = 0;
    int32_t timeout_cnt = 0;

    for ( ; ; )
    {
        rsp_start = strchr( parser_ptr, MBUSRF4_HEADER );
        if ( rsp_start != 0 )
            break;
        else
            mbusrf4_process();

        timeout_cnt++;
        Delay_ms( 1 );
        if ( timeout_cnt >= TIMEOUT_EXIT )
        {
            log_error( &logger, "TIMEOUT!( Header not found )" );
            return;
        }
    }

    timeout_cnt = 0;

    for ( ; ; )
    {
        for ( int32_t cnt = 0; cnt < parser_cnt; cnt++ )
        {
            if ( rsp_start == ( parser_ptr + cnt ) )
            {
                if ( cnt + RSP_LEN <= parser_cnt )
                {
                    full_rsp = 1;
                }
                else
                {
                    full_rsp = 0;
                }
            }
        }

        if ( full_rsp == 1 )
            break;
        else
            mbusrf4_process();

        timeout_cnt++;
        Delay_ms( 1 );
        if ( timeout_cnt >= TIMEOUT_EXIT )
        {
            log_error( &logger, "TIMEOUT! ( Response not found )" );
            return;
        }
    }

    rsp_start += STATUS_DIFF;

    if ( *rsp_start == 0x00 )
        log_info( &logger, "TX OK" );
    else if ( *rsp_start == 0xFF )
        log_info( &logger, "TX ERROR" );
    else
        log_error( &logger, "TX PARSER ERROR" );
}

static void mbrusrf4_clear_buff ( void )
{
    memset( parser_buf, 0, parser_cnt );
    parser_cnt = 0;
}

static void mbusrf4_log_data ( uint8_t log_type, uint8_t *log_buf, int32_t log_len )
{
    if ( LOG_HEX == log_type )
    {
        for ( int32_t data_cnt = 0; data_cnt < log_len; data_cnt++ )
        {
            log_printf( &logger, "[ 0x%.02X ]", ( int32_t )( *( log_buf + data_cnt ) ) );
        }
    }
    else if( LOG_STR == log_type )
    {
        for ( int32_t data_cnt = 0; data_cnt < log_len; data_cnt++ )
        {
            log_printf( &logger, "%c", *( log_buf + data_cnt ) );
        }
    }
    else if( LOG_DEC == log_type )
    {
        for ( int32_t data_cnt = 0; data_cnt < log_len; data_cnt++ )
        {
            log_printf( &logger, "%d", ( int32_t )( *( log_buf + data_cnt ) ) );
        }
    }
    else
    {
        log_error( &logger, "Log type error!" );
    }
    log_printf( &logger, "\r\n" );
}

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

void application_init ( void )
{
    log_cfg_t log_cfg;
    mbusrf4_cfg_t cfg;

    uint8_t payload_buff[ 20 ] = { 0 };

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

    mbusrf4_cfg_setup( &cfg );
    MBUSRF4_MAP_MIKROBUS( cfg, MIKROBUS_1 );
    mbusrf4_init( &mbusrf4, &cfg );

    parser_cnt = 0;
    parser_ptr = &parser_buf[ 0 ];
    mbusrf4_process( );
    mbrusrf4_clear_buff();

    //Command SET mode
    payload_buff[ 0 ] = MBUSRF4_SET_VALUE_IN_EEPROM_MEMORY;
    payload_buff[ 1 ] = MBUSRF4_EEPARAM_WMBUS_MODE_S2_SHORT_PREAMBLE;

    mbusrf4_send_command( &mbusrf4, MBUSRF4_CMD_SET_MODE, 2, &payload_buff[ 0 ] );
    Delay_ms( 500 );
    mbusrf4_process( );
    mbusrf4_parser_tx();
    mbrusrf4_clear_buff();

    // Reads FW version
    mbusrf4_send_command( &mbusrf4, MBUSRF4_CMD_GET_FW_VERSION, 0, &payload_buff[ 0 ] );
    Delay_ms( 500 );
    mbusrf4_process( );
    log_info( &logger, "FW version:" );
    mbusrf4_parser_rx( LOG_HEX );
    log_printf( &logger, "-----------------------------------------------------------\r\n" );
    mbusrf4_process( );

    Delay_ms( 1000 );
    log_info( &logger, "Application Task" );
}

void application_task ( void )
{
    // RX App mode
    #ifdef DEMO_APP_RECEIVER

    if ( mbusrf4_get_state_ind( &mbusrf4 ) == 0 )
    {
        Delay_ms( 100 );
        mbusrf4_process( );

        mbusrf4_parser_rx( LOG_STR );
    }

    #endif

    // TX App Mode
    #ifdef DEMO_APP_TRANSMITER

    mbusrf4_transmit_data( &mbusrf4, msg, 17 );
    Delay_ms( 100 );
    mbrusrf4_clear_buff();
    mbusrf4_parser_tx();
    Delay_ms( 2000 );

    #endif
}

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.