terahurts
Posts: 5
Joined: Wed Sep 18, 2013 10:14 am

I2C level shifter - Explain like I'm 5...

Sat Sep 21, 2013 8:27 am

I've got a project in mind for doing some basic temperature monitoring and switching some (mains) lights for an aquarium and I'm trying to get my head around I2C. I've done nothing with electronics since leaving school over 20 years ago and I'm more than a bit rusty on the terminology.

I want to interface:

I2C/Serial LCD backpack display : http://www.hobbytronics.co.uk/i2clcd-backpack-v2
I2C relay board: http://www.ebay.co.uk/itm/Twin-Relay-wi ... 19bfa93519

I2C/1-Wire interface: http://www.abelectronics.co.uk/products ... /1-Wire-Pi

The relay board and LCD backpack both list 5v, does this mean I need to insert a logic level shifter into the I2C bus, and if so would one of these http://www.hobbytronics.co.uk/raspberry ... -level-i2c be all that is required?

Dutch_Master
Posts: 362
Joined: Sat Jul 27, 2013 11:36 am

Re: I2C level shifter - Explain like I'm 5...

Sat Sep 21, 2013 1:17 pm

I2C is a 'common collector' bus. That means that the attached device just has a transistor (or similar) to ground and relies on a resistor to pull the data line high. In your case, have the I2C bus at 5V and protect the input pins of the RPi with a resistor (100 Ohms is enough).

User avatar
Tage
Posts: 287
Joined: Fri May 24, 2013 2:29 am
Location: St Thomas, Ontario Canada

Re: I2C level shifter - Explain like I'm 5...

Sat Sep 21, 2013 2:22 pm

yes, you will need a level shifter to connect a 5V gadget that uses 5V on the I2C bus to the Pi that works with 3.3V.
the simplest level shifter is the type you see on the schematic for the last link. but you do not need the 5V to 3.3V regulator. the Pi already has 3.3V.
the level shifter works in this way:
the MOSFET has the Gate connected to 3.3V and the Source connected to the Pi GPIO. the Drain connects to the I2C bus on the gadget. a resistor from the GPIO pin to 3.3V pulls the pin voltage to 3.3V when the GPIO pin is inactive.

you need a resistor from Drain to 5V, to pull up the pin voltage when the Pi GPIO is high and the MOSFET is OFF.
When the Pi GPIO goes low, the MOSFET turns ON, because its gate is at 3.3V and its Source is at GND. So the Drain goes low.

if the gadget pulls its pin low, current will flow in the body diode of the MOSFET and the Source is pulled down. a low level appears on the Pi GPIO (Source). the MOSFET turns ON, so the Source will swing down to the Drain voltage.

the idea with this contraption is that any device on the I2C bus can pull down the bus voltage. and the 5V devices will see 5V levels and the 3.3V devices will see 3.3V levels. No device can force the bus voltage high. the GPIO must be set so that it has open drain configuration, or a very weak pullup.

for a better understanding, read the I2C specification. there is also a good NXP app note about level shifting, "AN97055, Bidirectional level shifter for I2C bus and other systems". anybody that wants to use this type of level shifter should be aware of its limitations. if the temperature is low, a "normal" low level MOSFET will not work, you need one that has low enough gate threshold. BSN20 is one example. it costs about $0.30 at Mouser. there are also special ICs for level shifting I2C.

Galaxynik
Posts: 43
Joined: Mon Jan 02, 2012 2:55 pm

Re: I2C level shifter - Explain like I'm 5...

Sat Sep 21, 2013 5:50 pm

you dont need any sort of level shifter for the 1 wire interface - I have one of these (surplus to requirements used once before I realised I needed more than one bus so I went for the rpi3 from sheepwalk (8 buses)0 pm Ime if you want to save a few quid :-)

Return to “Interfacing (DSI, CSI, I2C, etc.)”