I2C

I2C is a serial communication bus able to address multiple devices along the same 2-wire bus.

Toit exposes the peripheral through the i2c library.

import i2c

main:
  bus := i2c.Bus
      --sda=21
      --scl=22

Each device must have a unique address. The address can be found in the datasheet for the selected peripheral - it's an 7-bit integer.

In case of the Bosch BME280 sensor, the address is 0x76:

import i2c

main:
  bus := i2c.Bus
      --sda=21
      --scl=22

  device := bus.device 0x76

Frequency

The default frequency of the I2C bus is 400kHz in Toit. This can be changed as an argument to the i2c.Bus at construction time with

  i2c.Bus
      --sda=21
      --scl=22
      --frequency=100000

Remember that when changing the frequency, the associated pull-up resistors may have to be changed as well.