--- title: 'Temperature sensor' disqus: hackmd --- Temperature sensor - DS18B20 = William Millqvist - wm222bt ## Abstract This is a tutorial on how to connect the temperature sensor DS18B20 with the quadruple bearer board Lopy4. Then show how to send the data over to pybytes and display the temperature in a dashboard. It will give a good overview of how an IoT project works. In this tutorial will contian setup of Lopy4 and the sensor DS18B2a and also the programming and how the temperature is collected, sent and presented. Project duration: 10 hours. ## Table of Contents [TOC] ## Objective I want measure the temperature in my country house. You are never there in the winter and want to know that it does not get too cold. By using a temperature sensor and sending the data to any platform you do not need to go there and check. Since it is too far from the nearest LoraWan station to get any connection, wifi will be used to send the data. ## Material In the table below, there is a list of the required hardware for this project. The total cost for att componentes is €64 according to the links down below. No shipping is included in the price. The links are Pycom webshop and electrokit. Since only wifi is used you could reduce the price by buying a Wipy 3.0. |Component |Function|Price (euro)| Link | |---------|--------|--------|----| |Pycom LoPy4 |Microcontroller | €35 | [Pycom webshop](https://pycom.io/product/lopy4/) | |Pycom Expansion board 3.0 |Development board|€16 | [Pycom webshop](https://pycom.io/product/expansion-board-3-0/) | |Temperature sensor - DS18B20 |Collecting data |€4 | [Electrokit](https://www.electrokit.com/produkt/temperatursensor-ds18b20/) | |Breadboard |Self explanatory|€6 | [Electrokit](https://www.electrokit.com/produkt/kopplingsdack-400-anslutningar/) | |Jumper wires |Self explanatory|€3 | [Electrokit](https://www.electrokit.com/produkt/labbsladd-20-pin-15cm-hane-hane/) | ## Computer setup In this chapter I will explain the software setup needed for this project. I used Windows 10 and for IDE I chose to use Atom. Then you also need to install the extention Pymakr. I followed the [Pycom documentation](https://docs.pycom.io/gettingstarted/installation/) for software setup. Down below I will explain the simple steps for this setup. 1. Connect the Lopy4 to the expansion board. 2. Uppdating firmware on the expansion board. 3. Install Atom.io 4. Install the pymkr plugin in Atom 5. Connect the device by using a USB-cable and press *ctrl-alt-c* When the device is connected, you can create a project, write your program and upload it by pressing *ctrl-alt-s*. ## Putting everything together In this chapter I will describe how the electronics are connected. This is only for development and not for production. A finished product would for example need a case for protection. In the circuit diagram below contains the temperature sensor, a breadboard, the Lopy4, the expantion board and wiring. The sensor contains a resitor and need a operating voltage between 3.0 V and 5.5 V. This means 3.3 V from the Lopy4 is perfect. The sensor has three pins. It is connected to ground via the black cable, to 3.3V via the red cable and the data is sent and the data is sent via the blue cable to Pin 10. ![](https://i.imgur.com/iubZ89K.jpg) ## Platform In this project, Pybytes platform is used to present the data that is collected. It was done because it was easy to start with. It only needed a few extra lines in the code to send the data, but it was also free. It shows the data that comes into a table with also a line chart to see the recent developments. In further development, another platform like google cloud or ubidots could be used. There, you could set it to send a notification if the temperature would drop more than normal. This had meant that you do not have to go in and check the temperature at regular intervals. ## MicroPython code The code down below is m</i>ain.p</i>y for my program. You need a library for the onewire sensor, that I took from [Pycom documentation](https://docs.pycom.io/tutorials/all/owd/). The first while-loop just finds an initial temperature, and spins until it finds a reasonable value (over 10 degrees). The second while-loop finds a second temperature. If it is within 1 degree from the previous value, it sends the mean of these two values to pybytes. This means that you get a smoother change in temperatur. Then the second value is set to the old one and a new temperature is measured again. But if the new value is more than 1 degree away from the old one, it prints out "Invalid value" and measure a new value. ```python= import pycom import time from machine import Pin from onewire import DS18X20 from onewire import OneWire # DS18B20 data line connected to pin 10 ow = OneWire(Pin('P10')) temp = DS18X20(ow) sensor_temp = [0,0] while sensor_temp[0] < 10: sensor_temp[0] = temp.read_temp_async() while True: sensor_temp[1] = temp.read_temp_async() if abs(sensor_temp[1] - sensor_temp[0]) < 1: print((sensor_temp[0]+sensor_temp[1])/2) pybytes.send_signal(1, (sensor_temp[0]+sensor_temp[1])/2) time.sleep(1) sensor_temp[0] = sensor_temp[1] temp.start_conversion() time.sleep(1) else: print("Invalid value") ``` ## Data transmission and connectivity The data is submitted to pybytes. It sends the temperature every 2 seconds. Since there was no Lora gateway close enough, I used Wifi as wireless protocol. This means that I do not need to worry about the number of bytes. When you use Pybytes, the transport protocol is MQTT. I used the function below to send the temperaure to pybytes ([Pycom](https://docs.pycom.io/pybytes/api/send_signal/)). ``` pybytes.send_signal(signal,value) ``` Signal means that all values that are sent on the same signal will be presented together. In this case the value is the measured temperature. if lora had been working it might have been better, because it is less pwer consuming. ## Presenting the data The line chart below is the temperature from pybytes. It is degrees Celsius on the y-axis and time on the x-axis. ![](https://i.imgur.com/iG0VIXE.jpg) The data is saved every 2 seconds and I choose Pybytes platform because of the simplicity. ## Finalizing the design In the following figure you can see the final device. ![](https://i.imgur.com/SyoOPuu.jpg) The main goal with this project was to measure temperature and send it to the internet. This is completed and works well. To develop the project even more, try to send a notification to the user if the temperature gets to low. It may not be so necessary to send data as often. The temperature may need to be checked only once every minute. In addition to this, the project worked well.