Skip to content

I2C Communication and Sensors

I2C interface

The I2C bus uses two pins:

  • SCL — clock
  • SDA — data

Connect SCL, SDA, power (3.3 V or 5 V as required by the sensor), and GND.

Access the bus via p.I2C.

I2C function calls

Scan the I2C bus and return addresses that responded (0–127).

Example: HMC5883L (address 0x1E = 30)

from eyes17 import eyes
p = eyes.open()
print(p.I2C.scan())
# e.g. [30]

Write bytes to an I2C slave.

parameter description
address slave address 0–127
bytestream list of bytes

HMC5883L: set measurement range

# CONFB register = 0x01; gain in bits 5–7
p.I2C.writeBulk(30, [0x01, 1 << 5])

Read bytes starting at register regaddr.

parameter description
address slave address
regaddr starting register
numbytes how many bytes to read
return list of bytes, or False on timeout

HMC5883L: read X, Y, Z raw data

from numpy import int16
vals = p.I2C.readBulk(30, 0x03, 6)
Bx = int16(vals[0] << 8 | vals[1])
By = int16(vals[2] << 8 | vals[3])
Bz = int16(vals[4] << 8 | vals[5])
print(Bx, By, Bz)

Set I2C bus frequency in Hz (default is typically ~100–400 kHz depending on firmware).

p.I2C.config(100000)  # 100 kHz

Auto-detect and read sensors

p.guess_sensor() scans the bus and, for known addresses, returns a list of initialized sensor objects from eyes17.SENSORS.

Sensors with first-class connect() support in guess_sensor():

Address Sensor Docs
0x48 ADS1115 page
0x23 BH1750 page
0x77 BMP180
0x78 / 118 BMP280 / BME280 page
0x29 VL53L0X page
0x68 MPU6050 page
0x1E HMC5883L page
0x0D QMC5883L see HMC5883L page
0x5A MLX90614 page
0x39 TSL2561 page
0x40 SHT21
0x57 MAX30100 (logger) page

SPI devices use p.SPI, not I2C.

p.guess_sensor()
from eyes17 import eyes
p = eyes.open()
sens = p.guess_sensor()
# DETECTED :  [30]
# ____DOCS____ : HMC5883L ...
print(sens[0].getRaw())
# or sens[0].getVals() depending on the driver
Manually initialize a known sensor
from eyes17 import eyes
from eyes17.SENSORS import HMC5883L, TSL2561, MPU6050, BMP280

p = eyes.open()
mag = HMC5883L.connect(p.I2C, address=0x1E)
print(mag.getRaw())

light = TSL2561.connect(p.I2C)
print(light.getRaw())  # [total, IR, ...]

Example sensors

Detailed pages:

from eyes17 import eyes
from eyes17.SENSORS import MPU6050, TSL2561

p = eyes.open()
print(MPU6050.connect(p.I2C).getRaw())
print(TSL2561.connect(p.I2C).getRaw())

Non-I2C: ultrasonic SR04

Distance via SQ2/IN2 is covered under Digital I/O:

print(p.sr04_distance(), 'cm')

Graphical sensor logger

The ExpEYES / SEELab desktop app includes I2C Modules → General Purpose I2C Sensors: auto-detect, live gauges, logging, and curve fitting.

I2C Modules → General Purpose I2C Sensors

Gauges

Logging and analysis

Android app / Blockly