MicroPython Temperature Sensor
Tags: micropython, nodemcu, tmp36
Published: 2017-06-14 14:11:18

---
title: MicroPython Temperature Sensor
published: true
cover_image: https://www.kenwalger.com/blog/wp-content/uploads/2017/06/temp_sensor_code.png
description: Using MicroPython with a NodeMCU ESP8266 for temperature readings
tags: MicroPython, NodeMCU, TMP36
---

I've shown an example of the "Hello World" in IoT, a blinking light, in this <a href="https://www.kenwalger.com/blog/iot/micropython-and-nodemcu-esp8266/">post</a>. Blinking lights are great and make for a nice visual experience, but what if we want to do something with an IoT device, such as a <a href="https://www.amazon.com/gp/product/B01N0B48NI/ref=as_li_tl?ie=UTF8&amp;amp;tag=kenwalgersite-20&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;linkCode=as2&amp;amp;creativeASIN=B01N0B48NI&amp;amp;linkId=dc1e9f82e2beb42bca36616a22b8820d" target="_blank" rel="noopener">NodeMCU ESP8266</a>Â that goes beyond the visual? Let's take our NodeMCU and add a temperature sensor. Then, with <a href="http://www.micropython.org" target="_blank" rel="noopener">MicroPython</a>, we'll get our readings.
<h3>Needed Equipment</h3>
To get started on this project you'll need the following equipment:
<ul>
 	<li>NodeMCU ESP8266</li>
 	<li><a href="https://www.amazon.com/gp/product/B011NEQ7J4/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B011NEQ7J4&amp;linkCode=as2&amp;tag=kenwalgersite-20&amp;linkId=c8ee5dcc279985c2897c08b0ef5a504f" target="_blank" rel="noopener">TMP36 Analog</a><img style="border: none !important; margin: 0px !important;" src="//ir-na.amazon-adsystem.com/e/ir?t=kenwalgersite-20&amp;l=am2&amp;o=1&amp;a=B011NEQ7J4" alt="" width="1" height="1" border="0" /> Temperature Sensor</li>
 	<li>Mini <a href="https://www.amazon.com/gp/product/B0135IQ0ZC/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B0135IQ0ZC&amp;linkCode=as2&amp;tag=kenwalgersite-20&amp;linkId=bbc58c90bb19ac2c27c2637e36945ee1" target="_blank" rel="noopener">Breadboard 5.5Ã—8.2Ã—0.85cm</a><img style="border: none !important; margin: 0px !important;" src="//ir-na.amazon-adsystem.com/e/ir?t=kenwalgersite-20&amp;l=am2&amp;o=1&amp;a=B0135IQ0ZC" alt="" width="1" height="1" border="0" /></li>
 	<li><a href="https://www.amazon.com/gp/product/B005TZJ0AM/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B005TZJ0AM&amp;linkCode=as2&amp;tag=kenwalgersite-20&amp;linkId=92661e6db23b8ee403829d8aa5dfb7ba" target="_blank" rel="noopener">Male to Male Jumper Wires</a><img style="border: none !important; margin: 0px !important;" src="//ir-na.amazon-adsystem.com/e/ir?t=kenwalgersite-20&amp;l=am2&amp;o=1&amp;a=B005TZJ0AM" alt="" width="1" height="1" border="0" /></li>
</ul>
I have found <a href="https://www.amazon.com/gp/product/B01IH4VJRI/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B01IH4VJRI&amp;linkCode=as2&amp;tag=kenwalgersite-20&amp;linkId=3326e2a9ffcc5d2da8294511c4512f95" target="_blank" rel="noopener">this kit</a><img style="border: none !important; margin: 0px !important;" src="//ir-na.amazon-adsystem.com/e/ir?t=kenwalgersite-20&amp;l=am2&amp;o=1&amp;a=B01IH4VJRI" alt="" width="1" height="1" border="0" /> of basic electronic components to be pretty good for starting one's IoT device journey. It doesn't include things like the TMP36 temperature sensor, but it has a wide variety of other pieces that are ultimately useful.
<h5>TMP36 Specs</h5>
Since this project will be using the TMP36, let's discuss it briefly.

<a href="https://www.kenwalger.com/blog/wp-content/uploads/2017/06/TMP36-e1497222851900.jpg"><img class="size-full wp-image-375 aligncenter" src="https://www.kenwalger.com/blog/wp-content/uploads/2017/06/TMP36-e1497222851900.jpg" alt="TMP36 Temperature Sensor" width="200" height="200" /></a>

