Mistake on this page?
Report an issue in GitHub or email us

I2C

I2C class hierarchy

The I2C interface provides I2C Master functionality. I2C is a two wire serial protocol that allows an I2C Master to exchange data with an I2C Slave. You can use it to communicate with I2C devices such as serial memories, sensors and other modules or integrated circuits.

The I2C protocol supports up to 127 devices per bus, and its default clock frequency is 100KHz.

Note: Remember that you need a pull-up resistor on sda and scl. All drivers on the I2C bus are required to be open collector, and so it is necessary to use pull-up resistors on the two signals. A typical value for the pull-up resistors is around 2.2k ohms, connected between the pin and 3v3.

I2C class reference

Note: The Arm Mbed API uses 8 bit addresses, so make sure to left-shift 7 bit addresses by 1 bit before passing them.

Public Member Functions
 I2C (PinName sda, PinName scl)
 Create an I2C Master interface, connected to the specified pins. More...
 I2C (const i2c_pinmap_t &static_pinmap)
 Create an I2C Master interface, connected to the specified pins. More...
void frequency (int hz)
 Set the frequency of the I2C interface. More...
int read (int address, char *data, int length, bool repeated=false)
 Read from an I2C slave. More...
int read (int ack)
 Read a single byte from the I2C bus. More...
int write (int address, const char *data, int length, bool repeated=false)
 Write to an I2C slave. More...
int write (int data)
 Write single byte out on the I2C bus. More...
void start (void)
 Creates a start condition on the I2C bus. More...
void stop (void)
 Creates a stop condition on the I2C bus. More...
virtual void lock (void)
 Acquire exclusive access to this I2C bus. More...
virtual void unlock (void)
 Release exclusive access to this I2C bus. More...
int transfer (int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, const event_callback_t &callback, int event=I2C_EVENT_TRANSFER_COMPLETE, bool repeated=false)
 Start nonblocking I2C transfer. More...
void abort_transfer ()
 Abort the ongoing I2C transfer. More...

I2C hello, world

/*
 * Copyright (c) 2014-2020 Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mbed.h"

// Read temperature from LM75BD

I2C i2c(I2C_SDA, I2C_SCL);

const int addr7bit = 0x48;      // 7 bit I2C address
const int addr8bit = 0x48 << 1; // 8bit I2C address, 0x90

int main()
{
    char cmd[2];
    while (1) {
        cmd[0] = 0x01;
        cmd[1] = 0x00;
        i2c.write(addr8bit, cmd, 2);

        ThisThread::sleep_for(500);

        cmd[0] = 0x00;
        i2c.write(addr8bit, cmd, 1);
        i2c.read(addr8bit, cmd, 2);

        float tmp = (float((cmd[0] << 8) | cmd[1]) / 256.0);
        printf("Temp = %.2f\n", tmp);
    }
}

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.