# How to build a light inidicated temperature sensor **Written by Elias Holmér, EH223UF.** ## Project overview The objective of this project is to build a temperature sensor which collects and stores the current temperature over the internet. The temperature sensor is intended to be used in an office and will feature three colored LED's to visually let the user know if the temperature is at an acceptable or non acceptable level. This project should take about 2-4 hours depending on previous knowledge. ## Purpose The device that aims to be constructed during this tutorial is meant to be used in a home office where the temperature during the day often rises to concerning degrees i.e. over 30 degrees celsius at some hours of the day. The light indicators serves the purpose to tell the user when the temperature has risen to high and that the user should either try to remedy it or stop working to resume when the temperature has dropped to a more comfortable degree. The device will also collect data during operation and present it in a graph such that the user can see at what hours it would make sense to get some work done. ## Material |Materials |Links to product| |-----|--------| |LoPy4|[Link ~39USD](https://pycom.io/product/lopy4/)| |Expansion Board|[Link ~18USD](https://pycom.io/product/expansion-board-3-0/)| |BreadBoard|[Link ~7USD](https://www.electrokit.com/produkt/kopplingsdack-400-anslutningar/)| |Jumpwires|[Link ~4USD](https://www.electrokit.com/en/product/test-wires-100mm-m-m-30pcs/)| |MCP9700|[Link ~1USD](https://www.electrokit.com/en/product/mcp9700-e-to-to-92-temperature-sensor/)| |LED's|[Link ~14USD](https://www.electrokit.com/en/product/lysdiodsats-5-farger-300st-2/)| |Battery pack|[Link*Optional ~3USD](https://www.electrokit.com/en/product/battery-holder-3xaaa-with-switch-and-jst-connector/)| |USB cable|[Link ~2USD](https://www.electrokit.com/en/product/usb-kabel-a-hane-micro-b-hane-15cm/)| ## Materials, short explanation * LoPy4, a programmable microcontroller using micropython. * Expansion Board, allows the LoPy to connect to a micro-usb cable and pins. * Breadboard, solderfree way of connecting the microcontroller to components. * Jumpwire, used to make connections between components and the breadboard. * MCP9700, a temperature sensor used to measure the sourounding temperature. * LED's, light emitting diodes used to visually indicate the temperature. * Battery pack, optional component allowing the project to become wireless making placement indepentent of a wired power source. * USB-cable, used to communicate between the microcontroller via the expansionboard and a computer. ## Computer setup To program the microcontroller the IDE [ATOM](https://atom.io/) vs. 1.58.0 was used, follow the link for a tutorial on how to install. The language used was micropython since it works fine with both Atom and the LoPy4. Inside Atom the Pymakr addon was installed. To do this open up Atom and go to File -> settings -> install, search for pymakr and select install. The package should install itself automatically and once this is done you have successfully gained access to the pybytes REPL. A distribution of python will need to be installed on the computer which can be done following this [link](https://www.python.org/downloads/). For most a firmware update won't be needed, but it can be done using pycoms firmware updater tool for both the [expansion board](https://docs.pycom.io/updatefirmware/expansionboard/) and [microcontroller](https://docs.pycom.io/updatefirmware/device/). Click the links, download the updater tool that fits your OS and follow the instructions. When updating the microcontroller for the first time it is recommended to select the option to 'Erase during update' under the advanced options. This is a precaution to avoid issues regarding Wifi, Timers and other variables stored in the NVS/Config files. Checking this option during future updates should be done knowing that it also erases all Python code on the device. With the LoPy4 connected to your computer and Atom using the micro-USB, your code can be run with the playbutton on the bottom, just make sure the file you want to run is selected. Some frequent problems can be solved [here](https://docs.pycom.io/gettingstarted/troubleshooting-guide/). You will also need to connect your device to pybytes following pycoms very own [tutorial](https://docs.pycom.io/pybytes/gettingstarted/). The tutorial sets up it's communication using Wifi which is also what will be used throughout this project. When the tutorial has been completed your device will be able to send gathered data to your pybytes account where you can monitor and display your data in different forms. ## Setting up the sensor array ![Circuit schematic](https://i.imgur.com/wn08syK.png) #### Figure 1, wiring diagram In figure 1 above the circuitry can be seen for the project. In this circuit it is shown running with an external battery supplying the power. This could be skipped if the power supply is taken from the computer via the micro-USB cable. The temperature sensor is capable of running on 3.3 volts directly so no resistors need to be applied, according to the [datasheet](https://www.electrokit.com/uploads/productfile/41011/21942e-2.pdf) for the MCP9700. The restistance applied for the LED's is calculated using Ohm's law which is as following: $$U/R = I$$ The current for the LED's used in this project is rated to 25mA with a forward voltage of 2.1V and the voltage output is 3.3V from the LoPy4. Using the equation above the calculated resistance turns out to be 59 Ohms. A good practice for LED's is to use a resistance which is a fair bit higher than what is calculated. The reistance used in this project is going to be 330 Ohms because of the simple reason that it was the lowest one available at the time and there was no need to go lower. Using a higher resistance means a lower current which in turn means the LED will shine dimmer but a good enough light was produced with the resistance used. ## Platform The chosen platform is, as already described, pybytes. This is since it works great when integrated with Atom, is free and stores your data for a month. Pybytes is easy to use when following their tutorials as done in the computer setup section. ## The code To correctly read the values coming from the MCP9700 some math is neccessary. The following equation is applied to the signal voltage being read from the sensor: $$(MVin-500.0)/10.0 = Celsius$$ MVin is the millivoltage being picked up from the sensor. How this is implemented can be sen on lines 19 to 21 in the code shown in Figure 2. ![Figure 2](https://i.imgur.com/uudIojD.png) #### Figure 2, the code The libraries pycom, time and machine are all neccesary to import as well as the function Pin from machine. At line 6 the flashing LED that turns on when first connecting the LoPy4 is disabled. A counter variable holding an integer of value 0 is initiated. On line 10 and 11 the machine library is used to set up pin 16 as an input source which is used to read the signal from the temperature sensor. Lines 13 to 15 sets pin 8, 9 and 10 as inputs and stores them in fittingly named variables. At line 18 a while loop is started with a true condition which means the loop will run forever if not interrupted by an external signal or something goes wrong with the hardware. In the loop the temperature is read and rounded to a length of four digits. Following are three statements that will activate one of the LED's depending on the read temperature. To use different levels for the LED's just change the numerical values in the if statements on lines 24 and 27. After that the counter is indexed one step up, going from 0 to 1 on the first iteration of the loop, and then the system sleeps for one second. Another statement is checked on line 35, this time if the variable counter is larger than 9. If this is true the system will utilize pybytes to send a signal with identifier 1 and name "Celsius" to the pybytes database set up in Computer Setup. It will then return the counter to 0, this statement is used such that the system will only send data every tenth iteration in order to reduce the power consumption. The program then shuts of any lit LED and goes to sleep for ten seconds before starting the loop over. The purpose of the one second long sleep cycle at line 32 is to keep the LED lit for at least a second before being turned of again. Without the wait cycle the LED would just rapidly flash every ~eleventh second which the user probably wouldn't be able to se. ## Data transmission In the code displayed in figure 2 the temperature readings will be sent to pybytes every 11 * 10 = 110 seconds. Depending on the power source this might be too often. The code snippet in figure 2 was run taking power from a computer USB-port so the frequency of data transmission was of no problem, if using a battery it would make more sense to only send at, for example, every ten minutes. The data is sent through Wifi which was set up using the pybytes tutorial in Computer Setup. Line 35 in figure 2 shows the actual code for sending to pybytes using the [MQTT](https://mqtt.org/faq/) protocol. Using wifi does consume a bit more power but the device was run in an apartment very close to the router so implementing a LoRa/Sigfox signal wouldn't make sense. ## Presenting the data The data is sent from the microcontroller to pybytes which offers a range of easily set up presentation options such as graphs, charts and bars. The data is stored for a month and is saved every time new data is received from a device i.e. every 110:th second in this case. An example of how the dashboard could look is seen in figure 3. ![](https://i.imgur.com/DyX5YWt.png) #### Figure 3, pybytes dashboard When creating a new display or editing an existing one it is possible to change how the data is presented in the selected display as well as how many data points to show and how to scale the x-axis. ## Finalizing the design The final project device is a fragile one since it is put together usig a solderless breadboard and connected using jumperwires. The LED's legs are long and close to the resistors legs which in turn are also very long. This means that if the device is knocked around it runs a high risk of shortcircuiting the LED's or pulling out a wire. A 3D-printer would be a fine tool to design and produce a protective casing for the device but access to one wasn't possible during the developing. The project went smoothly without many hiccups, some problems were very time consuming during the start of the project since the flashing of the expansion board wasn't turning out allright. One of the problems had to do with the board being flashed in application mode. This was solved using the additional steps in the guide for the expansion board linked to in the computer setup step. Problems like these where due to inexperience with the pycom devices. The Atom IDE also presented some weird problems when trying to install the pymakr plugin. This was due to a faulty update from the developers that affected Windows 10 users. Instead of making a lengthy work around I switched to Ubuntu instead and continued the setup there. The intent of the device was to produce a graph of when the home office was at a temperature suitable for productive work. With the device and data gathered the working hours of the day could be switched around such that starting work early in the morning, around 4:00~4:30, meant the office was at a temperature around 22 to 23 degrees celsius. Work then continued until the device went from flashing orange to red and it was time to instead do some less mentally requiring activities(going to the gym or the beach) until the sun went around the building block and the office started to cool down again. Using a webhook in order to get a notification when the temperature started to near a cooler temerature would be a great addition to the project, however due to reading multiple summer courses while also working, time was of the essence and that function will have to be implemented further down the road. This project has added some great fundamental knowledge and after making this quite simple project more and more ideas has started coming to mind which I'm sure will be fun to make in the future. ![](https://i.imgur.com/nQoJ6ec.jpg) #### Figure 4, the device ![](https://i.imgur.com/fd5xZPs.png) #### Figure 5, temperature chart The fluctuating chart is most likely due to a larger fan that runs at high for most parts of the day on a swivel motion. ### Datasheet for MCP9700 [Datasheet](https://www.electrokit.com/uploads/productfile/41011/21942e-2.pdf) used to understand how to code a program able to read from the temperature sensor and how to connect it to the pycom LoPy4 device.