---
# System prepended metadata

title: How to build a temperature sensor

---

# How to build a temperature sensor
**My name:** Oskar Hjalmarsson
**Student credentials:** oh222ia

**Project overview**
I will use a lopy, a temperature sensor to create a mini-weatherstation.
**Time to make:** approximately 2 hours 

# Objective
Before this project I was faced with the problem of needing to check the temperature in our greenhouse to see if i needed to open the windows and doors to make it cooler. And sometimes I forgot to close the door and windows during night and the temperature dropped.That was the main reason I chose this project.

The purpose of the project is to keep track of the temperature in the greenhouse.

Hopefully after this project I will gain insight on how the temperature changes in the green house and how the temperature in the green house is affected by the weather outside.

# List of materials					

**Temperature sensor MCP9700**
	Sensor which measures the temperature.	

		 https://www.tme.eu/se/details/mcp9700-e_to/temperaturomvandlare/microchip-technology/?brutto=1&gclid=EAIaIQobChMItOyrmaW46gIViKkYCh0QwQ-BEAkYAyABEgLgIvD_BwE

Price: 3 for 3 sek

**LoPy4**			 							
	Device that collects the data from the sensors and then sends the data in this case via wifi to a cloud unit.  

https://pycom.io/product/lopy4/

Price: 366,17 sek
	
**Expansion board** 
	Used as an expansion to the lopy4 so you can connect it with Micro Usb cabel and male jumper wires. Has more features you can read about in the link.	

https://pycom.io/product/expansion-board-3-0/

Price: 188 sek

**Micro USB cable**  
	A simpe Micro Usb – Usb cabel which makes it possible to connect the lopy4 device to a computer or a charger.

https://www.netonnet.se/art/ljud-bild/kablar/micro-usb-kabel/andersson-micro-usb-cable-1m-black-2-4a/1004158.13717/

Price: 79 sek

**Breadboard** 
	This is a breadboard which makes it possible to make circuits with electrical components without welding. Perfect for prototyping. 

https://www.kjell.com/se/produkter/el-verktyg/elektronik/elektroniklabb/kopplingsdack-400-hal-p36285

Price: 90 sek

**Jumper wire** 
	Jumperwires are used to connect different type of components on the breadboard. They are basically wires that are made to be used with breadboards and expansionsboard and similar components.

https://www.kjell.com/se/produkter/el-verktyg/elektronik/elektroniklabb/luxorparts-delbar-kopplingskabel-40-pol-hane-hane-p87901

90 sek


The materials used were included in a kit which were bought at electrokit. This is no longer available so i have found a few different sites where you can buy the components. The prices are in some cases translated from sek to euro and are estimate prices.

 Link to were the materials were bought:  https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/

# Computer setup

**Chosen IDE:**
https://atom.io/

I have usen the IDE Atom for this project. To get it working simply download it and install it from the link above. After you have downloaded it you will need to install the plugin ”pymakr”.

To do it, start the Atom, then press File>Settings. A window will open under a tab called settings. In that window there is a menu, press ”Packages”. Write pymakr and install the pymakr packages.

**How to flash and update the expansionboard:**
https://docs.pycom.io/pytrackpysense/installation/firmware/

