Raspberry Pi Beginners Tutorial 6: Using a Thermistor Temperature Sensor

Use a capacitor to take measurements using analogue sensors with your Raspberry Pi.

Using the circuit we made in tutorial 5, replace the photoresistor with the thermistor as follows:

Using a Thermistor Temperature Sensor with a Raspberry Pi schematic wiring diagram

Open IDLE on your Raspberry Pi (Menu > Programming > Python 2 (IDLE)) and open a new project (File > New File).  Then type the following:

import RPi.GPIO as GPIO
import time
from array import *

SensorPin=17
TriggerPin=27
GPIO.setmode(GPIO.BCM)
cap=0.000001
adj=2.130620985

def measureresistance(mpin,tpin):
    GPIO.setup(mpin, GPIO.OUT)
    GPIO.setup(tpin, GPIO.OUT)
    GPIO.output(mpin, False)
    GPIO.output(tpin, False)
    time.sleep(0.2)
    GPIO.setup(mpin, GPIO.IN)
    time.sleep(0.2)
    GPIO.output(tpin, True)
    starttime=time.time()
    endtime=time.time()
    while (GPIO.input(mpin) == GPIO.LOW):
        endtime=time.time()
    return endtime-starttime


def restotemp(rest):
    temp=[0]*151
    res=[0]*151
    temp[0]=-40
    temp[1]=-39
    temp[2]=-38
    temp[3]=-37
    temp[4]=-36
    temp[5]=-35
    temp[6]=-34
    temp[7]=-33
    temp[8]=-32
    temp[9]=-31
    temp[10]=-30
    temp[11]=-29
    temp[12]=-28
    temp[13]=-27
    temp[14]=-26
    temp[15]=-25
    temp[16]=-24
    temp[17]=-23
    temp[18]=-22
    temp[19]=-21
    temp[20]=-20
    temp[21]=-19
    temp[22]=-18
    temp[23]=-17
    temp[24]=-16
    temp[25]=-15
    temp[26]=-14
    temp[27]=-13
    temp[28]=-12
    temp[29]=-11
    temp[30]=-10
    temp[31]=-9
    temp[32]=-8
    temp[33]=-7
    temp[34]=-6
    temp[35]=-5
    temp[36]=-4
    temp[37]=-3
    temp[38]=-2
    temp[39]=-1
    temp[40]=0
    temp[41]=1
    temp[42]=2
    temp[43]=3
    temp[44]=4
    temp[45]=5
    temp[46]=6
    temp[47]=7
    temp[48]=8
    temp[49]=9
    temp[50]=10
    temp[51]=11
    temp[52]=12
    temp[53]=13
    temp[54]=14
    temp[55]=15
    temp[56]=16
    temp[57]=17
    temp[58]=18
    temp[59]=19
    temp[60]=20
    temp[61]=21
    temp[62]=22
    temp[63]=23
    temp[64]=24
    temp[65]=25
    temp[66]=26
    temp[67]=27
    temp[68]=28
    temp[69]=29
    temp[70]=30
    temp[71]=31
    temp[72]=32
    temp[73]=33
    temp[74]=34
    temp[75]=35
    temp[76]=36
    temp[77]=37
    temp[78]=38
    temp[79]=39
    temp[80]=40
    temp[81]=41
    temp[82]=42
    temp[83]=43
    temp[84]=44
    temp[85]=45
    temp[86]=46
    temp[87]=47
    temp[88]=48
    temp[89]=49
    temp[90]=50
    temp[91]=51
    temp[92]=52
    temp[93]=53
    temp[94]=54
    temp[95]=55
    temp[96]=56
    temp[97]=57
    temp[98]=58
    temp[99]=59
    temp[100]=60
    temp[101]=61
    temp[102]=62
    temp[103]=63
    temp[104]=64
    temp[105]=65
    temp[106]=66
    temp[107]=67
    temp[108]=68
    temp[109]=69
    temp[110]=70
    temp[111]=71
    temp[112]=72
    temp[113]=73
    temp[114]=74
    temp[115]=75
    temp[116]=76
    temp[117]=77
    temp[118]=78
    temp[119]=79
    temp[120]=80
    temp[121]=81
    temp[122]=82
    temp[123]=83
    temp[124]=84
    temp[125]=85
    temp[126]=86
    temp[127]=87
    temp[128]=88
    temp[129]=89
    temp[130]=90
    temp[131]=91
    temp[132]=92
    temp[133]=93
    temp[134]=94
    temp[135]=95
    temp[136]=96
    temp[137]=97
    temp[138]=98
    temp[139]=99
    temp[140]=100
    temp[141]=101
    temp[142]=102
    temp[143]=103
    temp[144]=104
    temp[145]=105
    temp[146]=106
    temp[147]=107
    temp[148]=108
    temp[149]=109
    temp[150]=110
    res[0]=190556
    res[1]=183413
    res[2]=175674
    res[3]=167646
    res[4]=159564
    res[5]=151597
    res[6]=143862
    res[7]=136436
    res[8]=129364
    res[9]=122667
    res[10]=116351
    res[11]=110409
    res[12]=104827
    res[13]=99584
    res[14]=94660
    res[15]=90032
    res[16]=85677
    res[17]=81574
    res[18]=77703
    res[19]=74044
    res[20]=70581
    res[21]=67298
    res[22]=64183
    res[23]=61223
    res[24]=58408
    res[25]=55728
    res[26]=53176
    res[27]=50745
    res[28]=48429
    res[29]=46222
    res[30]=44120
    res[31]=42118
    res[32]=40212
    res[33]=38398
    res[34]=36674
    res[35]=35036
    res[36]=33480
    res[37]=32003
    res[38]=30602
    res[39]=29275
    res[40]=28017
    res[41]=26825
    res[42]=25697
    res[43]=24629
    res[44]=23617
    res[45]=22659
    res[46]=21752
    res[47]=20891
    res[48]=20074
    res[49]=19298
    res[50]=18560
    res[51]=18481
    res[52]=18148
    res[53]=17631
    res[54]=16991
    res[55]=16279
    res[56]=15535
    res[57]=14786
    res[58]=14055
    res[59]=13353
    res[60]=12690
    res[61]=12068
    res[62]=11490
    res[63]=10953
    res[64]=10458
    res[65]=10000
    res[66]=9576
    res[67]=9183
    res[68]=8818
    res[69]=8478
    res[70]=8160
    res[71]=7860
    res[72]=7578
    res[73]=7310
    res[74]=7056
    res[75]=6813
    res[76]=6580
    res[77]=6357
    res[78]=6141
    res[79]=5934
    res[80]=5734
    res[81]=5540
    res[82]=5353
    res[83]=5172
    res[84]=4997
    res[85]=4828
    res[86]=4665
    res[87]=4507
    res[88]=4354
    res[89]=4207
    res[90]=4065
    res[91]=3927
    res[92]=3793
    res[93]=3663
    res[94]=3537
    res[95]=3414
    res[96]=3293
    res[97]=3175
    res[98]=3057
    res[99]=2941
    res[100]=2825
    res[101]=2776
    res[102]=2717
    res[103]=2652
    res[104]=2581
    res[105]=2507
    res[106]=2431
    res[107]=2355
    res[108]=2280
    res[109]=2206
    res[110]=2135
    res[111]=2066
    res[112]=2000
    res[113]=1937
    res[114]=1878
    res[115]=1822
    res[116]=1769
    res[117]=1719
    res[118]=1672
    res[119]=1628
    res[120]=1586
    res[121]=1545
    res[122]=1507
    res[123]=1470
    res[124]=1435
    res[125]=1400
    res[126]=1366
    res[127]=1333
    res[128]=1300
    res[129]=1268
    res[130]=1236
    res[131]=1203
    res[132]=1171
    res[133]=1139
    res[134]=1106
    res[135]=1074
    res[136]=1042
    res[137]=1010
    res[138]=978
    res[139]=948
    res[140]=918
    res[141]=888
    res[142]=861
    res[143]=834
    res[144]=809
    res[145]=787
    res[146]=766
    res[147]=748
    res[148]=733
    res[149]=721
    res[150]=713
    op=0
    for ii in range(0,150):
        if rest

