Skip to main content
This doc relates to the below revision of hardware
Mecha Logo

Comet (rev5)

For Pilot users

The all new Mecha Comet

Better in all ways, the next revision of Mecha Comet, coming soon.

Learn more

I2C

Introduction to I2C

The Inter-Integrated Circuit (I2C) protocol is a synchronous, multi-master, multi-slave serial communication standard. It's designed for short-distance communication between integrated circuits, commonly used to interface microcontrollers with peripherals such as sensors, displays, and memory devices.

I2C Protocol Fundamentals

  • Master-Slave Architecture: Communication is initiated and controlled by a master device, with slave devices responding to requests.
  • Two-Wire Interface: I2C utilizes two bidirectional lines:
    • SCL (Serial Clock Line): Synchronizes data transfer between devices.
    • SDA (Serial Data Line): Carries data between the master and slave devices.
  • Addressing: Each slave device is assigned a unique 7-bit or 10-bit address, allowing the master to selectively communicate with specific devices.
  • Speed Modes: I2C supports various speed modes, including Standard Mode (100 kHz), Fast Mode (400 kHz), and High-Speed Mode (3.4 MHz).

I2C Implementation on Mecha Comet GPIO

The Mecha Comet device supports I2C communication through its GPIO pins, allowing seamless integration with various I2C-compatible peripherals. The following sections outline the pin configuration and how to access I2C on the Mecha Comet.

FunctionGPIO Pin
I2C2_SCLGPIO5_IO16
I2C2_SDAGPIO5_IO17
I2C4_SCLGPIO5_IO20
I2C4_SDAGPIO5_IO21

How I2C Works Over GPIO

I2C can be implemented in two ways:

  1. Hardware I2C: Dedicated I2C hardware controllers within the Mecha Comet's processor manage the I2C protocol, offering optimal performance and reliability.
  2. Bit-Banged I2C: Software-driven implementation of the I2C protocol using general-purpose GPIO pins. This method is less efficient and may be used in situations where hardware I2C resources are limited.

The Mecha Comet prioritizes hardware I2C for its superior performance and robustness.

Accessing I2C on Debian-Based Systems

Checking Available I2C Buses

$ ls /dev/i2c-*

This will list available I2C buses, typically /dev/i2c-1, /dev/i2c-2, etc.

Enabling I2C

Ensure the I2C kernel module is loaded:

$ sudo modprobe i2c-dev

For persistent enabling, add it to /etc/modules:

echo "i2c-dev" | sudo tee -a /etc/modules

Installing I2C Utilities

$ sudo apt update && sudo apt install -y i2c-tools

Detecting I2C Devices

$ sudo i2cdetect -y 2

This will scan I2C bus 2 for connected devices.


Working with I2C To Read BMP180 Sensor Data

To demonstrate I2C communication on the Mecha Comet, this section uses the BMP180 sensor. The illustrated I2C interaction techniques are applicable to all I2C devices that can be integrated with the Mecha Comet.

BMP180 sensor and pin-out

bmp sensorbmp sensor pinout

BMP180 Pin Configuration

BMP180 is available in two modules. One is Five pin module and other is Four pin module. With Five pin module we have additional +3.3V pin which is absent in four pin module. Other than that the functioning is same.

Pin NameDescription
VCCConnected to +5V
GNDConnected to ground.
SDASerial Data pin (I2C interface)
SCLSerial Clock pin (I2C interface)
3.3VIf +5V is not present. Can power module by connecting +3.3V to this pin.

BMP180 MODULE Features

  • Can measure temperature and altitude.
  • Pressure range: 300 to 1100hPa
  • High relative accuracy of ±0.12hPa
  • Can work on low voltages
  • 3.4Mhz I2C interface
  • Low power consumption (3uA)
  • Pressure conversion time: 5msec
  • Potable size

BMP180 MODULE Specifications

  • Operating voltage of BMP180: 1.3V – 3.6V
  • Input voltage of BMP180MODULE: 3.3V to 5.5V
  • Peak current : 1000uA
  • Consumes 0.1uA standby
  • Maximum voltage at SDA , SCL : VCC + 0.3V
  • Operating temperature: -40ºC to +80ºC

How to attach Sensor to Mecha comet gpio

bmp sensor attached to mecha comet

To detect i2c device on the comet device use the following command

sudo i2cdetect -y 0

This is how output looks like on the device

image.png

Example Code demonstrates how to read data from the BMP180 sensor using I2C on the Mecha Comet.

Using Python

import time
from smbus2 import SMBus

I2C_BUS = 2
BMP180_ADDRESS = 0x77

