# Looking to build signal level converter to reduce 20V input to 3.3v out. Am I on the right track?

I'm working on a small pet project with my RaspberryPi and I'm looking hook it up to some sensors. The output from the sensors is normally at 0v but when the sensor is triggered, goes high to 20v. The RaspberryPi's gpio ports will only accept a maximum voltage of 3.3v, so I've been researching how best to drop the input voltage from 20v to 3.3v, so it won't fry my Pi's gpio ports. The Pi doesn't need to send anything back, it's strictly one-way into the Pi, and the line will only go to 20v when the sensor is triggered.

In my research, I read about a circuit comprising of an LM317 Variable Regulator, and 2 resistors, which could give me the 20v to 3.3v that I need. However, I'm worried about the kind of heat that could be given off with such a large difference in voltage.

I've tried to be as clear as possible, and have also included a handy dandy schematic of what I mean.

simulate this circuit – Schematic created using CircuitLab

To add to things, I'm likely to be adding more 20v connections to other io ports on the Pi, probably up to 15, so I'm also considering what the combined heat could be like...

Am I on the right track, and is the heat just something that I'm going to have to work with? Or is there a better solution out there?

• What kind of line. Are these outputs or inputs or both? – Passerby Oct 4 '13 at 7:18
• Device outputs 20v into the Pi. Only one way. – nageeb Oct 4 '13 at 7:58
• You need signal logic level converting (20v -> 3.3v few mA), not power converting (hundreds of mA or Amps) – Passerby Oct 4 '13 at 8:10
• Yes. I've re-worded my question to make it a bit clearer... – nageeb Oct 4 '13 at 14:59

If they are signals to the pi then use a resistor potential divider. A 1k ohm across the input and 0v of the pi and a 9k resistor from the pi input to a high voltage logic signal will reduce that voltage by ten to one. You should be looking for slightly less than a 10:1 reduction. Can you work this bit out yourself? See the link.

• Wouldn't this tie Vout directly to Vin? – nageeb Oct 4 '13 at 14:57
• @nageeb. Vout would be separated from Vin by the 9k resistor. The 1k connects from pi input to ground thus acting as a ten to one attenuator. – Andy aka Oct 4 '13 at 16:01
• @nageeb I've added your diagram with the resistors in place. Remember you'll need to calculate a different value for the 9k to get 3V instead of 2V but I'm assuming you are able to think this one through. Let me know if you need help here. – Andy aka Oct 5 '13 at 10:00
• So I'm looking for a 5K resistor... Seems simple enough. I'll give it a shot. thanks! – nageeb Oct 6 '13 at 16:16
• @MitjaGustin resistors introduce thermal noise and, considering a 100 kHz bandwidth that noise would be about 4 uV RMS. Small enough? You decide. Resistors are very good (or can be on) precision and if necessary 0.1% resistors could be used. – Andy aka Aug 27 '18 at 12:08

The two solutions posted so far ignore one factor - isolation. It may or may not matter, but I would use an opto-isolator (EG a 4n25) just for peace of mind (and they're dirt cheap).

The LED side can be driven from the 20v signal with an appropriate resistor.

The sensor side just needs a pullup or down (depending how you wire it) to the pin of the Pi. In fact, you might get away without a pullup resistor if the Pi's CPU has internal pullups, but I suspect they'd be very weak and it's not good to rely on these things.

This way, if your input signal accidentally becomes 25v, or 50v, or 500v, or -5v, your Pi does not go "bang".

To add an example circuit, substitute 5v for your 3v3 supply. D1 is optional, but it's nice to leave it in to protect against reverse-voltages:

• I see the use of the OptoIsolator here, but I'm not sure if it's applicable in my case. How/Where does the 3.3v (5v in this schematic) factor in? I think I might not have been clear enough in my Question, and have re-worded it and included a schematic. – nageeb Oct 4 '13 at 14:59
• OK, it works like this: Your 20v goes into the "12v" end and makes the LED light up inside the opto. This makes the transistor in the opto conduct electricity, which pulls the IO pin down to ground. R2 makes sure it goes back up to 3v3 when the LED switches off. R1 limits the current through the LED. D1 means that if some dolt connects things the wrong way round, nothing bad happens. The fact the link is activated by light and there's no direct electrical connection means that, if things go bad from the 20v end, the 4n25 might die but your Pi will live on. – John U Oct 4 '13 at 16:29
• I like the idea of isolation, it's very comforting. So I understand, this circuit will effectively invert the i/o, at the Pi, so when the input goes hi, at the Pi, it goes low. And I also have to supply an additional 5v to make this circuit happen, right? – nageeb Oct 4 '13 at 20:24
• @nageeb the 5v is just because John reused this schematic from some other post. Just use 3.3v instead. And yes, the input levels are reversed. Otherwise, you can connect the optocoupler's collector to 3.3v directly, and the emitter to ground through the resistor. Then connect the input to the emitter, like this i.stack.imgur.com/KTH7Z.png this would be a non-inverting input. – Passerby Oct 4 '13 at 21:34
• I like this solution, but it seems a bit overkill for my current requirements. If this project goes to production, I just might need to step the game up to something like this though. Thanks for your advice! – nageeb Oct 6 '13 at 16:17

While the optocoupler might be the best choice if you need isolation, other options are available.

A simple 3.3v Zener Diode and current limiting resistor is another one. Unlike a voltage divider, who's output depends on the ratio between resistors and input voltage (At 20v, a 10:1 divider is 2v out, but if 20v becomes 30v, it's 3v. This can be a problem with voltage spikes), a zener diode should always clamp a higher voltage down.

