# how to just use a specifed section of a analog value?

Hi hi!

this problem of mine is probably easy to solve, but im new to this so i need some guidance. The thing im trying to do is that i just wanna use a specific part of the analog value that a temperature sensor is giving me. EX.

the the temp sensor gives me a 0 -1023 raw value and that's equal 0 = -40 degrees and 1023 = +125 degrees. the thing is that i have no use for measuring up to 125 degrees.

i just wanna use -40 degrees to +40 degrees and i want it to be 0 = -40 degrees and 1023 = +40 degrees.

hoping that you guys out there can help me with this.. Thx Have a look at the map() and constrain() functions.

The value that the temperature sensor returns is based on it's design, not your desires. Mentioning something about the temperature sensor would be useful.

You can't change the range of values that analogRead() returns to mean something else. If the goal is more accuracy, a different sensor is needed - one that is designed for the ranges you are interested in.

the sensor im testing is a normal tmp36.

im testing the use of constrain()

map() and constrain() will not improve the accuracy of a tmp36.

i just wanna use -40 degrees to +40 degrees and i want it to be 0 = -40 degrees and 1023 = +40 degrees.

Improve the accuracy no, but do what the OP said, yes. He may have meant that he wanted greater accuracy in the -40 to +40 range but that is not what he wrote.

0…1023 == -40…+125 that is in formula -40 + 165*value/1023 °C ; meaning +40 == 496 on the analog scale.

in code you want to map 496 on 1023

val = map(analogRead(A0), 0, 496, 0, 1023); // notice that not all steps between 0…1023 will be used as there are only 497 steps.

as the mapping has 2 minima == 0 you can create a simpler formula

val = analogRead(A0) * 1023L/496; // the L prevents overflow.

that was exactly what i was looking for super thx i will try this out thx again

WHat you also can do is adjust the analogReference, make it extern and connect it to 3.3Volt (Arduino has a 3.3V pin).

with 5Volt default the 40°C max is approx 500 in analogRead ~~ 2.5Volt.

With 3.3Volt as the max, implies analogRead() will give about 0 for -40C and 775 for 40C, so that makes 775 steps for 80C so steps of almost 0.1 C.

If you use a voltage divider (5V -> 2x2.5) and connect AREF to 2.5Volt, you can get even the full 1023 steps for the 80C giving steps of about 0.08 C