# Reading 14v switch with 5v microcontroller

Discussion in 'General Electronics Chat' started by stoopkid, May 25, 2012.

1. ### stoopkid Thread Starter Member

Mar 3, 2011
136
1
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

2. ### t06afre AAC Fanatic!

May 11, 2009
5,939
1,222
You must build build a restive divider. With two resistors. Take a look here http://en.wikipedia.org/wiki/Voltage_divider It would probably be OK to connect the two grounds together. BUT I can not say for sure without knowing more about the setup.

3. ### stoopkid Thread Starter Member

Mar 3, 2011
136
1
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?

4. ### t06afre AAC Fanatic!

May 11, 2009
5,939
1,222
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

5. ### jwilk13 Member

Jun 15, 2011
228
12
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.

6. ### EB255GTX Active Member

Apr 30, 2011
54
2
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.

7. ### stoopkid Thread Starter Member

Mar 3, 2011
136
1
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.

8. ### tpny Member

May 6, 2012
216
0
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).

9. ### t06afre AAC Fanatic!

May 11, 2009
5,939
1,222
Using one resistor is a BAD design idea. Do not use this approach.

10. ### EB255GTX Active Member

Apr 30, 2011
54
2
Why?

say you use 470k - the power will be ~400uW. The smallest leaded resistor you have will be fine

11. ### stoopkid Thread Starter Member

Mar 3, 2011
136
1
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?

12. ### EB255GTX Active Member

Apr 30, 2011
54
2
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)

13. ### stoopkid Thread Starter Member

Mar 3, 2011
136
1
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.

14. ### tpny Member

May 6, 2012
216
0
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.

15. ### EB255GTX Active Member

Apr 30, 2011
54
2
*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

16. ### t06afre AAC Fanatic!

May 11, 2009
5,939
1,222
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.

17. ### EB255GTX Active Member

Apr 30, 2011
54
2
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....