RPi I2C 2018

(older version of RPi I2C 2017)

** This is the simple methos of using I2C with Python and RPi. It uses just one functions and also the clock stretch timeout can be set **

See https://www.byvac.com/index.php/Raspberry_Pi_and_I2C for more details.

Instructions

I2C is disabled by default and so needs enablingr via raspi-config

sudo raspi-config

sudo raspi-config' or through the RPi desktop. It is also advisable to install i2c tools:

sudo apt-get install -y i2c-tools

You can then use

i2cdetect -y 1

to check that the hardware is connected okay

Download the zip file above an place into a directory.

The module implements a single function as follows:

return value if any = i.i2c(<device address>,[list of bytes to send],<number of bytes to return>,X)

<device address> This is the I2C of the address connected to the bus

[list of bytes to send] This can be empty []. The function will send the bytes in the list

<number of bytes to return> After sending byte it will read and return this number of bytes, can be 0

X is optional and should either be left off the list or set to 0. If set to 0 then that address will be sent to the I2C bus instead of the <device address>. This will normally be 0 for a general call address, any read will still use the <device address>.

Examples

mem = i.i2c(0xd0,[3,0xe8],10)

Reads 10 bytes of memory from address 1000 (0x3e8) into mem; mem is a list of bytes.

i.i2c(0x44,[1,2,3],0)

Sends bytes 1,2,3 to a device with the address 0x44

i.i2c(22,[],5)

Read 5 bytes form a device with address 22

i.i2c(0x33,[0x55],1,0)

Send a general 0x55 to all devices on the bus using general call (address 0) and return 1 byte from device 0x33

Class

The class initialisation has 3 options:

i2c_base = 0x20804000 This is the base address of the BSC1 register and can be found in the BCM data sheet, the default (shown) is for the BCM2835 which is fitted to most RPi

timeout = 3000 Is the number of clock cycles the device will wait before deciding the slave will not respond. The default value of 3000 will give 30mS using a clock of 100kHz

bus = 1 The I2C bus number nearly always 1 unless a very old RPi is used. If this is changed the the i2c_base will also need changing

Voltage Levels

This troubles some. The master I2C, in this case the RPi has pull up resistors that go the to 3.3V supply. I2C has two logic states 'off' and 'zero'. The master will drive the I2C bus to zero by effectively switching the pin to ground. When a logic 1 is required the master simple switched the pin to high impedance (or it should - hopefully). The pull up resisters will then take the pin to logic 1.

It should then be clear that even on a 5V system, logic 1 is determined by the pull up resistors. If follows then that provided the master and slave behave correctly a 5V device can be connected to a 3.3V device provided the pull up resistors go to 3.3V

Timeout

When a slave device is busy after receiving a command from the master, it will hold the SCL low to indicate to the master that it is not ready. This is called clock stretching. The RPi will conform to this rule but will only wait by default for 0.6mS and then device that the slave is not responding and return with an error.

For 'real' devices, LCD display for example this is simply not enough and really needs a mechanism for adjusting. A clear screen on an lcd display or a write to an EEPROM may take a few mS.

To this end the new i2crpi module on initialisation will write to the relevant BCM register and the time out can be adjusted from 0 to 65535 * clock cycles