So you’ve got your Particle Photon or Electron, you’ve got your sensors and external components and you’re ready to build an IoT device to make the world your slave. But hang on a minute, the sensor you have only operates on 5V and you vaguely recall that using 5V with the Particle I/O pins could be bad.. watch on.
Hello and welcome to our tutorial on Logic Level Converters for Particle. I’m Sam from Core Electronics and today we’re going to be taking a look at what are Logic Levels and how to convert them. How to use 5 V devices with 3.3 V devices and vice versa and make sure we don’t damage any components that aren’t specified for the right voltage.
So what is a Logic Level first of all? Well a Logic level is simply the operating voltage, the maximum digital voltage that a microcontroller or a system operates at. So an Arduino Uno for example is a 5V microcontroller because the microcontroller is being fed 5V and it then has that 5V to output on digital pins, it can accept 5V on its input pins - things like that, it’s 5V compatible. Now a 3.3V device is exactly the same, it operates and 3.3V and the high signal is 3.3V and the low signal is 0, the low level is 0 volts. Now why this is important is that voltages aren’t always compatible, much like too much current going into a pin an incorrect voltage can potentially damage a pin or an entire chip and give you a nice little frisbee or coaster which isn’t what you want. So how do we prevent this? Well we use whats called a Logic Level Converter and these are important because if I take a 5V Microcontroller - say we’re operating on an I2C bus - and I connect that up to something like a photon. Now a Photon operates on 3.3V from Particle and now most of the pins are what we call ‘5 volts tolerant’ and you might hear this word come up a bit, particularly with Particle gear and the Teensy Boards which are DIY boards designed for people who’ve come from platforms like Arduino. Arduino operates on 5 volts so they get it, you know they understand that someone might accidentally hook up 5 volts so they’re ‘tolerant’ which means you won’t destroy or damage your board by putting 5V into the pin, connecting one of the pins to 5V but it’s still only going to output 3.3V. So it’s tolerant but it’s not a 5V Board.
So say we’re hooking these 2 controllers up and we’re accidentally sending 5V into the Photon now there are 2 pins on the Photon which aren’t 5V tolerant so it happens to be one of those pins then you’re going to either fry a pin or damage your board as I’ve said. That brings us to Logic Level Converters - i’ll bring this into the shot here, now Logic Level Converter - this red chip here if you can see, is really quite simple. It’s made up of special kind of transistors which their purpose is to convert Logic Levels and what happens is you connect a 5V supply up to one side and the 3.3V up to the other side. Your higher voltage and your low voltage side. This is a bi-directional converter from SparkFun, it’s awesome, it’s probably my favourite logic level converter. Its small, it’s easy, it’s breadboard compatible and it’s bi-directional which is great. If you input a 3.3V signal to the low voltage side it’s going to come out as a 5V signal on the higher voltage side. When it’s zero volts or ground it will be ground as well. They are not designed for analogue signals, just something to bear in mind, they’re not designed to give a wide range and map that voltage from the 3.3V to the 5V. They are only digital convertors, something to keep in note. So that is sort of how they work and they just work by switching the supplies on or off when they get a voltage on that pin. Now this guy has 4 channels so there are 4 different pins on the high voltage side and the low voltage side that you can connect up to and it’s important to note that you still have to connect a 5V supply and a 3.3V supply up to the boards. They don’t magically generate these voltages on their own, they’re quite a simple chip as you can see. Fortunately the Photon has a 3.3V regulated line and a 5V line which taps on to the USB power supply which is regulated per USB spec which is pretty cool, So I’ve got here just as a bit of an example because sure, you know, try it and hope you don’t blow up your microcontroller! But let’s actually use it and watch it converting logic levels. I’ve got a Neo Pixel ring here from Adafruit - fantastic bit of gear, but it is 5V. The chips on the little addressable LEDs operate on 5V which means that our 3.3V pins on our microcontroller aren’t gong to be able to deliver that 5V on a logic level. Now logic level has thresholds which is important as well so even though it might not get the exact voltage - you know you might have seen a pin on a micro controller at 4.8V or 4.9V and you might think “oh, that’s not 5V - how is it working?” Well digital systems aren’t just designed to have one set voltage level - you have your 5V and the microcontroller will read pretty much anywhere. It depends on the chip itself but it’s usually between 2.8 to 3.3V is like the low threshold for your voltage and the 5V is the tops. So if you imagine that any voltage in between that gets triggered as a high voltage and then you’ve got a space - you know a bit of a buffer just to be sure - and then say between 1.8V or 2.2V to ground is registered as a low signal. So in some situations you can actually get away without putting 3.3V to a 5V device because a low voltage isn’t going to damage a device, only a high voltage. But it’s always best to be safe because I might want to get the data input back from this going into a 3.3V device and in which case the 5V is definitely going to damage something which isn’t good. So lesson learned - unless you really know what you’re doing always use a logic level converter.
So enough talk, enough talk, let’s plug this in - just using a USB port for power and you’ll see the Photon turn on and initialise and there we go, we’ve just got a simple rainbow function, kind of a wipe on this Neo Pixel - it looks quite pretty there, yeah it’s nice, but that is operating on 5V and this is 3.3V. Now if you don’t believe me let’s get our handy, ever faithful multimeter out and we’re going to just take a measurement. So these digital signals are moving extremely quickly so it’s going to be hard to get a voltage of the digital signal, but I’ll disconnect this just for a moment - actually I will NOT do that! I will grab another jumper cable from over here and we’re going to connect this up to our 3.3V line here and be sure to put it on the low voltage side. Now let’s get another jumper wire and connect our probe to ground - and this is just so you don’t take my word for it, you know that the logic level will magically work but that it actually does work (It’s taking a while to connect to the cloud and I’ll just reset that there) alright so I’ve got my ground wire connected. Now on this pin here I have got 3.3V - my multi meter is reading 3.3V and if I measure the pin on the other side I get 4.7V so that is in the high voltage range which is exactly what you want. That’s awesome, so it works! It converts a 3.3V digital signal to 5V and just like that, that is how you can safely protect your devices, your 5V devices and your 3.3V devices when you are using them with each other which is cool. So that is all for today guys, that is logic level conversion with Particle devices but it applies to any device, not just Particle device. This SparkFun breakout board - sorry, the logic level converter board for the breakout flex on there is awesome, bi-directional, super cheap - I love to have half a dozen handy on my bench for when I’m working on projects and you know in the spur of the moment I just need to connect different devices up to each other so I recommend picking up a few. All you’ll need to do is to solder the headers on - super easy. So go forth and use your digital devices safely and check out some of our other tutorials and projects for more awesome stuff. Have a great day guys - seeya! :-)