Reading 14v switch with 5v microcontroller

Thread Starter

stoopkid

Joined Mar 3, 2011
146
I have a power line that will switch between 0 and 14 volts. I want to read it as a digital input into an atmega328. Can I simply join the grounds of the 14v system and my atmega and use a resistor between the 14v line and my atmega input pin? If so, how do I calculate the resistor value and wattage? The only way I know how to calculate resistance for voltage drop is with LEDs but how do I know how much current to calculate for?

Thanks
 

Thread Starter

stoopkid

Joined Mar 3, 2011
146
Actually the atmega will be powered by a voltage regulator input from that system so they already share a ground. So a simple voltage divider is all I need, say 1k and 520 ohms? Is the wattage negligable?
 

t06afre

Joined May 11, 2009
5,934
The total current will 14/1520 or approx 9.2 mA. The wattage for each resistor is equal to U*I(voltage times ampere). We can substitute U with I*R. Hence the wattage for 1K resistor will be 0.084 Watt. And for the 520ohm resistor 0.044 watt. I assume you use hole mounted resistors. And they are able to handle 0.25 Watt. Ideally you do not design with values close to this limit. But in this case you are on the safe side
 

jwilk13

Joined Jun 15, 2011
228
If you want to make sure the 14V input and your microcontroller are electrically separated (not saying you do), you could use an optocoupler (optoisolator) like this one. If you don't care, a resistive divider will work just fine as t06afre suggested.
 

EB255GTX

Joined Apr 30, 2011
62
If you only want the digital value of your 14V input, i.e. 0V = low, 5V (or more) = high then you only need one resistor. A series resistor of high value will limit the current such that you will not damage the protection diodes in the micro. Try something like 470k.

With a divider, if you don't design it carefully you may end up with the digital input reading as low when the 14V rail is not right up at 14v - e.g. 12V might give you a logic low which is probably not what you want.
 

Thread Starter

stoopkid

Joined Mar 3, 2011
146
If I use a single resistor, do I need to be concerned about its wattage or will it be very low? I may have to worry about voltage fluxuation so that may be a better idea.
 

tpny

Joined May 6, 2012
220
what about putting a transistor between them. 0~14v through a resistor into the base to get 0~5v out from the emitter (Vcc = 5v).
 

Thread Starter

stoopkid

Joined Mar 3, 2011
146
Ok so using a transistor... Can I pull my pins high with a resistor and have an NPN pull the pins low to read from my microcontroller? Do I just set it up so that it will sink a miliamp or two?
 

EB255GTX

Joined Apr 30, 2011
62
Ok so using a transistor... Can I pull my pins high with a resistor and have an NPN pull the pins low to read from my microcontroller? Do I just set it up so that it will sink a miliamp or two?
One resistor will do exactly what you want, two is overkill and may provide an inferior solution, so now adding a transistor and more resistors seems a bit nuts to me....also note the transistor will invert your signal, so invert it back in software (or use the single resistor and you won't have to)
 

Thread Starter

stoopkid

Joined Mar 3, 2011
146
The transistor seems like a good idea to me because both of the resistor ideas risk damage to the micro controller if the voltage is unexpectedly high. I would imagine a transistor would be a bit more robust than the MC pin and the MC is only going to either be pulled up to 5v or down to ground.
 

tpny

Joined May 6, 2012
220
Since you mentioned your uC is 5V you cannot present 14V signal to it - which is what you are doing with "one resistor". Configure the transistor as a voltage follower so the signal will not be "inverted" - that is, connect uC to the emitter of the npn. Then set up your uC pin as high-impedence so that it becomes an input pin.
 

EB255GTX

Joined Apr 30, 2011
62
*shrug* you have several recommendations at this point, pick the one you are most comfortable with.

As a veteran of many commercial designs, I have a tendency to go for the simplest solution. You can't get simpler than one resistor, and it's good enough for atmel to recommend it for sensing mains directly so I'd trust it on 14v :)
 

EB255GTX

Joined Apr 30, 2011
62
Well for one thing you if the 14 volt source is in off state. The input will most probably be floating. And take high level. It also other aspect with this that is dodgy.
I can't really understand your wording, but I think you are talking about the power to the micro being off and the 14v supply being on. This situation is fine as long as the micro is powered by a properly regulated and protected power supply-a 3 terminal regulator is good.

Like I say above, Atmel are happy for you to connect mains to an input pin of thier micros using the single resistor method, and they should know what they are talking about....
 
Top