## 12v on Pic input

This is the place for any magazine-related discussions that don't fit in any of the column discussion boards below.
Newz2000
Posts: 507
Joined: Wed May 18, 2005 1:01 am
Location: Des Moines, Iowa, USA
Contact:

### 12v on Pic input

Yes, I've read around and it seems there are as many opinions on the subject as their are search results in Google.<p>So, I have three inputs on the pic 12f675. Two of them will be from the car's 12+v system. (one will detect when the headlights are on, the other will detect when a door opens)<p>Here's what I've come up with:
• put a resitor in series with the pin. Benefit: low parts count, cheap. Problem: How do you decide the value of the resistor? If I knew what the resistence of the PIC was I could figure out how many ohms would be needed to drop the voltage from 12+ to 3-5v, but I don't know that.
• use a zener. I have a 5.1v zener I can put in there. Benefit: low parts count. Problem: I've never used a zener like this before. Do I need to worry about current?
• switch transistor. Benefit: I know it will work. Problem: two or three parts (transistor + 1 or 2 resistors) using 5 - 7 pins.
<p>This particular mCu doesn't have clamping diodes, but if it did, should I just run 12v straight into the pin and let the clamping diodes take care of it?<p>What would you do?

josmith
Posts: 340
Joined: Tue Dec 04, 2001 1:01 am
Contact:

### Re: 12v on Pic input

use an opto isolator so that the chip only sees voltage from it's own supply.

Chris Smith
Posts: 4325
Joined: Tue Dec 04, 2001 1:01 am
Location: Bieber Ca.

### Re: 12v on Pic input

Actually you need a voltage divider. <p>If you need 2 volts signal from a 12 volt source, then its a 6:1 ratio. <p>If its 6 volts, then its 50:50.<p>If you take a 1000 ohm resistor, add it to a 5000 ohm resistor, send the power through these resistors to ground and tap off the center, the voltage will be 6:1 or 2 volts at the divide, depending on which resistor leads, and which one lags. <p>The current flow available is the only thing affected by the values of the actual resistor as long as they remain at the same ratio. <p>The signal and the voltage will remain the same at the center tap, while only the current available [ and over all draw] will change at the tap, regardless of any combination of resistance values, as long as the Ratio remains the same. <p>This remains true at 5 mega ohms to one mega, 500,000 ohms to 100k, or 50,000 ohms to 10k, etc., as long as the ratio remains the same.

grahamg
Posts: 1
Joined: Fri Oct 21, 2005 1:01 am
Location: Johannesburg
Contact:

### Re: 12v on Pic input

Hello Matt,<p>Application note AN521 from Microchip titled "Interfacing to AC power lines" states that you only need to limit the input current to the pic's input as the input protection diodes in the chip will keep the input voltage within correct limits. Any resistor between the value of 100K - 270K should work fine. No other components required. If you are using GP3 as one of your inputs it only has a protection diode to VSS(GND) and an external diode will then be needed from this pin to VDD.<p>Regards, Graham

Gorgon
Posts: 325
Joined: Wed May 04, 2005 1:01 am
Location: Norway
Contact:

### Re: 12v on Pic input

<blockquote><font size="1" face="Verdana, Helvetica, sans-serif">quote:</font><hr>Originally posted by Matt Nuzum:

Here's what I've come up with:
• put a resitor in series with the pin. Benefit: low parts count, cheap. Problem: How do you decide the value of the resistor? If I knew what the resistence of the PIC was I could figure out how many ohms would be needed to drop the voltage from 12+ to 3-5v, but I don't know that.
• use a zener. I have a 5.1v zener I can put in there. Benefit: low parts count. Problem: I've never used a zener like this before. Do I need to worry about current?
• switch transistor. Benefit: I know it will work. Problem: two or three parts (transistor + 1 or 2 resistors) using 5 - 7 pins.
<p>This particular mCu doesn't have clamping diodes, but if it did, should I just run 12v straight into the pin and let the clamping diodes take care of it?<p>What would you do?<hr></blockquote><p>Hi Matt,
In your case I would have used a resistor from the 12v to a zener diode (5,1V) in parallel with a resistor, connected between the input and 0V. The input resistor is needed to reduce the current in the zener. (if you don't use it you'll get smoke from the zener)<p>The zener diode will protect the input from excessive voltage both + and -. When designing I normally don't use the internal protection in controllers for this. It is cheaper to change a shorted zener than a blown controller.<p>The parallel high value resistor is returning the input to 0v(off) when the input signal is gone. For a hiZ input the capacitance in the zener is sufficient to keep the input on, if left floating.<p>TOK
Gorgon the Caretaker - Character in a childrens TV-show from 1968.

