The problem I have is that when the 5V is turned on, I'm getting the correct reading of 2.53V, but when the battery is off, I'm getting a voltage reading of 2.1V, when I expected it to be 0V.
If the wiring exactly matches the schematic, then the transistor must have a little current flowing from C to E, even though nothing is connected to B. You could try connecting B to ground to see if it then gives you something closer to the expected value.
However, to me, your design seems unusual. It's not a method I've come across before.
When I want to measure the voltage of a battery with an Arduino I've done it like this:
The shown values of R1,R2 will give about 1/3 of the battery voltage at their junction. I usually include R3 to reduce the risk of damaging the Arduino, although in this case it's not going to do much. C1 is to reduce noise.
If it's an automotive environment, with an alternator charging the battery whilst the Arduino is being used, then more care will be needed with filtering / clamping voltage spikes from the alternator
[Edit I was writing this before I saw @sonofcy's post. I've not bothered looking at the 2n2222 data sheet to see what the leakage current from C to E would be with B not connected]
I guess you are right. I've tried a few hours ago to connect the C to ground, I don't think it made it work right, I'll try again and post the results.
Regarding your scheme, this is a concept I didn't think of.
Connecting the two batteries in series won't affect them charging or discharging each other?
How will this connection affect the voltage reading? won't I be measuring the sum of the two batteries combined? what if battery one is not constant voltage?
I don't understand why you think they are in series. Maybe I'm missing something.
If the 5V power to the Arduino isn't close to 5V then the measured value will be off by some amount. You could use the internal reference and adjust the value of R2,R3 to give a maximum of 1.1V on the analog input pin.
You might need to calibrate your sketch to allow for voltage reference errors. E.g. measure the battery voltage with a multimeter and at the same time print out the value calculated by your sketch on the serial monitor. Then adjust the multiplying factor in your code to give an answer close to the multimeter value.
It's good to do N analogReads(), sum the values, then divide by N to get a better average value.
I'm trying to measure the voltage of a battery when the ground is not the same as the ground used for the Arduino.
How do you suggest I can solve this?
Yes, @Dave_Lowther was right by stating it's an automotive application.
And the scheme I attached is only a very small part from a larger system.
This vehicle has 2 batteries, one used only as an auxiliary battery which is not connected to the main battery (at all time) because it is mainly used to power a fridge over night and I'm separating the two batteries (for years now) so in the morning, I'll have one good battery to start the car with.
It's not becoming measurable having same voltage of arduino.
You need common gnd or separate measurement circuit.
By the way, what battery gives 5V nominal?
I have a voltage regulator from the main battery which gives 5V, I don't know what nominal is...
I would like to measure the voltage of the two batteries.
The first main battery which is used to run the car and the Arduino, is not a problem to measure as it has the same ground as the Arduino. The second battery which is mostly separated completely from the main battery is the challenge here.
I understand, I'm looking for the cheapest solution.
A simple solution might be to use a pro min connected to the second battery and connect it to the other arduino via serial port and optoisolators.
I think it will be an overkill for this application, it's ok if the two will share a common ground assuming no charging/discharging will be made through this connection and that when the power is off this connection is disconnected.
I thought my scheme would achieve this behavior but I was wrong.
A second option I had in mind was to use a relay instead of the transistor, but I thought the transistor will be both smaller and nicer as a solution.
Just to be clear, the two batteries I have both are automotive type (12.6V) and when I'm charging them (while driving) I'm connecting them in parallel for over 15 years now using a toggle switch (one alternator) without any problems.