exp: golang.org/x/exp/io/i2c Index | Examples | Files | Directories

package i2c

import "golang.org/x/exp/io/i2c"

Package i2c allows users to read from and write to a slave I2C device.


This package is not maintained anymore. An actively supported cross-platform alternative is https://periph.io/.



Package Files

devfs.go i2c.go

func TenBit Uses

func TenBit(addr int) int

TenBit marks an I2C address as a 10-bit address.

type Devfs Uses

type Devfs struct {
    // Dev is the I2C bus device, e.g. /dev/i2c-1. Required.
    Dev string

Devfs is an I2C driver that works against the devfs. You need to load the "i2c-dev" kernel module to use this driver.

func (*Devfs) Open Uses

func (d *Devfs) Open(addr int, tenbit bool) (driver.Conn, error)

type Device Uses

type Device struct {
    // contains filtered or unexported fields

Device represents an I2C device. Devices must be closed once they are no longer in use.

func Open Uses

func Open(o driver.Opener, addr int) (*Device, error)

Open opens a connection to an I2C device. All devices must be closed once they are no longer in use. For devices that use 10-bit I2C addresses, addr can be marked as a 10-bit address with TenBit.


d, err := i2c.Open(&i2c.Devfs{Dev: "/dev/i2c-1"}, 0x39)
if err != nil {

// opens a 10-bit address
d, err = i2c.Open(&i2c.Devfs{Dev: "/dev/i2c-1"}, i2c.TenBit(0x78))
if err != nil {

_ = d

func (*Device) Close Uses

func (d *Device) Close() error

Close closes the device and releases the underlying sources.

func (*Device) Read Uses

func (d *Device) Read(buf []byte) error

Read reads len(buf) bytes from the device.

func (*Device) ReadReg Uses

func (d *Device) ReadReg(reg byte, buf []byte) error

ReadReg is similar to Read but it reads from a register.

func (*Device) Write Uses

func (d *Device) Write(buf []byte) (err error)

Write writes the buffer to the device. If it is required to write to a specific register, the register should be passed as the first byte in the given buffer.

func (*Device) WriteReg Uses

func (d *Device) WriteReg(reg byte, buf []byte) (err error)

WriteReg is similar to Write but writes to a register.


driverPackage driver contains interfaces to be implemented by various I2C implementations.

Package i2c imports 5 packages (graph) and is imported by 32 packages. Updated 2020-12-30. Refresh now. Tools for package owners.