ADC Pi with TMP36

Created 02/10/2015 | ADC Pi with TMP36 Temperature Sensor

In this tutorial we will use the ADC Pi, ADC Pi Plus or ADC Pi Zero with a Analog Devices TMP36 temperature sensor to make a digital thermometer. For this you will need your Raspberry Pi, an ADC Pi, ADC Pi Plus or ADC Pi Zero, and a TMP36 Voltage Output Temperature Sensor.

We will use the AB Electronics python library to talk to the ADC Pi, you can download the library from github using the following command in a terminal, and this will download the full python library into the currently selected folder.

git clone https://github.com/abelectronicsuk/ABElectronics_Python_Libraries.git

You will need to enable i2c on your Raspberry Pi, see our other tutorial on i2c: I2C, SMBus and Raspbian Linux

The AB Electronics python library uses another library called python-smbus, you can install it using apt-get with the following commands.

sudo apt-get update

sudo apt-get install python-smbus

Finally you will want to tell python where you download the AB Electronics python library by adding it into PYTHONPATH.

export PYTHONPATH=${PYTHONPATH}:~/Desktop/ABElectronics_Python_Libraries/ADCPI/

If you downloaded it to somewhere other than your desktop you will need to change the path to match your download destination.

Now all of the libraries are installed and the Raspberry Pi is configured to use i2c we can begin building our project.

Parts Used:

ADC Pi Plus or ADC Pi Zero

Analog Devices TMP36 Voltage Output Temperature Sensor

Connecting the Sensor to the ADC Pi Plus

Click to enlargeIf you haven’t done so install your ADC Pi Plus onto the Raspberry Pi by connecting it onto the GPIO header.  Make sure your Raspberry Pi is turned off when you do this to minimise the risk of damaging the Raspberry Pi or the ADC Pi Plus.

Next connect the TMP 36 sensor to the ADC Pi Plus. Pin 1 connects to +5V, Pin 2 to input 1 on the ADC Pi, Pin 3 connects to GND.

For this tutorial we will start by creating a new python program file called demo-tmp36.py.  You can use your favourite text editor for writing the program.  You can find a complete example of demo-tmp36.py in the ABElectronics_Python_Libraries/ADCPI/ folder.

At the top of your program you will need to import the ABEHelpers library, ADCPI library and time library.

#!/usr/bin/python
from ABE_ADCPi import ADCPi
from ABE_helpers import ABEHelpers
import time

The ADCPI library is used for all communication with your ADC Pi Plus, it gives you control over almost everything that can be done with the MCP3424 controller.

The ABEHelpers class contains a function called get_smbus() that allows us to find out which I2C bus is being used on the Raspberry Pi.  Create an instance of the ABEHelpers class called i2c_helper and use it to create an i2c_bus object.

i2c_helper = ABEHelpers()

i2c_bus = i2c_helper.get_smbus()

Next we need to create an instance of the ADCPI class using the i2c_bus object and call it adc

adc = ADCPi(bus, 0x68, 0x69, 18)

0x68 and 0x69 are the I2C address for the ADC chips, if you have changed the address selection jumpers on your ADC Pi Plus then you will need to change these numbers to match the new addresses.

Now create a function to calculate the temperature based on the voltage output from the ADC Pi Plus

def calcTemperature(inval):
    # TMP36 returns 0.01 volts per C - -40C to +125C
    # 750mV = 25C and 500mV = 0C so the temperature is (voltage / 0.01) - 50
    return ((inval/0.01)-50)

We are going to need is a loop so that the same commands can run over and over.  This can be done with a simple while loop.

while True:

As True is always true the while loop will keep running until you exit the program with a Ctrl-C.

In order to clear the console for each read we need to issue a clear command.

    # clear the console
    os.system('clear')

Next we read from channel 1 on the ADC Pi Plus and use the calcTemperature function to convert the voltage to a temperature and print to the console.

     # read from adc channels and print to screen
    print ("Temperature on channel 1: %0.02f°C" % calcTemperature(adc.read_voltage(1)))

Using the time.sleep method we add a 0.5 second delay before repeating the read again.

     time.sleep(0.5)

time.sleep takes one variable, a number which represents the number of seconds to wait.  1 will make the program sleep for 1 second while 0.1 would wait for 100ms.

That is everything we need to make read from the TMP36 sensor using your ADC Pi or ADC Pi Plus, your program should now look like this.

#!/usr/bin/python
from ABE_ADCPi import ADCPi
from ABE_helpers import ABEHelpers
import time
import os

i2c_helper = ABEHelpers()
bus = i2c_helper.get_smbus()
adc = ADCPi(bus, 0x68, 0x69, 18)

def calcTemperature(inval):
    # TMP36 returns 0.01 volts per C - -40C to +125C
    # 750mV = 25C and 500mV = 0C so the temperature is (voltage / 0.01) - 50

    return ((inval/0.01)-50)

while (True):

    # clear the console
    os.system('clear')

    # read from adc channels and print to screen
    print ("Temperature on channel 1: %0.02f°C" % calcTemperature(adc.read_voltage(1)))

    # wait 0.5 seconds before reading the pins again
    time.sleep(0.5)

Save your program as "demo-tmp36.py" and run it in a command terminal using

python demo-tmp36.py

You will now have a temperature reading from your sensor on the console which is updating twice a second.

Shop

Support

Click to contact us.
X

Have any Questions?

Send us any questions about this page or product and we will reply within 24 hours.

Submit your Message

X

Have any Questions?

Thank you for contacting us.

We will respond to your enquiry in the next 24 hours.

You can find answers to many questions and additional product support in our Knowledge Base and the Community Forum.