Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Best way to get 12V input into 5V uC

Status
Not open for further replies.

iso9001

New Member
Hello,

I have an input thats going to come on every so often and need to get it into the pic.

I know there are 100 ways to do this but the problem is I can't change the line at all. There are other circuits also monitoring it. I tried running it into a resistor and then a reverse zener diode but obviously thats a bad idea.

My thought was to use an N-Ch Mosfet, tie 5V to the source, the 12V wire to the gate and watch the Drain at my PIC.

But maybe someone has a better idea ?

Thanks
 
There's no problem with a zener. Just use a 5.1v zener and a 10k resistor or whatever.

The MOSFET's not only a somewhat expensive component, but you may need to protect that gate. If this is an automotive system, that 12v line can spike pretty high and gates are extremely susceptible to damage from exceeding their vgs-max for even a brief moment.
 
I see what your saying about the mosfet. But then again its sourced from an other circuit, not direct from the battery. Even gets its own ground/return wire. I'm betting its relatively clear of spikes. (New Car)

The problem is that right now I have

Signal > 10K > Diode > Reverse Zener to Ground > PIC

I thought that would have been fine. But when thats in place the system does not work correctly. I haven't put the system on the scope yet, but I'm 99% certain its just a plain jane 12V feed.
 
iso9001 said:
I see what your saying about the mosfet. But then again its sourced from an other circuit, not direct from the battery. Even gets its own ground/return wire. I'm betting its relatively clear of spikes. (New Car)

The problem is that right now I have

Signal > 10K > Diode > Reverse Zener to Ground > PIC

I thought that would have been fine. But when thats in place the system does not work correctly. I haven't put the system on the scope yet, but I'm 99% certain its just a plain jane 12V feed.

A resistor it generally all you need, but a car is a particularly nasty environment, so it's best to add other bits as well.

But I'm rather confused by your text description?, why the diode?, and why a 'reverse' zener?, that will surely prevent the PIC input ever going high?.
 
Yea, that was confusing so I made this:

This signal is an ignition signal, but it comes from either a micro or a buffereed source (not directly from battery, I don't know which since I haven't cracked it open yet)

With this setup in place the system would not work correctly. Oh, and I wrote 1k, but I've tried it with 10k too.
 

Attachments

  • Untitled-1.gif
    Untitled-1.gif
    2.4 KB · Views: 522
You don't need the diode on the left. It would mean signal can pull the pin high, but nothing pulls it low again. While the signal is low, the pin is floating and can go high or low unpredictably.
 
I did that so there would be no change of me pulling this low. But I see what you mean.

Still I'm nt sure why but it doesn't work when this is all in place. The other systems monitoring this never shut down or start up correctly. It doesn't make sense to me, but I know it wont effect anything being tied to a mosfet gate.
 
A PIC input IS a mosfet gate. It draws no current but does have an internal protection shunt diode that will conduct current from pin to Vdd pin when pin voltage is > (Vdd + about 0.3v). The zener will keep voltage from getting that high. The resistor will limit the current draw on the zener.

The series diode is wrong. The resistor already limits how much current can be pulled off the input. At 12v input, 10k resistance, 0.7mA.

The diode would come into play if you accidentally configured the pin as an output and the load elsewhere on that wire were very, very small. In the worst case the wire could be pulled up to 5v. But that's just not going to happen because the wire won't be that high impedance, and you're not going to configure that pin as an output.

For REAL isolation you can add an optoisolator. But there is no need for this unless the PIC and input signal use separate grounds.
 
A simple voltage divider should suffice...

Drop all that booshwah. All you need is two resistors. One goes to ground on one end and the other end goes to the PIC pin and one end of the other resistor. The other lead on the second resistor goes to your ignition signal.

12V ignition--resistor--< PIC input >--resistor--GND

The PIC pin is now at the output of a voltage divider. When the ig-signal is low, its low at the PIC. When it goes to 12V (or higher?) the divider's output is 4 - 5V, if you use the proper resistor values. The grounded resistor can be 4.3k Ohms, the other resistor can be 8.2k Ohm. These are common, easilly obtainable values. You can play with these values A LITTLE, but not much. These values will give you 0.344 of the value of the ignition input at the PIC pin, give or take the resistor percentage of error, usually 5%. 12V in means 4.12V at the PIC pin, easilly within its specs for a high signal. In fact the PIC should work down to 3V, so your input can be as low as 8.72V and still work! Conversely, the PIC input can be as high as 5.6V (if your Vcc is 5V), so your ignition can be as high as 16V! You get quite a range to work within. Also, you won't pull more than a milliAmp while doing this, so you can use 1/4 Watt resistors.

And that should do it.
kenjj
 
Last edited:
My solution is to use a comparator such as an LM339(quad), LM393(dual) or similar device. There is probably a single comparator in a SOT23-5 package but I just don't know the part number. You can set the threshold with a resistive divider, and you can have it invert or not as you wish. You can use a 100K resistor to limit the current into the device. You need to make sure that at least one of the inputs remains in the common mode range. This won't be a problem if you pick 6V as the threshold. The best part is that the output is open collector so it can got straight to the PIC input, provided you can enable the internal pullup resistor. If not, the extra resistor to +5V or +3.3V is not a big deal.

We use these parts on 0-24VDC inputs all the time, with a threshold of 16VDC. The other, nearly obsolete part, you can use is a 26LS32 Line Receiver. These parts are handy if you want to hook a bunch of inputs to a porcessor data bus, because they have a tri-state enable.
 