Save your project as temperature.py (File > Save As) in your Documents folder.

Now open Terminal (Menu > Accessories > Terminal) and type the following command:

python temperature.py

The Raspberry Pi will repeatedly display the current temperature. If you carefully hold your fingers over the top of the thermistor, the temperature should increase. You can stop this program from running by pressing CTRL+Z.

The above script has to start by ensuring the capacitor is empty, charge the capacitor, measure the time it takes to charge, use this data to calculate the resistance and then use the resistance to figure out what the temperature is. It then needs to return the temperature and repeat the whole process again.

When a script requires such a complicated procedure with multiple steps, it's possible to break down the steps into smaller segments by using functions. Functions allow you to create a dromant section of code at the beginning of your script and use (or "Call") it later on. Each function can be called multiple times and you can input and outpit data to and from the function.

On line 11 we create the function measureresistance with the command:

def measureresistance(mpin,tpin)

We have also created two parameters in the brackets which will tell the function which GPIO pins are used (mpin and tpin). These parameters are only valid within the functions code and will be populated with the values we specify when calling the function.

The function will then discharge and recharge the capacitor and calculate the resistance up until line 24. On line 24, we use the return command to output the resistance.

The code we have created does not do anything until we call it on line 341 with:

mrst=measureresistance(SensorPin,TriggerPin)

By including the SensorPin and TriggerPin parameters in the brackets, we will transfer their values to the function's mpin and tpin parameters respectively. The measurereistance function will now calculate and return the resistance, which will be stored in the mrst parameter for future use.

In lines 27 to 336 we create a function called restotemp which contains a predefined list of resistance values and the corresponding temperature for the thermistor.