In this recipe, we will measure the room temperature with your Galileo/Edison using the Grove temperature sensor module. I will be showing two ways of doing it first using mraa and then upm.
As the name suggests, the resistance ('isotr' part) offered to the current flowing through this device is dependent on the thermal (the 'therm' part) energy applied to it. Now from the previous recipe, we know that this change in resistance can be measured by sensing the change in the voltage across the device using the ADC module on the Edison/Galileo. The voltage that we will be measuring is representative of the temperature. This representation is dependent on the characteristics of the chosen thermistor. To convert it into standard temperature representation form, such as in Celsius or Fahrenheit, you will have to apply some equation on the read raw adc value. These equations can be inferred from the data sheet of the chosen thermistor. That is what we be doing using the mraa library first:
The setup is quite straight forward, the temperatures sensor is connected to analog port A1. In the script, port A1 is initialized and the analog voltage is read and then using the equations found here: http://www.seeedstudio.com/wiki/Grove_-_Temperatur..., the temperature is calculated in Celsius and printed out to the console.
import mraa# More info here -> http://www.seeedstudio.com/wiki/Grove_-_Temperature_Sensor_V1.2B=3975ain = mraa.Aio(1)a = ain.read()resistance = (1023-a)*10000.0/atemp = 1/(math.log(resistance/10000.0)/B+1/298.15)-273.15print"Temperature now is"+ str(temp)
As mentioned in one of my previous recipe, upm can be used to interface with sensors. Either you can write code using mraa to drive your sensor (in the process, write initializing code and supporting code in case of more complicated components) or you can leverage on the work done by good folks at Intel and use upm. upm supports an array of sensors and peripherals for you to use. The following script does the same thing as done by the previous script but this time with upm and you can see how simple things can get with the upm. In the script below, we import the upm library using
and specify the ADC in pin that you want to use
temp = pyupm_grove.GroveTemp(1)
You can read the temperature using the “value” method.
Putting it ll together in one piece, we have:
import pyupm_grove # Get the UPM library# Create the temperature sensor objecttemp = pyupm_grove.GroveTemp(1)# Use"value"method on it to get the temperature print"Temperature now is"+ str(temp.value())
You can find these scripts here on Github.