Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Using I2C protocol with the nRF52

November 19, 2023

Using I2C Protocol with the nRF52

The I2C (Inter-Integrated Circuit) protocol is a popular serial communication protocol that allows multiple devices to communicate with each other over a single bus. It is widely used in various applications such as sensors, displays, and memory devices. In this blog post, we will explore how to use the I2C protocol with the nRF52 microcontroller.

Introduction to I2C Protocol

I2C is a synchronous, master-slave communication protocol. It requires only two wires - SDA (Serial Data) and SCL (Serial Clock). The SDA line carries data, while the SCL line handles clock synchronization.

The I2C protocol supports multiple devices on the same bus by assigning each device a unique 7-bit (or sometimes 10-bit) address. The master initiates communication by sending start and stop conditions, and the slaves respond based on their assigned address.

Set Up I2C on the nRF52

To use the I2C protocol with the nRF52 microcontroller, we need to configure the appropriate pins and registers. Here is an example of how to set up I2C using the nRF52 SDK:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include "nrf_drv_twim.h"

#define TWI_INSTANCE_ID 0

const nrf_drv_twim_t m_twim = NRF_DRV_TWIM_INSTANCE(TWI_INSTANCE_ID);

// Initialize I2C
void twim_init(void)
{
    ret_code_t err_code;
    
    nrf_drv_twim_config_t twim_config = NRF_DRV_TWIM_DEFAULT_CONFIG;
    twim_config.sda = SDA_PIN;
    twim_config.scl = SCL_PIN;
    twim_config.frequency = NRF_TWIM_FREQ_100K;
    
    err_code = nrf_drv_twim_init(&m_twim, &twim_config, NULL, NULL);
    APP_ERROR_CHECK(err_code);
    
    nrf_drv_twim_enable(&m_twim);
}

In the above example, SDA_PIN and SCL_PIN represent the GPIO pins connected to the SDA and SCL lines, respectively. We use the nRF52 TWIM (Two-Wire Interface Master) driver from the nRF SDK to handle the low-level I2C communication.

Writing to and Reading from I2C Slaves

Once we have set up the I2C on the nRF52, we can start communicating with I2C slave devices. Let’s see an example of how to write to and read from an I2C slave:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "nrf_drv_twim.h"

#define TWI_INSTANCE_ID 0
#define SLAVE_ADDRESS 0x42

const nrf_drv_twim_t m_twim = NRF_DRV_TWIM_INSTANCE(TWI_INSTANCE_ID);

// Write data to an I2C slave
void write_to_slave(uint8_t* data, uint8_t length)
{
    ret_code_t err_code;
    
    err_code = nrf_drv_twim_tx(&m_twim, SLAVE_ADDRESS, data, length, false);
    APP_ERROR_CHECK(err_code);
    
    while (nrf_drv_twim_is_busy(&m_twim));
}

// Read data from an I2C slave
void read_from_slave(uint8_t* data, uint8_t length)
{
    ret_code_t err_code;
    
    err_code = nrf_drv_twim_rx(&m_twim, SLAVE_ADDRESS, data, length);
    APP_ERROR_CHECK(err_code);
    
    while (nrf_drv_twim_is_busy(&m_twim));
}

In the above example, SLAVE_ADDRESS represents the 7-bit address of the I2C slave device we want to communicate with. The write_to_slave function writes data to the slave, while the read_from_slave function reads data from the slave. The length parameter represents the number of bytes to write or read.

Conclusion

Using the I2C protocol with the nRF52 microcontroller is straightforward once you have set up the appropriate pins and configured the TWIM driver. This blog post provided a brief introduction to the I2C protocol and demonstrated how to write to and read from I2C slave devices using the nRF52 SDK.

I hope this blog post was helpful in understanding how to use the I2C protocol with the nRF52.


➡️ Implementing SPI communication on the nRF52


⬅️ Exploring the UART communication on the nRF52


Go back to Posts.