You might need to update your expansionboard with the latest firmware. The way to update varies a bit depending on which operating system you have on your computer( there have been problems on mac and windows 7).  Check on the backside of your expansionboard which version you have. Click on the link above then choose either v3.0 or 3.1 as shown in the picture below:
![](https://i.imgur.com/f5KAnDI.png)
 
 
After that follow the instructions on the link to update. 	https://docs.pycom.io/pytrackpysense/installation/firmware/

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

**How the code is uploaded:**
The code is uploaded via Atom. Copy the code from the link into a new file in Atom. Then create a project folder and save the file in it as main.py. The next step is to connect your lopy to Atom. Put the lopy on the expansionboard as the picture shows.

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

Then proceed to put in the Micro Usb cabel, first in the lopy4 expansion board then in your computer. If you have flashed your expansionboard or have it updated you should see that something has connected as the picture below shows.
![](https://i.imgur.com/KzUBV0W.png)

Now you should be ready to upload your code. You should have a folder with a chosen project name on your computer. In this folder you should have the main.py with your code pasted in. The code is found below. 

Press File>Add Project Folder then add your project folder. 

Make sure you have the project folder selected then press the ”Upload project to device” button or ctrl+alt+s.


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

# Putting everything together:
**How to connect the sensors to the lopy:**
 This setup is for development only. Put the sensor on the breadboard as shown below. Then connect the it to the lopy. The red wire goes from 3v3 pin on the upper pin on the sensor. The yellow goes from the middle sensor to the P16 pin ( G3), this is where the sensor output get sent. The black cabel goes from GND from the lopy to the the lower pin on the sensor. Be sure to put the sensor in the right direction as shown in the picture or change the wiring accordingly.
![](https://i.imgur.com/PWexUhP.png)

# Platform

**To get started:**
In order to use pybytes you need to register an account, register your lopy-device and if you use wifi, register your wifi.

Click on the link: https://pybytes.pycom.io/ and register an account.

Log in and follow these steps on the webpage:
![](https://i.imgur.com/18rN6vA.png)


Click on the ”getting started” button to the left. Then click on ”Configure Networks”

Then click on add wifi. After that you will need to enter the name of your wifi and its password. 

Next step is to add a device to pybytes. Connect you device to the computer with usb.
![](https://i.imgur.com/ms43GNV.png)

Then click on the device symbol on the left menu. After that click ”ADD VIA USB”.

Then choose LOPY4. Under next step choose wifi under the ”ENABLE NETWORKS” menu. Click next.

Choose name and the wifi-connection for the device. 

Next step is to Install the pycom firmware updater. Follow the steps in this link to the official documentation. https://docs.pycom.io/pybytes/connect/quick/

**Thoughts on choice of platform:**
I have been using Pybytes as my platform where I recieve data. Pybytes is a cloudbased platform where you can receive and save data via wifi, LoRa, Sigfox or Lte. Since there wasn’t any free LoRa gateway close enough I have used wifi to send data. However when I move my unit out to the greenhouse I will either have to pay for sigfox or set up an LoRa gateway to collect the data.

 A continuation of this project is to create many weatherstation units to be able to measure how the microclimate in cities are affected by different buildings, materials and roads and parks and so forth. Depending on the size of the project an own LoRa gateway might be the way to go. 

I will also explore more options for different platforms than pybytes to get a better understanding of what works best.

The code

The temperature sensor changes resistance depending on the temperature. This makes it possible to calculate the temperature from the change in voltage as shown below. Then I send the temperature value ”degC” to pybytes channel 1 via wifi. 

This codes takes data, a change in voltage received on pin 16 

```
import machine
import struct
import time
import pycom


adc = machine.ADC()
apin = adc.channel(pin='P16')

while True:
    millivolts = apin.voltage()
    degC = (millivolts - 500.0) / 10.0

    print(degC)

    pybytes.send_signal(1, degC)
    time.sleep(10)
```


# Presenting the Data
 I have chosen to send data every 10 seconds. The size of the data sent is 8 bytes and it is sent via wifi. Pybytes uses MQTT. 

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

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

# Finalizing the design

I had in mind a more advanced project but settled on this because I lacked time. Before starting I didn’t read the course description and thought this course was 50% and ending in late august.

However I have learnt alot during this course about all kinds of iot-related information and how to setup an unit. I am feeling enthusiastic about starting a new project in the coming weeks.

Alot of aspects of this project could have been improved. I could have connected to sigfox, bought a batteryholder, welded together the sensor and put it outside in the greenhouse instead of keeping it as a prototype. I could also have investigated the precision of the sensor and maybe created a graph that better suited it. I could have used webhooks to send notification to my phone if the temperature got to hot or cold.

![](https://i.imgur.com/RKofYPt.jpg)
