Tutorial 6
Explanation:
The temperature sensor is integrated in the MSP 430 chip and it's ready for us to use. Essentially an analog temperature sensor converts the ambient temperature into a voltage reading, which can then be sensed by an analog pin of a micro-controller.
We are going to use the analog temperature sensor integrated in our MSP 430 and is going to read the temperature as a voltage, and by using mathematical equations we are going to be able to convert that voltage into a Fahrenheit or Celsius value.
We are going to use the analog temperature sensor integrated in our MSP 430 and is going to read the temperature as a voltage, and by using mathematical equations we are going to be able to convert that voltage into a Fahrenheit or Celsius value.
New PRE-WRITTEN FUNCTIONS OF ENERGIA THAT WE ARE GOING TO BE USING:
This functions are already on Energia and are there for us to use it and make our life a little easier:
How to know which one to choose? in summary, if you are measuring an analog signal that is sourced by the launchpad use the DEFAULT. Ex. The potentiometer reads the voltage of the same launchpad, the voltage just goes from the VCC to the launchpad and then return, the same voltage of the launchpad is just moving in a cycle. In this case use the DEFAULT Now if you are going to read something else use the 1.5V or the 2.5V, this are more accurate with external readings. Ex. The analog temperature sensor reads the environment and converts that signal into volts, then that goes to the launchpad. In this case use 1.5V or 2.5V
- analogReference(type): Configures the reference voltage used for analog input (i.e. the value used as the top of the input range). The options are:
- DEFAULT: the default analog reference of input voltage (VCC) ~3.3V-3.6V
- INTERNAL1V5: internal analog reference voltage of 1.5V
- INTERNAL2V5: internal analog reference voltage of 2.5V
- EXTERNAL: the voltage applied to the VREF pin is used as the reference.
How to know which one to choose? in summary, if you are measuring an analog signal that is sourced by the launchpad use the DEFAULT. Ex. The potentiometer reads the voltage of the same launchpad, the voltage just goes from the VCC to the launchpad and then return, the same voltage of the launchpad is just moving in a cycle. In this case use the DEFAULT Now if you are going to read something else use the 1.5V or the 2.5V, this are more accurate with external readings. Ex. The analog temperature sensor reads the environment and converts that signal into volts, then that goes to the launchpad. In this case use 1.5V or 2.5V
- long: Long variables are extended size variables for number storage, we could also say that is an integer but with math, so remember if you are going to do math with an integer it needs to be "long" instead of"int".
Ex. long newValue=(2 * 1000)/2 //the new integer is newValue which will be 1000 - Serial.write ('val'): Writes binary data to the serial port. This data is sent as a byte or series of bytes; to send the characters representing the digits of a number use the "print()" function instead. In simple words with serial.write what the message that is going to be send even if its a number, the computer is not going to see it as a number just as a value.
Ex. Serial.write ('45') // the computer will read 45 but not as a number just as a 45 (will read it as a byte)
Step 1:
The integrated temperature sensor of our Launchpad Energia recognize it as A10, now that you know that go ahead and cope the code below into a new sketch
Step 2:
There is no need to explain what is going on with this code, the only new thing that we see in this code is analogReference(), this function is to change the voltage reference, and we use the voltage reference of 1,5V because the voltage that is reading is from an external source, which is the temperature of the evironment.
Step 3:
As usual go ahead and compile this code, then upload it into your Launchpad.
Step 4:
Now that the code is in the MSP 430 go ahead and click the serial monitor button (top right of Energia) and a new window should open.
This is are the values in voltage that the temperature sensor is reading, in order to see the changes in reading, you can either rub the chip or expose it to something cold, a can of compressed air is a good option.
Well, this values are neat, but is not enough for us to know the temperature, so we are going to take care of that.
Well, this values are neat, but is not enough for us to know the temperature, so we are going to take care of that.
step 5:
In order for us to convert the voltage reading into a Fahrenheit value, we need to use this formula:
Fahrenheit= (analogRead - 630 * 761/1024). And for energia to understand this formula we need to use the code below:
Fahrenheit= (analogRead - 630 * 761/1024). And for energia to understand this formula we need to use the code below:
step 6:
Analyze the code and try to figure out what is going on. Essentially is the same code as the last one, we just set up some new "integers" (long) and also more serial.prints() in order to get a clearer message in the serial monitor. There is a detailed explanation of the code at the end of the steps.
step 7:
As usual compile it, and upload it to the Launchpad. Once the code is upload it, if you click into the serial monitor, you should be able to see the reading but now in Fahrenheit format.
Congratulations, now you know how to use the for-loop and the while loop but, take your time to analyze the code.
Still if you do not understand what is going on, I will make a simple explanation of the code below:
- First of all we know that the Launchpad is going to be reading the temperature by an analog temperature sensor, which is connected to a pin (A10), so it would make sense to set up the pin as an INPUT, but because it's an analog pin we do not need to necessarily do it.
- Also in the setup() we see that we initialize the communication between launchpad and computer by using Serial.begin(9600), then we set the voltage reference as 1.5V using analogReference(INTERNAL1V5), we do this because the signal that the launchpad will be reading is external (in this case use 1.5V or 2.5V).
- In the loop() we see that we set up a new "integer" which is sensorValue, but because we are going to do "math" inside the integer we need to set it up as "long", and by doing this we see that analogRead will be the value of A10 minus 630. Then we do the same thing with Fahrenheit value, and this will be equal to the value of sensorValue times 761, and that value over 1024. We need to use "long" two times because it would be to much information to retain in a simple "long" so it wouldn't work.
- Step number 3 pretty much give us the value in Fahrenheit format, now we just need to send it back to the computer, we do this by using Serial.print (fahrenheitValue,DEC), this will send the fahrenheitValue in decimal format to the computer, the we use Serial.write('°') to put the ° after the value, then we use Serial.println('F") to put the F after the ° and remember that the "ln" is to finish the line. So we end up with something like "80°F" and then a new line.
- And we delay this code to once per second, because temperature do not change that fast righ
Challenge:
Can you make the LED turn on if certain temperature is met? Just think of all the new possibilities this code give us, once you can make the LED turn on if a temperature is met, it would be a matter of little modifications to make an alarm sound or turn on something else.
Hint: you need to use the if/else statement.
In case you can't figure it out I will let the code below, but try to do it by yourself.
Hint: you need to use the if/else statement.
In case you can't figure it out I will let the code below, but try to do it by yourself.