# README
I2C-bus interaction for peripheral sensors on Single Board Computers
This library written in Go programming language intended to activate and interact with the I2C bus by reading and writing data.
Usage
func main() {
// create new connection to I2C dev on /dev/i2c-0 with address 0x27
i2cDevice, _ := i2c.New(0x27, "/dev/i2c-0")
// free I2C connection on exit
defer i2cDevice.Close()
// write to command 0x1 the value of 0xF3
if _, err := i2cDevice.WriteBytes([]byte{0x1, 0xF3}); err != nil {
return err
}
}
Note: Error handling has been skipped for brevity.
Fork
Forked from https://github.com/googolgl/go-i2c as this library is unresponsive to Pull requests. Changes in this fork include:
- Added
ReadRegU32BE()
andWriteRegBytes()
functions. - Added
WriteThenReadBytes()
function. - Clean up of code: removal of Logrus debug logging and CGO.
Tutorial
In repositories contain quite a lot projects, which use i2c library as a starting point to interact with various peripheral devices and sensors for use on embedded Linux devices. All these libraries start with a standard call to open I2C-connection to specific bus line and address, than pass i2c instance to device.
You will find here the list of all devices and sensors supported by me, that reference this library:
- Liquid-crystal display driven by Hitachi HD44780 IC.
- BMP180/BMP280/BME280 temperature and pressure sensors.
- DHT12/AM2320 humidity and temperature sensors.
- Si7021 relative humidity and temperature sensor.
- SHT3x humidity and temperature sensor.
- VL53L0X time-of-flight ranging sensor.
- BH1750 ambient light sensor.
- MPL3115A2 pressure and temperature sensor.
- PCA9685 16-Channel 12-Bit PWM Driver.
- MCP23017 16-Bit I/O Expander with Serial Interface Driver.
Getting help
GoDoc documentation
Troubleshooting
How to enable I2C bus
Various SBC's (Single Board Computers) has vendor specific methods for activating the I2C
bus on the GPIO Pins. On the Raspberry Pi you may need to activate using the raspi-config
utility. On Radxa Rock Pi devices uses rsetup
to activate through Overlays.
After activating the I2C bus a reboot is usually required for it to start working.
How to find I2C bus allocation and device address
Use the i2cdetect
utility to check your i2c bus is active.
$ i2cdetect -l
i2c-10 i2c ddc I2C adapter
i2c-1 i2c rk3x-i2c I2C adapter
i2c-6 i2c rk3x-i2c I2C adapter
i2c-4 i2c rk3x-i2c I2C adapter
i2c-11 i2c ddc I2C adapter
i2c-0 i2c rk3x-i2c I2C adapter
i2c-9 i2c fde50000.dp I2C adapter
i2c-7 i2c rk3x-i2c I2C adapter
To discover is a sensor is active on the bus, scan the appropriate bus it is connected to.
$ i2cdetect -y 0
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: 60 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Above we can see that the VNCL4040 is connected at address 0x60
.
License
Go-i2c is licensed under MIT License.