Author Topic: Level shifting  (Read 5741 times)

dougal

  • Sr. Member
  • ****
  • Posts: 289
Level shifting
« on: July 09, 2013, 12:39:01 pm »
What is a good way to deal with projects that involve both 5V and 3.3V components?


I've got a few sensors that are 3.3V, and I'm a little scared of frying them.


Just dropping a single output from 5V to 3.3V seems simple enough. But how would you deal with bi-directional communication on multiple I/O pins?


Mark

  • Full Member
  • ***
  • Posts: 196
Re: Level shifting
« Reply #1 on: July 09, 2013, 01:05:16 pm »
Dougal
For I2C devices that are 3v3 I have made the pullup resistor go to the 3v3 line.

It hasn't fried that one.


Mark

Bluebie

  • Sr. Member
  • ****
  • Posts: 486
Re: Level shifting
« Reply #2 on: July 09, 2013, 04:43:42 pm »
yep that should be fine for i2c. You can buy dedicated level shifting chips, but if you prefer to build things out of individual components one somewhat inefficient way is the technique used by the disipark for it's USB connector. Grab some 3.3v zenner diodes and connect the spark to your device through some resistors (500ohm or something like that) and with zenner diodes connecting to the resistors *on the device side* to ground, with the diode oriented so it's stripe is on the signal side, not the ground side. This will effectively short circuit any extra voltage above 3.3v, and the resistor will limit the current. It is inefficient because the extra voltage is thrown away as heat, just like the linear regulator on the digispark and other arduinos does to take input voltages down to 5v.


You can even use this technique to make crude power supplies for small chips and microcontrollers, but it's pretty crummy because it ends up wasting nearly the same amount of power regardless how much the chip attached uses. Good if you don't have any linear regulators on hand and your project isn't battery powered though.


Something like this isn't necessary for i2c devices because the digispark should never connect 5v to the device - it should only alternate between leaving the pin floating (unconnected) and connected to ground.


Another solution is to power your digispark at less than 5v. By connecting the 5v connection point to a 3.3v power supply the whole thing will run at that lower voltage just fine, but you should compile your project with the 1mhz or 8mhz option, as 16mhz and 16.5mhz are out of spec for operating below roughly 4v. Also note that you cannot use usb when running at lower voltages, unless you specifically disconnect the usb power pin somehow (bit of tape over it or something) and power the device externally.

dougal

  • Sr. Member
  • ****
  • Posts: 289
Re: Level shifting
« Reply #3 on: July 09, 2013, 06:30:05 pm »
Thanks for all the great information. For some reason, I was thinking that if you powered the Digispark at less than 5V (even on the 5V input, not Vin) that the voltage got boosted up. I'm sure 8MHz or lower would be fine for the low-voltage components I have.


And at least one sensor (temp/barometer) does use I2C, so all I have to worry about there is just the voltage to the power inputs, not the data lines -- good to know. Or, I could even use a 5V source, with a couple of resistors as a voltage divider for the sensor power, yes?




Bluebie

  • Sr. Member
  • ****
  • Posts: 486
Re: Level shifting
« Reply #4 on: July 17, 2013, 07:24:24 pm »
If you power the spark at 5v, a resistor voltage divider might work, but it's a really crummy way to do it. You would be better off using a resistor from 5v to signal, then a zenner diode from ground to signal to limit the voltages. Alternatively you could use a diode of some sort from 5v to the resistor, then resistor to signal. LEDs can be good. A red LED has a forward voltage drop of about 1.8-2.0v, so if you use that as your diode, the voltage after the LED will be about 3-3.2v, which should work great. You can also likewise power the i2c device through a red LED to drop the voltage down. 5v -> red led -> 3.3v device -> gnd. Most 3.3v devices are tolerant of voltages from 3.0v up to 3.6v.


If you don't have any red leds, but you do have some blue, white, or ultraviolet leds, you can use them like zenner diodes, but facing the other way - their 3.3v forward voltage will drain out any extra power above roughly 3.3v. You may even be able to visibly see when the data bus is active by the brightness of the LEDs (they'll turn off whenever a device pulls to ground).

dougal

  • Sr. Member
  • ****
  • Posts: 289
Re: Level shifting
« Reply #5 on: July 18, 2013, 12:17:31 pm »
Interesting. I did recently buy a big bag of assorted LEDs from Amazon, a whole rainbow (they even came with the proper current-limiting resistors for each color). What I probably need to do is break out my multimeter and start experimenting with some of these things.