This temperature sensor is a low voltage sensor, requiring 2.7 V to 5.5 VDC input. It returns a Celsius temperature reading from the Vout pin in an operating range of -40Â°C to +125Â°C. It is reasonably accurate, especially for hobby/demo situations with a Â±2Â°C accuracy. You can download the specs for the TMP36 <a href="http://kenwalger.com/documents/TMP35_36_37.pdf">here</a>.
<h3>Temperature Sensor Project</h3>
<h5>Hardware Configuration</h5>
<span style="color: #ff0000;">Make sure the NodeMCU is disconnected from USB when making connections.</span>

<a href="https://www.kenwalger.com/blog/wp-content/uploads/2017/06/temp-sensor-schematic.png"><img class="alignnone size-full wp-image-379" src="https://www.kenwalger.com/blog/wp-content/uploads/2017/06/temp-sensor-schematic.png" alt="Temperature Sensor schematic" width="960" height="636" /></a>
<ol>
 	<li>To add the TMP36 sensor to the NodeMCU we need to make sure that it is properly oriented. The flat side of the temperature sensor should be facing the bottom of the board.</li>
 	<li>Connect the rightmost lead, in position a8 to the negative rail on the board.</li>
 	<li>The negative rail then is connected to the GND pin (pin a29)on the NodeMCU.</li>
 	<li>Connect the leftmost lead (a10) of the TMP36 to the positive rail.</li>
 	<li>Connect the 3v3 pin (a30) on the NodeMCU to the positive rail.</li>
 	<li>Finally, connect a jumper wire from the center lead (c9) to the A0 pin on the NodecMCU (j16).</li>
</ol>
<a href="https://www.kenwalger.com/blog/wp-content/uploads/2017/06/bread_board.png"><img class="alignnone size-full wp-image-383" src="https://www.kenwalger.com/blog/wp-content/uploads/2017/06/bread_board.png" alt="Temperature Sensor bread board" width="600" height="341" /></a>

The A0 pin on the NodeMCU is the analog-to-digital conversion (ADC) pin.
<h5>Code</h5>
With the hardware side of things built, let's see what we can do in MicroPython to get a temperature! Fortunately, MicroPython's <code>machine</code> library makes this pretty simple for us.

I'll be working in the console REPL. Â The code should work the same, however, if you are working in the <a href="https://www.kenwalger.com/blog/iot/micropython-webrepl-esp8266/">WebREPL</a> environment.

Our first step is to handle our imports
<pre><code>from machine import ADC</code></pre>
That brings in our necessary ADC connections, and we can assign a variable to <code>pin 0</code> for that
<pre><code>adc = ADC(0)</code></pre>
We can print out the value from the TMP36 now with <code>adc.read()</code> which returns the Celsius temperature, well almost. The value it returns is ten times the temperature. Let's write a function that will handle that conversion for us.
<pre><code>def temp(value):
    return value/10
</code></pre>
While we're at it, let's write a function to convert to Fahrenheit as well.
<pre><code>def fahrenheit(celsius):
    return (celsius * (9/5)) + 32
</code></pre>
With those in place, we can get, and display our readings.
<pre><code>reading = adc.read()

celsius_temp = temp(reading)

fahrenheit_temp = fahrenheit(celsius_temp)

print("TMP36 reading {}\nDegrees Celsius {}\nDegrees Fahrenheit {}".format(reading, celsius_temp, fahrenheit_temp))</code></pre>
After executing our print statement we should get back our readings. MicroPython certainly has made things easy for us with the ADC methods.

<a href="https://www.kenwalger.com/blog/wp-content/uploads/2017/06/temp_sensor_code.png"><img class="alignnone size-full wp-image-385" src="https://www.kenwalger.com/blog/wp-content/uploads/2017/06/temp_sensor_code.png" alt="Temperature Sensor REPL" width="600" height="293" /></a>

For your convenience I have included the <a href="https://gist.github.com/kenwalger/f94dc82a2e797da372dcbae46fe78ddc" target="_blank" rel="noopener">code is available</a> as a Gist on GitHub as well.
<h3>Wrap Up</h3>
In this post, I have shown how to get temperature readings from an analog temperature sensor, such as the TMP36. In just a few lines of MicroPython code, we are able to get quite a bit of functionality.Â This is one of the many great things about MicroPython, the direct access to hardware is generally pretty easy.

I think the next step in exploring MicroPython and the NodeMCU will be to take these temperature readings and see if we can connect them to a service such as <a href="https://www.losant.com" target="_blank" rel="noopener">Losant</a> and generate some visualizations of our temperatures.

<hr />

Follow me on Twitter <a href="https://www.twitter.com/kenwalger" target="_blank" rel="noopener">@kenwalger</a> to get the latest updates on my postings on MicroPython and IoT and let me know what you are building with MicroPython. If you enjoyed this article, or have questions, leave comments below. Or see the original post on my <a href="https://www.kenwalger.com/blog" target="_blank">blog</a>.