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.
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?
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
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.
I am sill trying to get more information on the programming language so I can be more informed and do more.
Ken
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:
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:
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.
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.