# Function to convert sensor resistance to temperature

**URL:** https://discourse.nodered.org/t/function-to-convert-sensor-resistance-to-temperature/20861
**Category:** General
**Created:** [26 January 2020 05:35 UTC](https://discourse.nodered.org/t/function-to-convert-sensor-resistance-to-temperature/20861 "2020-01-26T05:35:44Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![seattlebiker](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/seattlebiker/32/11484_2.png) [@seattlebiker](https://discourse.nodered.org/u/seattlebiker)
#### Post date: [26 January 2020 05:35 UTC](https://discourse.nodered.org/t/function-to-convert-sensor-resistance-to-temperature/20861/1 "2020-01-26T05:35:44Z")

</div>

Please forgive the long and an apparent off-topic post, but I'm at my wits end trying to figure out a math function node.

I have a temperature/humidity sensor from MetOne Instruments that I'm trying to come up with a function node that would convert the sensor resistance to a temperature. It's been a LONG time since I've been in a math class so I'm hoping a math-savvy person can help me. The formula is below the chart which I've included for reference and checking against solutions derived by the equations. I was trying to include an image of the formulas from the sensor PDF but couldn't figure out how to do that, but where you see -1, that is actually the -1 exponent, or the reciprocal as in (Rt-1) is 1/Rt (at least from what I remember and what I've found on the web). But for the life of me I can't get the right temperature given the resistance, or even close. If I use 23663 ohms (60 degrees), when I solve for Tc it is nowhere near 23663. I'd LIKE to use the formula to get the temperature, but, since I only REALLY need is the sensor resistance from 0 to 100 degrees Fahrenheit, so maybe a lookup table? Suggestions? Thanks in advance.

# Tc Rt Tc Rt Tc Rt °F Ohms °F Ohms °F Ohms

50 26801 78 18908 106 13171  
51 26469 79 18672 107 12995  
52 26140 80 18439 108 12821  
53 25816 81 18208 109 12648  
54 25497 82 17980 110 12477  
55 25181 83 17754 111 12308  
56 24870 84 17531 112 12140  
57 24562 85 17310 113 11974  
58 24259 86 17092 114 11809  
59 23959 87 16876 115 11646  
60 23663 88 16662 116 11484  
61 23371 89 16451 117 11324  
62 23082 90 16241 118 11165  
63 22797 91 16034 119 11008  
64 22516 92 15829 120 10853  
65 22238 93 15627 121 10698  
66 21963 94 15426 122 10545  
67 21691 95 15227  
68 21423 96 15031  
69 21158 97 14836  
70 20896 98 14644  
71 20638 99 14453  
72 20382 100 14265  
73 20129 101 14078  
74 19879 102 13893  
75 19632 103 13710  
76 19388 104 13528  
77 19147 105 13349

Sensor Range = -58􀁱F to +122􀁱F (-50􀁱C to +50􀁱C)

Conversion Formulas:

Tc = (((Rt-1) + (23100-1))-1 – 13698.3) / (-129.163)  
Rt = (((-129.163Tc) +13698.3)-1) – 23100-1)-1  
Where: Tc = Temperature in degrees F  
Rt = Sensor Resistance in Ohms

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [26 January 2020 08:49 UTC](https://discourse.nodered.org/t/function-to-convert-sensor-resistance-to-temperature/20861/2 "2020-01-26T08:49:11Z")

</div>

> [@seattlebiker](#):
>
> Tc = (((Rt-1) + (23100-1))-1 – 13698.3) / (-129.163)

Can I just check that is the formula you are trying to evaluate. Are all the `-1` references actually raised to the power -1? So the formula is actually  
Tc = (((Rt^-1) + (23100^-1))^-1 – 13698.3) / (-129.163)  
Possibly it would be good to post a link to where we can see the formula written correctly or post a screenshot of the formula.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [26 January 2020 09:14 UTC](https://discourse.nodered.org/t/function-to-convert-sensor-resistance-to-temperature/20861/3 "2020-01-26T09:14:47Z")

</div>

OK, I have worked it out. The formula is

```auto
Tc = (((Rt^-1) + (23100^-1))^-1 – 13698.3) / (-129.163)

```

which gives you the temperature in Deg C (not F as you said). The Tc is a big clue. However the table you give is in Deg F. So 23663 ohms is 60 F. Here it is coded in javascript.

```auto
[{"id":"1caf4ac8.763d55","type":"inject","z":"5bb5a6a6.97527","name":"","topic":"","payload":"23663","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":682,"y":391,"wires":[["31737584.9b14d2"]]},{"id":"31737584.9b14d2","type":"function","z":"5bb5a6a6.97527","name":"Calc Temp C","func":"const rt = msg.payload\n// calc deg c\nmsg.payload = -(1/(1/rt + 1/23100) - 13698.3)/129.63\nreturn msg;","outputs":1,"noerr":0,"x":844,"y":391,"wires":[["c7a8c240.74c42","3d1baaf3.2190fe"]]},{"id":"c7a8c240.74c42","type":"debug","z":"5bb5a6a6.97527","name":"Deg C","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1019,"y":391,"wires":[]},{"id":"3d1baaf3.2190fe","type":"function","z":"5bb5a6a6.97527","name":"Convert to F","func":"// convert to F\nmsg.payload = msg.payload*9/5 + 32\nreturn msg;","outputs":1,"noerr":0,"x":872,"y":457,"wires":[["422892ad.08db14"]]},{"id":"422892ad.08db14","type":"debug","z":"5bb5a6a6.97527","name":"Deg F","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1027,"y":457,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [26 January 2020 11:32 UTC](https://discourse.nodered.org/t/function-to-convert-sensor-resistance-to-temperature/20861/4 "2020-01-26T11:32:10Z")

</div>

Have some stars for our math star! 🌟🌟🌟  
😀

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [26 January 2020 13:00 UTC](https://discourse.nodered.org/t/function-to-convert-sensor-resistance-to-temperature/20861/5 "2020-01-26T13:00:39Z")

</div>

Nicely done,  
(and add in a Math.round(...) to make them integers as per OP). spot on.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [26 January 2020 14:05 UTC](https://discourse.nodered.org/t/function-to-convert-sensor-resistance-to-temperature/20861/6 "2020-01-26T14:05:47Z")

</div>

> [@dceejay](#):
>
> and add in a Math.round

I don't think it is a good idea to throw away accuracy like that, not in the function. If it is desired only to show it to the nearest integer then do that when it is displayed.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [26 January 2020 14:18 UTC](https://discourse.nodered.org/t/function-to-convert-sensor-resistance-to-temperature/20861/7 "2020-01-26T14:18:55Z")

</div>

And likewise all those decimals give an unrealistic expectation of accuracy :-). I did just mean as the final step in the degF function. Which is as close to display as you get. ( unless you want to jsonata it in the debug node 😉

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [26 January 2020 14:24 UTC](https://discourse.nodered.org/t/function-to-convert-sensor-resistance-to-temperature/20861/8 "2020-01-26T14:24:44Z")

</div>

It might be going to an influx database for all we know, in which case it should not be rounded before going in. Or perhaps it might be fed into a PID node for temperature control, in which case rounding before that would be a disaster. Many of the dashboard widgets allow rounding in the widget itself so do it there.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [26 January 2020 14:39 UTC](https://discourse.nodered.org/t/function-to-convert-sensor-resistance-to-temperature/20861/9 "2020-01-26T14:39:13Z")

</div>

True. But only if the input measurement is 100% accurate. I suspect most measurements of resistance are no better than 1% error

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [26 January 2020 14:40 UTC](https://discourse.nodered.org/t/function-to-convert-sensor-resistance-to-temperature/20861/10 "2020-01-26T14:40:11Z")

</div>

Sorry, which bit do you mean is true, but only if ...

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [26 January 2020 14:41 UTC](https://discourse.nodered.org/t/function-to-convert-sensor-resistance-to-temperature/20861/11 "2020-01-26T14:41:23Z")

</div>

True = You are correct.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [26 January 2020 14:51 UTC](https://discourse.nodered.org/t/function-to-convert-sensor-resistance-to-temperature/20861/12 "2020-01-26T14:51:55Z")

</div>

If you mean about the PID, the accuracy of the sensor is immaterial (apart from the obvious effect on the final control temperature). If the process value is rounded before going into the PID algorithm it messes it up and will prevent the fine adjustments necessary for good control. The Derivative is particularly badly affected as the value will not move smoothly between rounded values.  
For example, this is a big problem using the DS18B20 temperature sensor for accurate control. Even if you calibrate the sensor the fact that the output has a resolution of 0.0625 degree C means that it is difficult to use Derivative effectively.

---

<div class="post-metadata">

### Author: ![seattlebiker](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/seattlebiker/32/11484_2.png) [@seattlebiker](https://discourse.nodered.org/u/seattlebiker)
#### Post date: [27 January 2020 06:31 UTC](https://discourse.nodered.org/t/function-to-convert-sensor-resistance-to-temperature/20861/13 "2020-01-27T06:31:21Z")

</div>

Wow, thanks Colin! I posted just to find someone with more smarts than me to fill in some blanks in my math knowledge, and _BONUS_, even got a function node to boot! So, let me say that I didn't even notice it before, but the formulas are the SAME for both conversions from/to Celsius and Fahrenheit. It's laughable because the only change between the formulas is one formula shows "Where: Tc = Temperature in C" and in the other (supposedly for Fahrenheit) it is: Where: Tc = Temperature in F" Somebody didn't proofread the document! So, rather than post links to the offending formulas, here they are pasted-in:

Celsius:  
Conversion Formulas:  
Tc = (((Rt-1) + (23100-1))-1 – 13698.3) / (-129.163)  
Rt = (((-129.163Tc) +13698.3)-1) – 23100-1)-1  
Where: Tc = Temperature in 􀁱C  
Rt = Sensor Resistance in Ohms

AND supposedly Fahrenheit:  
Conversion Formulas:  
Tc = (((Rt-1) + (23100-1))-1 – 13698.3) / (-129.163)  
Rt = (((-129.163Tc) +13698.3)-1) – 23100-1)-1  
Where: Tc = Temperature in 􀁱F  
Rt = Sensor Resistance in Ohms

Thanks again, Colin, for you math and Node-RED expertise!

Bruce

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [27 January 2020 15:44 UTC](https://discourse.nodered.org/t/function-to-convert-sensor-resistance-to-temperature/20861/14 "2020-01-27T15:44:54Z")

</div>

Well, on this side of the Atlantic, we use _proper_ measurements - so we don't need those old-fashioned Fahrenheit that start and end in weird places 🤣

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [27 January 2020 17:12 UTC](https://discourse.nodered.org/t/function-to-convert-sensor-resistance-to-temperature/20861/15 "2020-01-27T17:12:07Z")

</div>

Yes, but we do have continue to support the colonies.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [10 February 2020 17:12 UTC](https://discourse.nodered.org/t/function-to-convert-sensor-resistance-to-temperature/20861/16 "2020-02-10T17:12:13Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