kjennejohn said:
Drop all that booshwah. All you need is two resistors. .... can be 4.3k Ohms, the other resistor can be 8.2k Ohm. These are common, easilly obtainable values.

The OP said he tried and it does not work with 10K input resistor. Why would your suggestion works in this case?

iso9001 said:
The problem is that right now I have

Signal > 10K > Diode > Reverse Zener to Ground > PIC

I thought that would have been fine. But when thats in place the system does not work correctly
 
Somehow the way the other circuits work is getting screwed up w/ my resistor / diode / zener.

The diode was exactly for the case the pic went crazy and went low. About two months ago I would have said this could never happen.... It did happen to me tho, one of the pins on my board got shorted to ground and caused all sorts of fuss before a fuse finally blew. That diode may look dumb, but I'll take the .7V drop on the line for the peace of mind at least while in development and I don't have a proper case.

That straight resistor voltage drop won't work.

Comparator sounds fine except that I have no use for comparators anywhere else on the board and this is an optional portion anyway. I'd really like to make it work, but its not needed.

I agree, since they are all the same ground no need for an opto.

What I can't figure out is how the hell its screwing up the other devices. At the most with the R/D/Z thing I have they could 'possibly' detect a little more current draw, but thats it. Its very strange. I know that with that diode there it doesn't even matter if I have the pic set to input, low, high, float.

I'll have o pull out the scope and see exactly whats going on. I'm stumped.
 
If I had to debug this issue, I'd look for a couple things:

1) what is the impedance of the 12V signal? - if it is in the KOhm, multi KOhm range, that'll change the possible solutions.

2) what is the "low" value. The single resistor solutions will all keep the PIC's threshold voltage - ~1.2V (ttl thresolds ). Considering the environment, I wouldn't be too surprised if the "ground" level isn't bouncing around. Using a resistor divider would at least let you scale this threshold up without adding any more active parts.
 
iso9001 said:
Somehow the way the other circuits work is getting screwed up w/ my resistor / diode / zener.

The diode was exactly for the case the pic went crazy and went low. About two months ago I would have said this could never happen.... It did happen to me tho, one of the pins on my board got shorted to ground and caused all sorts of fuss before a fuse finally blew. That diode may look dumb, but I'll take the .7V drop on the line for the peace of mind at least while in development and I don't have a proper case.

The diode doesn't do anything?, even if a short occurred, and the resistor would prevent and short upsetting anything anyway - although it should probably be higher, 10K or 22K should be fine.

That straight resistor voltage drop won't work.

If a straight resistor won't work then you have something badly wrong!, either in your PIC program, or the source of the signal - it's probably NOT a 12V logic signal, and could be anything?.

What I can't figure out is how the hell its screwing up the other devices. At the most with the R/D/Z thing I have they could 'possibly' detect a little more current draw, but thats it. Its very strange. I know that with that diode there it doesn't even matter if I have the pic set to input, low, high, float.

Yes it does!, with the PIC pin as an output, and set low, you're pulling the signal low via a 1K resistor and a forward biased diode - like I said before, the diode is pointless - make the resistor larger and throw the diode away!.

I'll have o pull out the scope and see exactly whats going on. I'm stumped.

Yes, scope the signal, find out what it is - I suspect it's NOT what you thought?.
 
hi,

All i can,t understand ur conversation correctly. But the diodes connected in series would normally drop the voltage to their cutoff voltage ie it will reduce input voltage by 0.6 V in case of silicon and 0.3 V in case of germanium. Reverse 5.1V zener diode will give 5.1 V exactly and U can 7805 regulators and an germanium diode in series as an alternate.

If the PIC cannot drive ur device then definitely there should be a mistake in ur program or it happened during burning the hex file into pic.
 
Well.... I got it on the scope and really should have checked this before I made the first boards, but it looks like its a damn multiplexed 5V signal. All my diagrams tell me its 12V/0V, but I come to find out that its indeed a 5V resistor ladder. Feel pretty damn dumb there, than again this is a rather large project for one person... I'll let myself off easy this time.

Well.... crap. That actually still doesn't explain (to me) why it gets screwed up with my former protection circuit. Unless its acting as a sort of pull-down for the whole thing.

This is at least easy to fix now. Tie it into an ADC PIN and away i go. I know, for protection I could use a rail-to-rail opamp in voltage follower mode, thats adding some cost and pcb real estate... but its not very likely tapping into this will cause a failure (pretty low amperage) and I just make sure not to ever drive it high or low.

At least I have some ideas now regarding when I do need to get 12V in to a 5V micro.
 
Last edited:
If you get a very cheap optocoupler (.25usd) you can run 12-24 volts into the LED, just use something like a 470- 560 ohm resistor before the LED. The transistor side usually will only have 2 pins on a quad opto IC( collector = voltage in, emitter = voltahe outwhen switched on), so run 5 volts into the collector, the output will be 5 volts, and will be completely isolated from the source as light is turning on the transistor, not a wire. Conversly, to get 12 volts from 5 for example, use the reverse, 5v goes into the LED, 12v is on the transirstor collector, the output is 12v whent he LED is on. This is a great method where isolation is required to prevent damage. There are different speeds of opto's, so if using high speed signals get a fast opto.

If you need the output of the opto to be 5v/0v logic, you must tie the output of the opto to GND with a 10k, otherwise it will float and you will have logic problems.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top