R1 should be about 10k or higher. This should limit current to 2mA or less.

• this is what rasp pi websites recommend as far as I can tell. It also seems that limiting the current to below 0.5mA is also a good idea and maybe an output resistor of a few hundred ohms will help too. It seems, from what I can tell that the RP is a fragile little thing but there doesn't appear to be manufacturers specs on it. – Andy aka Oct 4 '13 at 23:29
• @Andyaka broadcom doesn't like to release specs on their SOCs. Or anything really. – Passerby Oct 4 '13 at 23:32
• very odd - and I was thinking of getting one. – Andy aka Oct 4 '13 at 23:35
• @Andyaka not really. Broadcom only thinks their direct customers, oems, should have access to datasheets, and under NDA. Same reason broadcom routers are poorly if at all supported under dd-wrt or openwrt. – Passerby Oct 4 '13 at 23:40

I do this fairly regularly in the products I design.

simulate this circuit – Schematic created using CircuitLab

For your application, you'll want to use a Schottky diode to guarantee you get the logic low. If you want to be really safe, add a pair of clamping diodes.

• @ThePhoton That is why I said using a Schottky may have a slight advantage. I suggested it because I know it works. – Matt Young Oct 4 '13 at 22:09
• If you shorted the diode the lowest voltage seen at the input would be 1.65V due to the two 10k resistors halving the 3.3V rail. – Andy aka Oct 4 '13 at 22:12
• @Andyaka That would be because I drew it wrong, fixed. – Matt Young Oct 4 '13 at 22:18

For converting signal levels from 20V to 3.3V you don't need LM317. The simplest (and very good) schematic for such things is the following:

simulate this circuit – Schematic created using CircuitLab

This schematic has additional advantage that protects the 3.3V input from external influences, for example high negative or positive voltage peaks, static discharges, etc.

Depending on R1 power rating, the schematic can withstand input signals up to several hundreds volts for short time and several thousands volts static discharge without harm for the low voltage logic.

It works following way:

1. If the input voltage is higher than 3.3V+0.6V = approx 4V, the diode D1 will open and will clamp the voltage in the point 1 to approximately 4V

2. If the input voltage is lower than -0.6V the diode D2 will open and the voltage in point 1 will be clamped to approx -0.6V. If the signal never goes under zero, D2 is always closed and can be safely removed. (But then there will be no protection to negative input signals)

3. This way, the input voltage from 0..20V will be reguced to -0.6 .. +3.9V that is safe for all 3.3V CMOS devices. As long as this voltage is a little bit higher than the CMOS logic power voltage, some small current will flow through the input protection diodes of the CMOS ICs - it is determined by the difference between the forward voltage of the silicon diodes (D1 and D2) and the schottky protection diodes used by CMOS technology and the resistance of R2. This current will be approximately few mA. If this is too big, D1 and D2 can be changed to low current schottky diodes and this way making input current close to zero.

You can read more for the CMOS protection schematics in the following Fairchild application note.

Additional information: As long as some people think that the above schematic can cause the Raspberry Pi GPIO IO failure, I made some effort and found GPIO electrical specifications. According to this document, the input protection diodes are not schottky, but silicone. This way, the above schematic will work even with smaller input current than expected.

Additionally, I increased the value of R2 to 10k, in order to increase the reliability of the schematic (in price of the maximal speed).

Anyway, this is the last edit by myself to this answer.

• While good information for stepping down a power source, OP actually needs signal level converting. A Power regulating LDO or Switching Supply would not be a good or cheap solution here. – Passerby Oct 4 '13 at 8:11
• @Passerby: Sorry the word LM317 mislead me. It is fixed now. – johnfound Oct 4 '13 at 8:29
• The output signal will be +3.9V not 3.3V. – Andy aka Oct 4 '13 at 9:16
• @Andyaka - yes, of course. That is why R2 is there. (BTW, check the acceptable input voltages for 3.3V logic). – johnfound Oct 4 '13 at 9:21
• Maybe you can supply a link that describes this? – Andy aka Oct 4 '13 at 9:26