You want to measure temperature using a TMP36 and an analog-to-digital converter.
Use an MCP3008 ADC chip.
However, unless you need more than one analog channel, you should consider using the DS18B20 digital temperature sensor, which is more accurate and doesn’t require a separate ADC chip (Recipe 12.9).
To try this recipe, you will need:
• Breadboard and jumper wires (see “Prototyping Equipment” on page 380)
• MCP3008 eight-channel ADC IC (see “Integrated Circuits” on page 381)
• TMP36 temperature sensor (see “Integrated Circuits” on page 381)
Figure 12-13 shows the arrangement for this, using a breadboard.
Make sure that you get the TMP36 the right way around. One side of the package is flat, while the other is curved.
You will need to set up SPI on your Raspberry Pi, so if you haven’t already done so, follow Recipe 8.6.
Open an editor (nano or IDLE) and paste in the following code. As with all the program examples in this book, you can also download the program from the Code section of the Raspberry Pi Cookbook website, where it is called adc_tmp36.py.
import spidev, time
spi = spidev.SpiDev() |
The program is based on that of Recipe 12.4. A little bit of additional math calculates the temperature in degrees Celsius and Fahrenheit:
$ sudo python adc_tmp36.py Temp C=19.287109 Temp f=66.716797 Temp C=18.642578 Temp f=65.556641 Temp C=18.964844 Temp f=66.136719 Temp C=20.253906 Temp f=68.457031 Temp C=20.898438 Temp f=69.617188 Temp C=20.576172 Temp f=69.037109 Temp C=21.865234 Temp f=71.357422 Temp C=23.154297 Temp f=73.677734 Temp C=23.476562 Temp f=74.257812 Temp C=23.476562 Temp f=74.257812 Temp C=24.121094 Temp f=75.417969 Temp C=24.443359 Temp f=75.998047 Temp C=25.087891 Temp f=77.158203 |
Discussion
The TMP36 outputs a voltage that is proportional to the temperature. According to the datasheet for the TMP36, the temperature in degrees C is calculated as the voltage (in volts) times 100 minus 50.
The TMP36 is fine for measuring the approximate temperature but is specified as having an accuracy of only 2%. This will only get worse if you attach long leads to it. To some extent, you can calibrate an individual device, but for better accuracy, use a DS18B20 (Recipe 12.9), which has a stated accuracy of 0.5% over a temperature range of -10 to +85 degrees C. Being a digital device, it should not suffer any loss of accuracy when attached to long leads.
See Also
Take a look at the TMP36 datasheet.
Table A-3. Prototyping equipment | |
Description | Suppliers |
M-M jumper wires | SparkFun: PRT-08431, Adafruit: 759 |
M-F jumper wires | SparkFun: PRT-09140, Adafruit: 825 |
F-F jumper wires | SparkFun: PRT-08430, Adafruit: 794 |
Half-sized breadboard | SparkFun: PRT-09567 Adafruit: 64 |
Pi Cobbler | Adafruit: 1105 |
Table A-6. Integrated circuits | |
7805 voltage regulator | SparkFun: COM-00107 |
L293D motor driver | SparkFun: COM-00315, Adafruit: 807 |
ULN2803 Darlington driver IC | SparkFun: COM-00312, Adafruit: 970 |
DS18B20 temperature sensor | SparkFun: SEN-00245, Adafruit: 374 |
MCP3008 eight-channel ADC IC | Adafruit: 856 |
TMP36 temperature sensor | SparkFun: SEN-10988, Adafruit: 165 |