[Solved] analogRead accuracy

I’m trying to read the value from a soil temperature sensor(found here: http://www.vegetronix.com/Products/THERM200/) using analogRead(), converting it to volts and then using the conversion to get the temperature:

double val = analogRead(pin);
double volts = val / 4096.0 * 3.3;

return (volts * 41.67) - 40;

The volts seem about 0.6V too low (@1.06V), so my output is 4.08 Celsius -which would kill my plants at that low of temperature. My volt-o-meter reads 1.6V which would put the temp around 27C… much closer to what I expect.

Am I doing something stupid?

Hi @tripzero

The top two lines are fine (except I think you should use 4095 instead of 4096 as it’s 0-4095 (4096 possible values).
Perhaps try returning just the voltage e.g.

 return volts;

If you’re getting the correct voltage, you’ll know it’s your conversion formula that’s wrong, if the voltage still isn’t 1.6V as you say it should be, then perhaps your sensor isn’t connected properly.

Do you power your sensor off Vin? (supply voltage 3.6V to 20V)

You might also want to measure the actual voltage of your 3V3 pin and insert this value in your volts mapping.

2 Likes

No, I power the sensor via 12V. I was powering it via 5V which I supply to VIN, but wondered if that was my problem so I rewired the sensor to get 12V. I’ll check the 3.3V pin.

UPDATE: looks like the 3.3V pin is 5V and using that in the equation fixes the results.

I did this. That’s why I know it’s low (1.06 vs 1.6).

The 3V3 pin is not supposed to show 5V! If it actually had 5V your Particle should be dead.
But if you happen to have a ground issue or your 12V is not perfectly filtered, this would also explain wrong readings.

To be certain, you’d need to give some more detail (maybe a photo) of your hardware setup.

1 Like