philba
Posts: 2050
Joined: Tue Nov 30, 2004 1:01 am
Location: Seattle
Contact:

### Re: 12v on Pic input

in non-automotive cases, I'd use the voltage divider that chris explained. the assumption is that voltage reversals are unlikely.<p>For automotive cases, a divider won't protect against reversal so gorgon's is the preferred solution.<p>yes, the PIC has clamping diodes that protect the inputs but I'd prefer not to have them be the first line of defense.<p>On the direct question of how big to make a current limiter, pick a reasonably low current value - say 1 mA and go from there. Most micros can handle very small input currents (cmos) so you can be uber careful here. Don't forget to use the max V you can expect (40-ish in a car).<p>edit: I looked at the 675's port block diagrams on pps 22-24 and only GP3 is missing the clamping diode to Vdd - it does clamp to Vss. The rest of them clamp to Vss and Vdd. I think it may have to do with HV programming (its the reset pin also) - you definitely want to avoid HV on that pin as it will put the chip into programming mode. This, of course, also means you need to be careful how you limit voltage to that pin if you want to use ICSP.<p>[ October 21, 2005: Message edited by: philba ]</p>

Newz2000
Posts: 507
Joined: Wed May 18, 2005 1:01 am
Location: Des Moines, Iowa, USA
Contact:

### Re: 12v on Pic input

Is this what you're describing?<p><p>And how do I calculate the resistors? Is it:
(12v - 5v)/.001 = ~6.8Kohm resistor to drop 7v? Wait, I think you're suggesting I drop more than 7v since there are wide fluxuations in voltage, so maybe:
(12v - 3v)/.001 = ~9Kohm (or likely a 10Kohm in this case)

philba
Posts: 2050
Joined: Tue Nov 30, 2004 1:01 am
Location: Seattle
Contact:

### Re: 12v on Pic input

I think your diagram would always have the pin at 0. <p>I'm no expert but have seen something about 40V being possible if the battery drops out when the alt is running. Maybe use a 12V zener and then divide off of that to get to 5V. R2/(R1+R2) = 5/12 Note that with a 5V supply the PIC will see any input over 2V as high.<p>an opto may just be a better approach.<p>[ October 21, 2005: Message edited by: philba ]</p>

Newz2000
Posts: 507
Joined: Wed May 18, 2005 1:01 am
Location: Des Moines, Iowa, USA
Contact:

### Re: 12v on Pic input

I'll bet this is what Gorgon was talking about:
<p>Only instead 4.7k for R2 it will need to be higher.<p>In this car there is a regulator that will keep the v under control even if the battery goes dead. (No alternator either)<p>I've got an optoisolater but it's a fancy 8 port one I bought for using on a parallel port, and I don't really want to use it for this. I may look through my parts pile to see if there's one there I can use, but I'm likely going to try this diagram tonight, unless someone speaks up with some additional wisdom.<p>Good to know 2v will be detected as high. I wondered how low you could go.<p>Here's my schematic (minus the parts in question):
<p>It doesn't look like much, but unlike most of the stuff I've done, this is 100% from scratch. Usually I start with someone else's stuff and modify it.<p>I've looking for a way to make the speaker louder. I'm just sending it a square wave. Choosing the right freq helped and connecting it to 12v helped too. It's a standard 16ohm mini speaker.<p>Any feedback appreciated.

philba
Posts: 2050
Joined: Tue Nov 30, 2004 1:01 am
Location: Seattle
Contact:

### Re: 12v on Pic input

I don't think you need to use 12V for the switches. Just a 5V pull up (to the PICs Vdd). No point in letting the 12V in if you don't need to.<p>You can get optoisolators really cheap from All and Goldmine. Last time I ordered from All, they had them for like a dime per so I got a bunch.<p>[ October 21, 2005: Message edited by: philba ]</p>

