I2C

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

Toit exposes the bus through the serial.protocols.i2c library.

import gpio
import serial.protocols.i2c as i2c

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

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

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

  device := bus.device 0x76

Frequency

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

main:
  bus := i2c.Bus
    --sda=gpio.Pin 21
    --scl=gpio.Pin 22
    --frequency=100_000  // Limit to 100kHz.
When changing the frequency, the associated pull-up resistors may have to be changed as well.