---
# System prepended metadata

title: Tutorial on a project on how to build an temperature sensor

---

# Tutorial on a project on how to build an temperature sensor

By **Ali Ferzali, af223qa, 19990827-0177 ; For Linnaeus University.**

- Table of Content
[ToC]

## Project Overview

This project describes how to easily create a sensor that monitors the temperature of your room or any other surrounding you deceive to use this in. There's some connecting cables, sensors and the use of the breadboard, and some use for programming, it's really easy to understand and use. 

Following this guide step by step, will take about 3 hours at the most, it can be done in under 1 hour if you have prior experience with IOT ( sensors, cables, breadboard) and Programming (Micropython in our case)

### Purpose of the project and why I created It [Objective]


I Choose to make this project because I wanted to be able to know and check the temperature in my room and house, I also wanted to know when I should turn on my fan, It's also related to my mother, she can't tolerate a lot of heat and sunstroke because of her illnesses, so I wanted to know when I should turn on my fan, because If it's not that hot or warm, and I turn on the fan, my mother will become dizzy and sick of the cold air. This all leads to the reasons I chose this specific project, and the purpose that this project will serve, it also gives insights into the temperatures in my room and how that affects everything.


## Material
I bought to bundles, LNU – 1DT305 Tillämpad IoT – LoPy4 and sensors bundle 949.00kr , and the Sensor Kit – 25 moduler bundle 299.00 kr. You also need 3 Cables to connect the device and the breadboard with the Temp Sensor.
**You only need 1 of all the listed material below !**


| Part | Specification |Purchase location | Cost [SEK]|
| -------- | -------- | -------- |-------- |
| Pycom FiPy     | Development board     | Electrokit    |From the Electroit Bundle    |
| Pycom Expansion Board     | Provides and allows USB, battery and pin access to the FiPy     | Electrokit     |From the Electroit Bundle      |
| Pycom  Antenna       | Enables wireless network connectivity     | Electrokit      | From the Electroit Bundle    |
| MCP 9700e     | Temperature sensor     | Electrokit     | From the Electroit Bundle 
| Breadboard     | Makes it easy to connect cables and components.     | Electrokit     | From the Electroit Bundle 