Newz2000
Posts: 507
Joined: Wed May 18, 2005 1:01 am
Location: Des Moines, Iowa, USA
Contact:

### Re: 12v on Pic input

<blockquote><font size="1" face="Verdana, Helvetica, sans-serif">quote:</font><hr>Originally posted by philba:
I don't think you need to use 12V for the switches. Just a 5V pull up (to the PICs Vdd). No point in letting the 12V in if you don't need to.<hr></blockquote><p>The switches are built into the car. I'm hooking one input to the door switch and the other to the headlight switch.<p>I can't mail order an optoisolator because I'm hoping to finish this over the weekend.<p>I'll try the zener aproach and if I have troubles I'll just use another switching transistor.

Gorgon
Posts: 325
Joined: Wed May 04, 2005 1:01 am
Location: Norway
Contact:

### Re: 12v on Pic input

<blockquote><font size="1" face="Verdana, Helvetica, sans-serif">quote:</font><hr>Originally posted by Matt Nuzum:
Is this what you're describing?<p><a href="http://imageshack.us" target="_blank">;)
Gorgon the Caretaker - Character in a childrens TV-show from 1968.

Gorgon
Posts: 325
Joined: Wed May 04, 2005 1:01 am
Location: Norway
Contact:

### Re: 12v on Pic input

<blockquote><font size="1" face="Verdana, Helvetica, sans-serif">quote:</font><hr>Originally posted by Matt Nuzum:
I've looking for a way to make the speaker louder. I'm just sending it a square wave. Choosing the right freq helped and connecting it to 12v helped too. It's a standard 16ohm mini speaker.<p>Any feedback appreciated.<hr></blockquote><p>Hi Matt,
Since you didn't write the value of the base resistor for the speaker transistor, I just wanted to make you aware of the need for current into the base. The hFE of the 2N2222 is not very high, so a resistor of 100 ohm should be used. This is the max current of what the PIC can output, and very important, set the output to low (0v) when not sounding!!<p>TOK
Gorgon the Caretaker - Character in a childrens TV-show from 1968.

Newz2000
Posts: 507
Joined: Wed May 18, 2005 1:01 am
Location: Des Moines, Iowa, USA
Contact:

### Re: 12v on Pic input

<blockquote><font size="1" face="Verdana, Helvetica, sans-serif">quote:</font><hr>Originally posted by Gorgon:
<p>Hi Matt,
Since you didn't write the value of the base resistor for the speaker transistor, I just wanted to make you aware of the need for current into the base. The hFE of the 2N2222 is not very high, so a resistor of 100 ohm should be used. This is the max current of what the PIC can output, and very important, set the output to low (0v) when not sounding!!<p>TOK
<hr></blockquote><p>
Hi, I think I'm using 2.2k. Can you tell me how you came up with that value or point me to a link on the web?<p>As I've mentioned, this is the first circuit like this I've built from the ground up. Choosing transistors still seems a little foreign to me.

ecerfoglio
Posts: 108
Joined: Thu Nov 25, 2004 1:01 am
Location: Buenos Aires Argentina
Contact:

### Re: 12v on Pic input

<blockquote><font size="1" face="Verdana, Helvetica, sans-serif">quote:</font><hr> Originally posted by Gorgon:
If you move the PIC input to the top of R2, its the correct decoding of what I wrote. R1 = 6.8 - 10k is ok, and R2 = 22 - 47k should be ok. Use a 1W zener and you can drop loads of overvoltage (depending on the wattage of R1).<hr></blockquote><p>If you don´t have a 5v zenner at hand, you can replace it with a pair of "garden variety" diodes (1N4001 to 4007 or 1N4148 / 1N914). <p>The first diode goes between ground and the R1/R2/pic input junction (like the zenner did)<p>Just conect the second diode´s anode to the (R1/R2/1st diode/pic input) junction and it's catode to the pic´s power suply (+ 5 V?)<p>This way the pic's input pin doesn't swing outside the pic's power suply pins - even if the pic is not energized.
E. Cerfoglio
Buenos Aires
Argentina

### Who is online

Users browsing this forum: Majestic-12 [Bot] and 168 guests