Go Down

### Topic: Project 3: Why dividing sensorVal by 1024 and multiplying offset by 100? (Read 3520 times)previous topic - next topic

#### cemooo

Hello everyone,

I recently bought the Arduino Starter Kit and it's one of the best things I've ever bought (together with LEGO EV3  ), but I have a few questions regarding the programming side.

Project 3:

Question 1.

Code: [Select]
`15 float voltage = (sensorVal/1024.0) * 5.0;`

I do not understand why is the math required. If I am correct sensorVal is an analog reading on the A0 pin, reading voltage sent by the temperature sensor. Why do we divide that value by 1024 and then multiply it with 5? What is the purpose of this and why are the numbers 1024 and 5 chosen?

Question 2.

Code: [Select]
`20 float temperature = (voltage - .5) * 100;`

Similar question. Why do use math in this line?

Thank you very much.

#### Raybrite

I am not certain but the book says the sensor will report a value of 0 to 1023. If you divide that by 1024 you will get a percentage that you can multiply to give you an output of 0 to 5 volts.
It is just converting the sensor output to a value that you can work with to control things.

You have to understand that we are trying to change ( convert) a heat range into a voltage and then use that to give us an indication of some kind. (That is what these transducers do.

I haven't looked at the second one yet. I just finished project number 1.
Hope this helps a bit to understand it.
Ken

#### cemooo

#2
##### Jun 01, 2015, 02:57 amLast Edit: Jun 01, 2015, 06:02 am by cemooo
Yes, but why not dividing by 204,8? Instead we divide by 1024 and then multiply with 5. I still don't get it.

#### Raybrite

That is done in many cases to help in seeing what the circuit is doing in case there are problems later.

#### Raybrite

I have downloaded the Data sheet and it says the offset voltage to get the sensor to read right is .5 volts which is where the .5 comes from in line 20. The voltage mentioned should be the one you found in step 15. Now you are converting that into a temperature in step 20.
These computations are made to have values to print out.
I should be on to this project in a day or two.
Ken

#### ctcarton

#5
##### Jun 04, 2015, 06:48 pmLast Edit: Jun 04, 2015, 06:48 pm by ctcarton
Code: [Select]
`20 float temperature = (voltage - .5) * 100;`
I think this is an error in the book. Apparently it has a lot of errors. According to the specs, the TMP37 generates 500mV at 25°C, and as the book mentions, varies by 10mV/°C. So based on that, the math should be:

Code: [Select]
`float temperature = 25 + (voltage - .5) * 100;`

So I think they just missed the 25.

However, in my kit I actually have the TMP36, which according to the specs generates 750mV at 25°C, so for me it should be:

Code: [Select]
`float temperature = 25 + (voltage - .75) * 100;`

You should check exactly which component you have. And anytime the book gives you magic numbers like this it's probably a good idea to double check them against the spec sheet yourself if you can. Although it's not really a big deal, it is more fun if the numbers are correct.

#### ctcarton

Code: [Select]
`15 float voltage = (sensorVal/1024.0) * 5.0;`

I do not understand why is the math required. If I am correct sensorVal is an analog reading on the A0 pin, reading voltage sent by the temperature sensor. Why do we divide that value by 1024 and then multiply it with 5? What is the purpose of this and why are the numbers 1024 and 5 chosen?

The number you read from the ADC A0 pin isn't the actual voltage value. The ADC divides it's analog input range of 0v - 5v into 1024 discrete output values. It gives you a number between 0 and 1023 where 0 means 0v and 1023 means 5v. So to calculate a value in between you take the reading and multiply by 5/1024.

Go Up