Simple Voltage reader using voltage divisor

Hi, I'm working in a little car project, I have several sensors attached and I'm doing something really dumb or don't know what could be, I always get a 1023 reading from an analog input from the voltage divider, the simplified circuit goes like this (dont mind the values/components orientation):

I wanna feed the circuit with 13-14V, get that value from the voltage divider, also with the same circuit feed my arduino using a regulator (and all the cap/protection/decoupling), but always got 1023, I test the voltage at the arduino analog input and the voltage there is the one that should be because of the voltage divider (12v is about 4.2v) without the input pin connected I get 0.

#define PIN_VOLT 2 

void setup()   {
   Serial.begin(9600);

   pinMode(PIN_VOLT,INPUT);

}


void loop() {  

    float Voltage = (float(Voltage1(analogRead(PIN_VOLT)))); 
    Serial.print(Voltage); 

}


const float R1 = 1901;
const float R2 = 1996;
const float resistorFactor = 1023.0 / (R2/(R1 + R2));

float Voltage1(float RawADC)
{
  float Volts = (RawADC/resistorFactor)*5.0;  

  
  return Volts;
}

Without going into it in depth, I am wondering why you are referring to the resistor values in the program.

The voltage division is done by hardware. You simply need to read the analogue voltage.

Assuming 12v in, resistances of say 10k and 6k8 would give a useable range to read the voltage across the 6k8. These values are preferred values.

If you want to look for a particular value for under or over voltage, simply use a variable supply to feed the circuit and record the ADC reading at that voltage to trigger your response.

Weedpharma

Sorry, that was my first sketch, I end up reading directly to the Analog Input value

V+---R1 - (AnalogInput3)- R2 -- GND

So I call the Voltage1 function that returns (bypassed): float Volts = RawADC;

From:

float Voltage = (float(Voltage1(analogRead(PIN_VOLT))));

I get 1023, if I measure that analog Pin I get the correct value according to the resistor, and its not 5V (1023).

I am confused. Are you saying that you get different result for ADC pins 1 and 2?

Weedpharma

take out : pinMode(PIN_VOLT,INPUT); from the setup

What's the output voltage of the voltage regulator? 5V? In this case you must feed into the 5V pin and not Vin because Vin is connect through a onboard voltage regulator and that cannot generate 5V out of 5v again (it has minimally 1.5V drop on it).

Yes, the voltage is a constant 5V, the connections are made like the picture, using Vin, so I would need to route that to the plug next to the USB or you really men the 5V pin (I'm using that to feed another sensor, pressure one)?

reses:
Yes, the voltage is a constant 5V, the connections are made like the picture, using Vin, so I would need to route that to the plug next to the USB or you really men the 5V pin (I'm using that to feed another sensor, pressure one)?

Yes, I mean feeding that directly to the 5V pin. If you provide, let's say 12V to the Vin pin, the Arduino is doing the same, using a linear voltage regulator to generate 5V and feeding that to the Vcc network on the board which is also connected to the 5V pin.

Thanks I did that, I was getting less than 5V because of the regulators, instead I replaced the regulator for a 12V one (anyway I was gonna need that).