Intermediate
30 min

Experience the magic of protocol transformation with SC18IM704 and ATmega328P

UART to I2C wizardry: Your communication transformer!

UART to I2C Click with Arduino UNO Rev3

Published Feb 14, 2024

Click board™

UART to I2C Click

Dev Board

Arduino UNO Rev3

Compiler

NECTO Studio

MCU

ATmega328P

Unlock the potential of your UART devices by effortlessly integrating them into I2C networks using our transformation solution, simplifying data exchange and enhancing control

A

A

Hardware Overview

How does it work?

UART to I2C Click is based on the SC18IM704, a bridge between the standard UART port and a serial I2C bus from NXP Semiconductors. The SC18IM704 consists of a full-functional advanced UART interface that communicates with the host MCU through the commonly used RX and TX pins of the mikroBUS™ socket. It is also characterized by a high baud rate of up to 460.8 kbit/s and 256-byte FIFO for the transfer/receive data process. The serial data format is fixed to a one start bit, 8 data bits, and one stop bit. After the reset feature, the baud rate defaults to 9600 bit/s and can be changed through the Baud Rate Generator (BRG) registers. After a Power-Up sequence or already-mentioned hardware reset, achievable through the RST pin of the mikroBUS™ socket, the

SC18IM704 will send two continuous bytes to the host MCU to indicate a Start-Up condition. These two continuous bytes are 0x4F and 0x4B, representing an ‘OK’ state in the ASCII messages protocol. After the correct initial sequence, direct communication is enabled with other I2C-bus devices connected to the populated I2C-bus terminal. The I2C bus uses two wires (SCL and SDA) to transfer information between connected devices, providing a byte-oriented interface that supports data transfers up to 400kHz. The SC18IM704 can also be placed in a software-configurable low-power mode (Power-Down mode). Upon entering the Power-Down state, the UART RX pin is used to exit Deep Power-down mode. The bridge remains in the Deep

Power-down mode as long as the RX pin remains in a high logic state. Any character sent brings the bridge out of Deep Power-down mode but ignores the character. In addition to all these features, the SC18IM704 has several general-purpose I/O pins on the populated header with labeled GP pins. These pins have the option of software setting their function as push-pull, open-drain, or input-only. 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.

UART to I2C Click top side image
UART to I2C Click bottom side 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

Used MCU Pins

mikroBUS™ mapper

NC
NC
AN
Reset
PD2
RST
NC
NC
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
NC
NC
PWM
NC
NC
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

UART to I2C 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 UART to I2C Click driver.

Key functions:

  • uarttoi2c_gpio_write - This function writes a desired data to the gpio port

  • uarttoi2c_gpio_read - This function reads data from the gpio port

  • uarttoi2c_i2c_write_then_read - This function performs a write then read with a repeated start to the I2C target device.

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 main.c
 * @brief UART to I2C Click Example.
 *
 * # Description
 * This example demonstrates the use of USB to I2C click board by reading the device ID
 * of a 3D Hall 11 click board connected to the I2C port and controlling the GPIO pins.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes the driver and performs the click default config which resets
 * the device and sets the GPIO pins 0-3 as push-pull output and others as input.
 * After that, reads and displays the chip firmware version.
 *
 * ## Application Task
 * Reads the device ID of a 3D Hall 11 click board connected to the I2C port, 
 * toggles the output pins and displays the GPIO port state. The results will
 * be displayed on the USB UART approximately once per second.
 *
 * @author Stefan Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "uarttoi2c.h"

// I2C target device configuration
#define DEVICE_NAME                 "3D Hall 11 click"
#define DEVICE_SLAVE_ADDRESS        0x35
#define DEVICE_REG_ID               0x0D
#define DEVICE_ID                   0x01

static uarttoi2c_t uarttoi2c;
static log_t logger;

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    uarttoi2c_cfg_t uarttoi2c_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.
    uarttoi2c_cfg_setup( &uarttoi2c_cfg );
    UARTTOI2C_MAP_MIKROBUS( uarttoi2c_cfg, MIKROBUS_1 );
    if ( UART_ERROR == uarttoi2c_init( &uarttoi2c, &uarttoi2c_cfg ) ) 
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }
    
    if ( UARTTOI2C_ERROR == uarttoi2c_default_cfg ( &uarttoi2c ) )
    {
        log_error( &logger, " Default configuration." );
        for ( ; ; );
    }
    
    uint8_t version[ 16 ] = { 0 };
    if ( UARTTOI2C_OK == uarttoi2c_read_version ( &uarttoi2c, version ) )
    {
        log_printf( &logger, " Firmware version: %s\r\n", version );
    }
    
    log_info( &logger, " Application Task " );
}

void application_task ( void ) 
{
    static uint8_t gpio_state = UARTTOI2C_NO_PIN_MASK;
    uint8_t slave_address = DEVICE_SLAVE_ADDRESS;
    uint8_t reg_addr = DEVICE_REG_ID;
    uint8_t device_id;
    if ( UARTTOI2C_OK == uarttoi2c_i2c_write_then_read ( &uarttoi2c, slave_address, 
                                                         &reg_addr, 1, &device_id, 1 ) )
    {
        log_printf( &logger, " %s - Device ID read: %s\r\n", ( char * ) DEVICE_NAME,
                    ( char * ) ( ( DEVICE_ID == device_id ) ? "Success" : "Fail" ) );
    }
    uarttoi2c_gpio_write ( &uarttoi2c, gpio_state );
    if ( UARTTOI2C_OK == uarttoi2c_gpio_read ( &uarttoi2c, &gpio_state ) )
    {
        log_printf( &logger, " GPIO state: 0x%.2X\r\n\n", ( uint16_t ) gpio_state );
        gpio_state = ~gpio_state;
    }
    Delay_ms ( 1000 );
}

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.