How to read Voltage through Arduino

Hi Everyone!
I'm facing a problem with reading a voltage through Arduino.

GOAL: I have to create a circuit which is able to read a voltage (less than 3V), provided by an external source and print it in Serial. I have also to disconnected/connected the external source from Arduino and I thought abount using a MOSFET type N or a RELAY controlled by a digital pin from Arduino.
I used the software thinkercad to practice and try to make it work, I used to simulate my external source a 1.5V battery.

Without the MOSFET or the RELAY connecting the positive pole of the battery to a analog pin (like A0 ) and the negative pole to the GND making the map from 0 to 1023 (10 bit resolution ADC) and from 0V to 5V I'm able to read correctly the Battery Voltage until now, so far so good.

PROBLEM: when I try to connect the MOSFET or the RELAY I have the same result. Without giving the voltage through the Gate-Source(using mosfet) or the coil(using relay) the Arduino reads in serial the Voltage.

Here a sketch of the circuit

Vit

Please post a proper schematic. That one is not useful. Also you talk about some MOSFET but it's not shown anywhere.

By the way, it's not a good idea to drive a relay coil directly from an Arduino GPIO pin.

What's the relay or MOSFET for?

If you are worried about draining the battery, the Arduino's inputs have nearly infinite resistance so almost no current flows and you aren't draining the battery (as long as the Arduino remains powered).

What same result? It works correctly?

Maybe this schematic could help. I will also attached the Arduino code

I talk about a MOSFET because I tried two types of circuit, one with the relay and one with a mosfet for the same aim disconnect the battery from the circuit. Maybe I was not that clear with the post so I will focus just with the circuit with the relay

float val2 = 0; float val = 0; float raw = 0;
int apin = A0;
int sec = 3000;
float R1 = 10000;
float R2 = 1000;

void setup()
{
  pinMode(13, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  digitalWrite(13, HIGH);

  raw = analogRead(apin);
  Serial.print("13 HIGH\n");
  Serial.print("Voltage raw (without map):");
  Serial.print(raw);
  Serial.print("\n");
  
  val2 = fmap(raw, 0, 1023, 0.0, 5.0);
  
  val = val2 / (R2/(R1+R2)); 
  
  Serial.print("Voltage (mapped):");
  Serial.print(val);
  Serial.println(); Serial.println();
  
  delay(sec);
  Serial.print("\n");
  Serial.print("\n");
  digitalWrite(13, LOW);

  raw = analogRead(apin);
  Serial.print("13 LOW\n");
  Serial.print("Voltage raw (without map):");
  Serial.print(raw);
  Serial.print("\n");
  
  val2 = fmap(raw, 0, 1023, 0.0, 5.0);
  
  val = val2 / (R2/(R1+R2)); 
  
  Serial.print("Voltage (mapped):");
  Serial.print(val);
  Serial.println(); Serial.println();
  
  delay(sec);
}


float fmap(float x,float in_m,float in_M,float out_m,float out_M)
	{
	return (x - in_m)*(out_M - out_m)/(in_M - in_m) + out_m;
}

What is the coil current specification for relay K1?

The way you have configured it, the relay doesn't conserve any battery power. The voltage divider is always connected to the battery so it's always draining it.

The only effect of disengaging the relay, is to make input A0 "float" disconnected, rendering the input value meaningless.

So, back to the drawing board...

I want to use the relay or the mosfet to disconnect the battery from the circuit when i don't want to read the voltage.
Thanks for your advice about the draining the battery, but I think I have to put the relay or the mosfet because the next step is to read not only one type of battery but at minimum 3 different batteries so I have also to think a way to read the voltage that I need in a certain moment. Other advices are appreciated

What same result? It works correctly?
The result is that even if I put digitalWrite(13, LOW); or digitalWrite(13, HIGH); the Arduino gives me the voltage of the battery

What is the coil current specification for relay K1?

40 mA

The way you have configured it, the relay doesn't conserve any battery power. The voltage divider is always connected to the battery so it's always draining it.

Yes, sorry I choose the wrong spot to the relay, but since my arduino should be always powered on I can choose to continously provide voltage through the pin 13 and do not draining the battery, it's not that efficient but a solution. Anyway I change the spot
immagine

I can't see what your clipped drawing shows. Please re-post the entire schematic.

Also, 40mA is too much current for an Arduino pin, you risk permanent damage to the pin. You have no flyback diode to absorb inductive kick back, as well.

but since my arduino should be always powered on I can choose to continously provide voltage through the pin 13 and do not draining the battery

this part, I can't understand what you are talking about...

Thanks for the advice , I will put it but since i'm working on the simulator I want to solve firstly the voltage reading to disconnect the battery from the circuit.

That one doesn't look any different. Anything unusual like the fact you are using a simulator, should be mentioned in the first post, not later on.

Arduinos have multiple analog inputs. You don't need to switch anything externally and you're not going to drain the battery. Your relay is likely to do more harm than good unless you're using a mercury-wetted reed relay and even then it's unnecessary. Power relays are not very good for switching low currents.

@vit_a_cuss,

On a standard Uno you have 6 analog inputs.
You keep mentioning disconnecting the battery? Do you need a switching circuit, a measuring circuit or both?
If one of the batteries you want to measure is higher than 5V then you will need a Voltage divider to measure it.
as @anon57585045 and @cedarlakeinstruments posted, a relay is not a good idea.
If you just want to measure the Voltage, as @DVDdoug posted the arduino pins have high resistance and you could even put a 100K resistor in series if you wanted to.

Connect a 33k resistor between A0 and GND so it's not "floating" when the relay contacts are open.

If the resistive divider is switched, unlike in the diagram, then additional resistors are not necessary. The divider output provides a resistive path to ground.

if its less then arduino 5V just tie it directly + to A0....... or over any resistance, no current, no V drop...
relay must be controlled over a transistor and the coil must have a reverse diode across it to choke coil transient
JCA34F is right, he could tie gnd to relay tho on the top right nc, would flicker a bit while relay closes.

2ccbfa029ec80d309ac1438f671a63c909ad0b1e_2_690x331
ofc I wouldnt do a real circuit quite like this, but it would probably work, and sry for the paint...
to me even this has more sense then relay just for measures, there should also be a forward diode on the BAT1+ going out to get any practicality as a low V cutout circuit out of this...

Is there more to this, or just learning sim?

The software is a simulator so I mentioned it before. Next time I will write it more clearly maybe in capital letters

I changed the connection of the positive of the battery from the spot NC to the NO spot since as you said ...

Thanks for the advice, so do you suggest to connect the external source (in this case the batteries) directly to the Arduino?
The positive to one of the analog input and the negative to the ground?

I'd prefer to have both

No the battery are less than 3V and I have to measure them separately

So are you suggesting to connect the batteries directly to the Arduino? Like this

My suggestion was to run the relay contacts between the battery and the resistive divider.

Are you ever building a physical circuit? If so, you'll have to deal with the hardware limitations I mentioned.

Running the battery directly into an analog pin, means when the Arduino is turned off, the battery will supply current into the pin via the internal clamping diodes.


y seems about right, but resistive divider is only good for overvoltage protection in this case, and should be on the arduino side like you said, to not let analog input float, or at least a pulldown, else he might get readings all over the place when the relay disconects, if not it can be coded to stop reading when relay is off
You could also use only a large (100k) series resistor,on the analog lines, without relay, to eliminate current going thru clamping diodes when arduino is off...
It would be good to get a better perspective on what you are trying to achieve, are you designing a real circuit?