How to calculate the constant used for analogRead()

Hi,

Basically I'm trying to use the analogRead() function to monitor voltage usage for a voltage divider set up. In this documentation (Battery Voltage - Photon 2 - Battery voltage | Reference | Particle), there is the constant 0.0011224 multiplied to the function, and it says that it calculated this based on two resistors (R1 = 806K, R2 = 2M) that lower the 3.6V LiPo battery output to a value that can be read by the ADC.

My question is: How did they calculate this constant? I am also trying to use two resistors to lower a 5V output to a 3.3V or less.

Thank you.

Hi and welcome to the community!
A voltage divider calculator (there are many online) can help you:

Best

Thank you. Yeah, I used one of those to determine that my R1 = 1.125 megaohms and R2 = 2 megaohms. However, I'm trying to figure out where Particle got the 0.0011224 value from

Normally you see the formula as

Vout = (Vin R2) / (R1 + R2)

but you can flip it around so it is:

Vin = ((Vout) (R1 + R2)) / R2

Using this formula, to get 3.3V maximum at Vout:

Vin = ((3.3)(806000 + 2000000)) / 2000000 = 4.6299

This means that at full ADC input of 3.3V, the Vin (high side of the voltage divider) must be 4.6299V. This corresponds to a value of 4095 from analogRead.

4.6299 / 4095 = 0.00113062

That's not exactly the same as the value in the docs, but it's close and I'm not sure why it's different.

1 Like

Hi,

Thank you so much. This is exactly what I needed clarification on.

Best wishes!

El El mar, abr. 9, 2024 a la(s) 2:49 p.m., Rick K via Particle <notifications@particle.discoursemail.com> escribió: