Temperature Sensing with the TMP36 and Pycom

Stephen just shared a new tutorial: "Temperature Sensing with the TMP36 and Pycom"

The TMP36 is a low cost, easy to use sensor that fits well with any project. With the TMP36, adding temperature sensing to your Pycom project is easy! In this tutorial, we will use the Pycom Lopy4, and send our measured data to The Things Network via …

Read more

Could you do an example (maybe same project) with Bluetooth(BLE).

Thank you!


We don’t have any plans currently to incorporate Bluetooth into a project like this. I’ll keep it in mind though!

Trying to follow this tutorial and when I run the code I get this error :
Traceback (most recent call last):
File “”, line 16
SyntaxError: invalid syntax

Line 16 of the code is: print(“Reading TMP36 Sensor…”).
Any thoughts? I’ve checked the voltage across the pins of the TMP36 - all good.
So I’m thinking it’s something to do with the sensor or pin allocations/wiring?

Hi Miles,

It’s most likely an error in your code, or with the indentation in your code. This should run with nothing attached. I just confirmed on my device that the code works as its shown on the website. I do notice that sometimes this page loads without any indentations in the code though. Does yours have indentations?

If that fails when is the last time you updated the firmware on your Pycom board?

Thanks for checking the code. I’ll update the firmware and try again
No indentation for the code

Hey Miles,

The preformatted text (code formatting) is currently down on our website. This is doubtlessly a case of missing indents. Indentation is critical for MicroPython to work properly.

I’m unclear how or if the expansion board 3.0 also needs upgrading?
With the pycom updater open and only expansion board plugged in can’t find it for a start. So I’m assuming it needs some sort of seperate updater or it updates together with the Lopy4?

Sorry Stephen,
More questions…
Which lines are indented? Perhaps you could add a screen shot in your tutorial of Atom to offer a hint?

Hi Miles,

We’ve fixed the website so it should be showing the indented code again. You should not need to update the expansion board, just the Lopy4 occasionally, but really the problem hear is the indents.

Thanks Steve,
It now runs, … sort of … and TTN connects but I only get one line of ‘application data’.

But then nothing. The code error I get is:
Traceback (most recent call last):
File “main.py”, line 35
IndentationError: unexpected indent

Line 35 refers to: app_eui = binascii.unhexlify(‘70B3D57ED001675E’)

I’m unclear what this means or how to trouble shoot? Any suggestions?


Hey Miles,

Can you copy and paste your code as it appears in Atom? There is a “Preformatted text” option in the forum field that will allow you to keep the code exactly as it is. It is this symbol </>
Just select all the code after you’ve pasted it and hit the preformatted text button. I can’t tell you whats wrong with your code without being able to see it. An indentation error is exactly what it sounds like. Something is indented improperly. Usually the line in question or the line right before it.

When you paste it in please remove the app_eui and app_key and replace them with “XXXXXXXXX” to protect your application.

Looking forward to your response!

Thanks Stephen, code as follows …

# Measuring temperature by TMP36
from network import LoRa
import socket
import utime
import binascii
import pycom
import ustruct
import machine
from machine import Pin

adc = machine.ADC()               # create an ADC object
apin = adc.channel(pin=Pin.exp_board.G3)   # Lopy4 specific: (pin = 'P16')   create an analog pin on P16 & connect TMP36

# Temp measurment
def temp_measure():
    print("Reading TMP36 Sensor...")
    value = apin()
    print("ADC count = %d" %(value))

    # LoPy  has 1.1 V input range for ADC
    temp = ((value * 1100 ) / 4096 - 500) / 10
    print("Temperature = %5.1f C" % (temp))

    return temp

# disable LED heartbeat (so we can control the LED)
# set LED to red

# lora config
lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.AS923)
# access info
app_eui = binascii.unhexlify('xxx')
app_key = binascii.unhexlify('xxx')

# attempt join - continues attempts background
lora.join(activation=LoRa.OTAA, auth=(app_eui, app_key), timeout=0)

# wait for a connection
print('Waiting for LoRaWAN network connection...')
while not lora.has_joined():
	# if no connection in a few seconds, then reboot
	if utime.time() > 15:
		print("possible timeout")

# we're online, set LED to green and notify via print
print('Network joined!')

# setup the socket
s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)

count = 0
# limit to 200 packets; just in case power is left on
while count < 200:

	# take temp measurment, turn the temp blue when measuring
	temp = temp_measure()

	# encode the packet, so that it's in BYTES (TTN friendly)
	# could be extended like this struct.pack('f', temp) + struct.pack('c',"example text")
    # 'h' packs it into a short, 'f' packs it into a float, must be decoded in TTN
	packet = ustruct.pack('f', temp)

	# send the prepared packet via LoRa

	# example of unpacking a payload - unpack returns a sequence of
	#immutable objects (a list) and in this case the first object is the only object
	print ("Unpacked value is:", ustruct.unpack('f',packet)[0])

	# check for a downlink payload, up to 64 bytes
	rx_pkt = s.recv(64)

	# check if a downlink was received
	if len(rx_pkt) > 0:
		print("Downlink data on port 200:", rx_pkt)
		input("Downlink recieved, press Enter to continue")

	count += 1

Hi Miles,

The code looks correct to me. Can you send me everything you get out of REPL when attempting to run?

Hi Stephen,
REPL run
>>> Running main.py

Traceback (most recent call last):
  File "<stdin>", line 36
IndentationError: unexpected indent
Pycom MicroPython 1.18.1.r10 [v1.8.6-849-d53c7f3] on 2019-01-10; LoPy4 with ESP32
Type "help()" for more information.

On the TTN console - I get one payload
Time: 16:04:05 Count: 01 payload: [not provided] temp: 0

Could it be my wiring of the TMP36?


I’m stumped Miles. It looks exactly the same as my code and mine is working. Maybe its how you have wired it. What about with nothing connected at all?

Ah, now up and running and receiving packets on the TTN console. I manually checked indents for each line of code. And after many attempts and iterations, it now just seems to work.
…next question(s)?

  1. On the TTN console, Where will the temp appear after loading the java decode script?

  2. Lights on, nobody home! When I try an run the LoPy4 + Exp3.0 on a lipo battery - the LoPy4 won’t run, despite power and Bat lights on the Exp3.0. I’ve read elsewhere that this may be a fault with the boards?

Hi Miles,

The first image in this TTN Tutorial shows the payloads being decoded.

It will be to the right of the Payload (shown in bytes) in the Data section of TTN.

To run the Lopy4 and Exp3.0 with a battery you need to install a jumper. There is a design choice here that is beyond me as to why it exists, but it’s easy to work around. If I remember correctly the SD card is powered instead of the board from the battery. If you power by USB and battery, then disconnect the USB it will stay on, but if you try to power just off the battery it never starts up. A Jumper fixes this though

“To use the battery, pull P8/G15 high (connect to 3v3 ). If you want to use the SD card as well, use a 10k pull-up.”

Thanks, again for your patience. I’ve now sorted the batt power issue out with a jumper. Now on to how to decode payloads? And I’m getting nowhere as I don’t have much of a clue about Java coding?
This is what I’m seeing of the data arriving at my TTN console:
`14:18:21 dev addr:xxxx app eui:xxx dev eui:xxx

This suggests to me the java script is not running correctly or I need to change something in the code either at the pycom end of the TTN end?

Hi Miles,

It looks like you are only getting the connection request through to TTN and no payloads are coming through. It should be displaying a new payload every ten seconds. Even if your Java decoder code doesn’t work, you should be seeing new payloads come in and their value in bytes. I would carefully check over your code again, as it seems that nothing is actually being sent.