# Calibration register addresses
CALIBRATION_REGS = {
'AC1': 0xAA, 'AC2': 0xAC, 'AC3': 0xAE,
'AC4': 0xB0, 'AC5': 0xB2, 'AC6': 0xB4,
'B1': 0xB6, 'B2': 0xB8, 'MB': 0xBA,
'MC': 0xBC, 'MD': 0xBE,
}

def read_int16(bus, addr, signed=True):
hi = bus.read_byte_data(BMP180_ADDRESS, addr)
lo = bus.read_byte_data(BMP180_ADDRESS, addr + 1)
val = (hi << 8) + lo
return val if not signed else val - 65536 if val > 32767 else val

def read_calibration_data(bus):
return {
k: read_int16(bus, v, signed=(k not in ['AC4', 'AC5', 'AC6']))
for k, v in CALIBRATION_REGS.items()
}


def read_raw_temperature(bus):
bus.write_byte_data(BMP180_ADDRESS, 0xF4, 0x2E)
time.sleep(0.005)
msb = bus.read_byte_data(BMP180_ADDRESS, 0xF6)
lsb = bus.read_byte_data(BMP180_ADDRESS, 0xF7)
return (msb << 8) + lsb

def read_raw_pressure(bus, oss=0):
bus.write_byte_data(BMP180_ADDRESS, 0xF4, 0x34 + (oss << 6))
time.sleep(0.005)
msb = bus.read_byte_data(BMP180_ADDRESS, 0xF6)
lsb = bus.read_byte_data(BMP180_ADDRESS, 0xF7)
xlsb = bus.read_byte_data(BMP180_ADDRESS, 0xF8)
return ((msb << 16) + (lsb << 8) + xlsb) >> (8 - oss)

def calculate_true_temperature(ut, calib):
x1 = ((ut - calib['AC6']) * calib['AC5']) >> 15
x2 = (calib['MC'] << 11) // (x1 + calib['MD'])
b5 = x1 + x2
temp = (b5 + 8) >> 4
return temp, b5

def calculate_true_pressure(up, b5, calib, oss=0):
b6 = b5 - 4000
x1 = (calib['B2'] * ((b6 * b6) >> 12)) >> 11
x2 = (calib['AC2'] * b6) >> 11
x3 = x1 + x2
b3 = (((calib['AC1'] * 4 + x3) << oss) + 2) >> 2

x1 = (calib['AC3'] * b6) >> 13
x2 = (calib['B1'] * ((b6 * b6) >> 12)) >> 16
x3 = ((x1 + x2) + 2) >> 2
b4 = (calib['AC4'] * (x3 + 32768)) >> 15
b7 = (up - b3) * (50000 >> oss)

if b7 < 0x80000000:
p = (b7 << 1) // b4
else:
p = (b7 // b4) << 1

x1 = (p >> 8) * (p >> 8)
x1 = (x1 * 3038) >> 16
x2 = (-7357 * p) >> 16
return p + ((x1 + x2 + 3791) >> 4)

def main():
with SMBus(I2C_BUS) as bus:
calib = read_calibration_data(bus)

ut = read_raw_temperature(bus)
temp, b5 = calculate_true_temperature(ut, calib)

up = read_raw_pressure(bus)
pressure = calculate_true_pressure(up, b5, calib)

print(f"Temperature: {temp / 10:.1f} °C")
print(f"Pressure: {pressure / 100:.2f} hPa")

if __name__ == "__main__":
main()

To Run the Python Script on the Mecha Comet:

  1. Save the script as bmp180_read.py.
  2. Make sure you have smbus2 installed:
    $ pip3 install smbus2
  3. Run the script:
    $ python3 bmp180_read.py
  4. Ensure you have the necessary permissions to access GPIO lines. You may need to run the script with sudo or add your user to the gpio group.
  5. Check the BMP180 connected to I2C pins on the Mecha Comet to see if it read the temperature and pressure data correctly.

Use Cases

  1. Sensor Integration I2C enables seamless data acquisition from a wide range of environmental sensors, including temperature, humidity, and barometric pressure sensors, facilitating real-time monitoring and data logging.
  2. Peripheral Interfacing I2C simplifies the connection and control of peripheral devices such as OLED displays for visual output, Real-Time Clocks (RTCs) for accurate timekeeping, and EEPROM chips for non-volatile data storage.
  3. Microcontroller Intercommunication I2C provides a robust and efficient method for establishing communication between microcontrollers, such as interfacing the Mecha Comet with Arduino or other MCU platforms, enabling distributed processing and control.