If I connect a 12V / 12V+ battery to the arduino (uno) analog, will it be damaged ? What if I connect 230-240V to it ? Don't worry I haven't tried it yet. :D
Thanks in advance.
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It only takes a minute to sign up.Sign up to join this community
Yes, it will be damaged. Especially if you connect mains direct to it.
The Arduino is designed to take up to 5V on any of its IO pins. Any more than that is asking for trouble. A slight amount above (as in say 5.5V) is usually tolerated OK, but anything more than that WILL cause irreparable damage.
And with mains, maybe even death.
For low DC voltages, like +12V, you can use a voltage divider to reduce the voltage to a 0-5V range.
For mains you will need to first reduce it down massively to a safe level (for example through a transformer) then re-scale and adjust it from there. Note that mains is AC not DC so it has a negative value of voltage as well, so you will either need to rectify and smooth it to create a DC voltage or, if you are interested in the waveform itself, reduce the voltage even more and add a DC offset into it to move the mid-point of the AC waveform up to around 2.5V.
For the 12 V battery, let's assume it might get up to 15 to 20 V when charging, so we need a voltage divider to divide that down.
The zener diode is there to clamp the input to no more than 5.1 V in case of spikes or other damaging things on the input.
The voltage divider will divide down to 25% of the input, ie.
output = R2 / (R1 + R2) = 10 / (30 + 10) = 0.25
So now if you measure 5 V on the analog input you know you have 20 V on the battery. Thus you can scale your calculations appropriately.
(A more common value would be a 33 k resistor, in which case redo the calculation and scale accordingly).
For 240 V I strongly advise against trying something like that with bigger resistors. For one thing, 240 V RMS will have 240 * sqrt (2) = 340 V peak to peak. And that is both negative and positive.
A suitable approach would be to run through a small transformer, which will reduce the voltage and also isolate you from (almost) certain death.
In addition to that you would need to rectify the resulting voltage (say, 12 V) so that you don't put negative voltages into your input pin. And on top of that filter it so smooth out the peaks and troughs.