Holooly Plus Logo

Question 12.7: You want to measure temperature using a TMP36 and an analog-......

You want to measure temperature using a TMP36 and an analog-to-digital converter.

Step-by-Step
The 'Blue Check Mark' means that this solution was answered by an expert.
Learn more on how do we answer questions.

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()
spi.open(0,0)
def analog_read(channel):
r = spi.xfer2([1, (8 + channel) << 4, 0])
adc_out = ((r[1]&3) << 8) + r[2]
return adc_out
while True:
reading = analog_read(0)
voltage = reading * 3.3 / 1024
temp_c = voltage * 100 – 50
temp_f = temp_c * 9.0 / 5.0 + 32
print(“Temp C=%f\t\tTemp f=%f” % (temp_c, temp_f))
time.sleep(1)

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
13

Related Answered Questions

Question: 12.11

Verified Answer:

Use the Tkinter library to open a window and write...
Question: 12.10

Verified Answer:

Use a low cost SR-04 rangefinder. These devices ne...
Question: 12.12

Verified Answer:

Write a Python program that writes the data to a f...
Question: 12.9

Verified Answer:

Use the DS18B20 digital temperature sensor. This d...
Question: 12.8

Verified Answer:

Use an analog accelerometer with a MCP3008 ADC chi...
Question: 12.6

Verified Answer:

Use a potential divider with one fixed resistor an...
Question: 12.2

Verified Answer:

Use the same basic recipe and code as Recipe 12.1,...
Question: 12.3

Verified Answer:

Low-cost resistive gas sensors are available that ...
Question: 12.4

Verified Answer:

The Raspberry Pi GPIO connector has only digital i...
Question: 12.5

Verified Answer:

Use a pair of resistors to act as a voltage divide...