[Electrokit IOT and Sensors Bundle](https://www.electrokit.com/en/product/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/) 
[Electrokit Sensor Bundle](https://www.electrokit.com/en/product/sensor-kit-26-modules/)
### Computer setup

I happen to have prior experience with python, and programming, I used pycharm IDE for python and VSC ( Visual Studio Code) as my IDE for C programming, but when it came to micropython and pycharm compatible IDE's, I only have atom and vsc as my choices, and as a person with prior use experience with vsc, I used that as my IDE for this IOT course. Installing the pymakr package was really easy, just go to Extensions-> search for pymakr-> Click install-> Done, now you have a pybytes REPL in VSC. There also needs to be a python installation of python on your computer. To install python click on the link which can be found here: [:link:][Python]. Another Step is too install node.js which can be found here: [:link:][NodeJS]
Pycom provides an easy tool to change the firmware of your LoPy4 here: [:link:][Firmware] download the installation file for your OS, install and run! Now you choose between several firmwares and if you want to do an erase of the system before (recommended) under advanced settings. 
To run code with VSC and your LoPy4 connected to the computer you have to make sure that your latest/current project is selected and press the run button. A great alternative is to set up your device on the pybytes website, especially if you want to communicate with the LoPy4 without it being connected to your computer. [:link:][Pybytes]Here you can sign up, set up a network connection over WiFi, LoRa, Sigfox or LTE (if you have a FiPy). It also let's you execute code over an online pybytes REPL.

To summarize all of this, make sure to;
1.Connect the FiPy to the expansion board (LED of the FiPy towards the micro-USB port) and then connect it all to your computer with a micro-USB cable.
2.Download the Pycom Device Firmware Updater and update your FiPy to the latest pybytes version. 
3. Install vsc (if you're following my guide, or else install atom)
4. Make sure to install the pymakr extension by following my vsc guide for it.


[Python]: https://www.python.org/downloads/
[NodeJS]:https://nodejs.org/en/
[Firmware]:https://docs.pycom.io/gettingstarted/installation/firmwaretool/
[Pybytes]: https://pybytes.pycom.io/

### Putting the sensor setup together



If you have the Lopy4 and the setup that I have follow this:
With the flat side of the TMP36 facing you, make the following connections:

1. Connect G3 (P16) on the expansion board to the middle pin on the TMP36.
2. Connect 3V3 on the expansion board to the left pin on the TMP36.
3. Connect GND on the expansion board to the right pin on the TMP36.

I also took the personal choice to connect the cables to place B - 12, 13, 14 on the breadboard. I also put the temp sensor at E- 12, 13, 14.

This is taken and quoted from [Source](https://www.losant.com/blog/how-to-read-the-tmp36-temperature-sensor-with-pycom-and-sigfox)

![](https://i.imgur.com/UMrlMmm.png)

I used the fritzing program to create a diagram and visual representation of my circuit , cables and sensor placement. There were no Lopy4 devices in the parts selection, so I had to use an arduino so at least you can get a visual idea on how it should look like. In the device that I have, GND and 3v3 are right beside each other, it's not like that on the arduino that's why it's not an exact copy.




Don't forget to make sure the LoRa antenna is connected to the 868/915 MHz port. Pycom recommends that you always attach the antenna to avoid damaging the device when using LoRa.





### Platform Selection and Choices

Pybytes is my choice for the platform that I will use to send my sensor data too, it's the easiest cloud based solution that is currently available, it also provides an acceptable level of data visualizations and customization. It's relatively easy to connect VSC to pybytes, and there were some hassle that I had, where pybytes couldn't find my device. I solved that issue by resetting the device, by clicking on the reset button close to the antenna jack on the device.
 
 
 ### The Code


For the temperature sensor I searched around a bit and found this great guide on how to get it started: [Temperature Sensor Guide](https://www.losant.com/blog/how-to-read-the-tmp36-temperature-sensor-with-pycom-and-sigfox)

- Code:
```python=1

import pycom
import machine
import time
# Getting the Raw Data from the sensor
adc = machine.ADC()
apin = adc.channel(pin='P16')

# Calculates the Temp
while True:
  millivolts = apin.voltage()
  celsius = (millivolts - 500.0) / 10.0

#Sending to pybytes in channel 1
  pybytes.send_signal(1, celsius)
  
  print(celsius) # If you want to have the option to print out in vsc
  
  time.sleep(30)

```

My File structure looks like this, Library is where I put any sensor information that has to do with the foreign library from for instance https://github.com/iot-lnu/applied-iot/tree/master/sensor-examples
There are different boot configurations you can use and have if you want to do that. The main part of all of this, is the main file, this is where you put what you want to run, you can see the main code above this.

![](https://i.imgur.com/8dyhKUW.png)


### Connectivity

The data is sent every 30 seconds over WiFi.
Pybytes.send_signal is used for transmitting one signal for each type of measurement with the MQTT protocol.
The LoPy4 is plugged into my pc through a usb 3.0 port.
The Wifi option consumes more power and electricity,  but the close proximity to my router ensures a good signal, while keeping the setup stationary through my pc, the power doesn't really matter and a LoRa/Sigfox signal would be hampered by there being a wall between my room and our home router, and it's a distance from the LoRa/Sigfox location/hotspot and my apartment.


### Presenting data

![](https://i.imgur.com/dMKn5D3.png)

Pybytes is easy to understand, use and customize to be able to present the input we get from the sensors in a visual way, it allows us to see things in an easier way and understand the trend and change over time. It also helps that the data is saved for a month.


### Finalizing the design


![](https://i.imgur.com/dVb7UQa.jpg)
![](https://i.imgur.com/EfzbZ5n.jpg)
![](https://i.imgur.com/wNJD8DS.jpg)


### Final Thoughts and Ideas
If I had a chance to change something that would have saved me a lot of hassle and time it would be my issues with getting pybytes to recognize my IOT device, I found a way to solve that by  by resetting the device, by clicking on the reset button close to the antenna jack on the device.
But in summary the project went fine, I really enjoyed myself, and I learned a lot and had a lot of fun, I would really like to thank all the TA:S and my fellow classmates for all the help and support during this course, and also thank Fredrik and Linnaeus University for the great IOT course :) 

